各位高手请指点一下,怎样使用连接池(急)

Newperson 2003-03-25 05:38:51
以下是从网上找到的一个连接池联接,大家请看一下(由于太长无法粘贴,请见谅)http://www.swm.com.cn/yingyong/rj-99-yy9/99-9-7.htm
我看过之后不知怎么用,如何写一个类并实例化一个并能够连接数据库(假设为库名为OK)并执行查询和更新.谢谢!!(我是新手请多包含)
...全文
5 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
meizizi 2003-03-27
连接最好用线程管理。
下边是一个较简单的例子。

import java.io.*;
import java.util.*;
import java.sql.*;
import com.system.datasource.dbquery.*;
import com.utility.simplequery.*;


public class DataSourceManager {

private static DataSourceManager instance = null;
//private static String jndiName =null ;

public static DataSourceManager getInstance() {
if (instance == null)
instance = new DataSourceManager();
return instance;
}
/**
* 从数据源 jdbc/DBDataSource 得到连接。
* 这个数据源要你自己建,在WebSphere和Weblogic都有管理控制台给你建的。
*/
public Connection getConnection(String jndiName) throws Exception {
Connection conn = null;
try {
javax.naming.Context ctx = new javax.naming.InitialContext();
javax.sql.DataSource ds =
(javax.sql.DataSource) ctx.lookup(jndiName);
conn = ds.getConnection();
} catch (Exception ex) {
throw ex;

}
return conn;
}

public Connection getSQLServerConnection(String jndiName) throws Exception {
Connection conn = null;
try {
javax.naming.Context ctx = new javax.naming.InitialContext();
javax.sql.DataSource ds =
(javax.sql.DataSource) ctx.lookup(jndiName);
conn = ds.getConnection();
} catch (Exception ex) {
throw ex;
}
return conn;
}
public void releaseAll(Connection conn, ResultSet rSet) throws SQLException {
try {
if (rSet != null) {
rSet.close();
}
} catch (SQLException e) {
throw e;
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
throw ex;
}
}

public DBQuery execute(String sql, DBType dbType,String jndiName) throws SQLException {
Connection connection = null;
PreparedStatement pStmt = null;
ResultSet rSet = null;
try {
if (dbType != null && dbType.dbtype.equals("oracle")) {
connection = this.getConnection(jndiName);
} else {
connection = this.getSQLServerConnection(jndiName);
}
pStmt = connection.prepareStatement(sql);

} catch (SQLException e) {
pStmt = connection.prepareStatement(sql);
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
try {
rSet = pStmt.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
DBQuery query = new DBQuery(pStmt, rSet, connection);
return query;
}

public int executeUpdate(String sql,String jndiName)
throws SQLException {
int flag = 0;
Connection connection = null;
Statement stmt = null;
try {
connection = this.getConnection(jndiName);
stmt = connection.createStatement();
flag = stmt.executeUpdate(sql);

} catch (SQLException ex) {
try {
if (stmt != null ) {
stmt.close();
}
}catch(Exception e ) {
e.printStackTrace();
}
try {
if (connection != null ) {
connection.close();
}
}catch(Exception e ) {
e.printStackTrace();
}
System.err.println("DataSourceManager.executeUpdate() insert:" + ex.getMessage());
throw ex;
}catch(Exception e) {
e.printStackTrace();
}

return flag;
}

}
回复
yzxasd 2003-03-25
这样用

Statement state;
DBConnectionManager dbManage = DBConnectionManager.getInstance();
Connection conn=null;
conn = dbManage.getConnection("idb");
try{
state = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = state.executeQuery("select * from NodeTable");
rs.last();
System.out.println(rs.getRow());
}catch(SQLException e){
e.printStackTrace();
}
//System.out.println(conn);
dbManage.freeConnection("idb",conn);
dbManage.release();
回复
q123a123z123 2003-03-25
要根据连接池实例化函数所要求的参数来传递,我做的连接池是传递xml文件中的一个值,实例化时根据该值配置数据库等
回复
UserJavaPerson 2003-03-25
连接池一般采用weblogic、websphere自带的连接池。

回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2003-03-25 05:38
社区公告
暂无公告