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

Newperson 2003-03-25 05:38:51
以下是从网上找到的一个连接池联接,大家请看一下(由于太长无法粘贴,请见谅)http://www.swm.com.cn/yingyong/rj-99-yy9/99-9-7.htm
我看过之后不知怎么用,如何写一个类并实例化一个并能够连接数据库(假设为库名为OK)并执行查询和更新.谢谢!!(我是新手请多包含)
...全文
40 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
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自带的连接池。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧