请问为什么,使用DBCP连接池得不到记录,而用DBPool却可以

liuzhengkang 2008-09-23 02:52:27
使用DBCP连接池得不到记录,而用DBPool却可以
package dbpooltest;
import java.sql.*;
import snaq.db.ConnectionPoolManager;
import org.apache.commons.dbcp.BasicDataSource;
/**
*
* @author lzk
* 2008-8-25 下午01:49:50
* @随机查询的实现
*/
public class SelectS {
private static Connection con = null;
private static PreparedStatement pstmt = null;
private static ResultSet rs = null;


public static Connection getDBPoolcon(){ //DBPool连接池
ConnectionPoolManager cpm = null;
Connection con = null;
long timeout = 2000;
try{
cpm = ConnectionPoolManager.getInstance("dbpool.properties");
}catch (Exception e){
System.out.println("Error Connection.getconnection(): "+e.getMessage());
}

try{
con = cpm.getConnection("localpool", timeout);
}catch (SQLException sqle){
System.out.println(sqle.getMessage());
}

return con;
}

public static Connection getDBCPcon(){ //dbcp连接池
Connection con = null;
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String URL = "jdbc:sqlserver://192.168.168.104:1433;DatabaseName=LTest";
BasicDataSource bds = new BasicDataSource();

try{
bds.setDriverClassName(driver);
bds.setUrl(URL);
bds.setUsername("sa");
bds.setPassword("ba123");
bds.setMaxActive( 50 ); //设置最大活动连接
bds.setMaxIdle( 10 ); //最大空闲连接
bds.setMaxWait( 10000 ); //最大等待时间
}catch(Exception e){
System.out.println("Error setConnection: "+e.getMessage());
}

try{
con = bds.getConnection();
}catch(Exception e){
System.out.println("Error getConnection:"+e.getMessage());
}
return con;
}


public static void main(String args[]) throws Exception{
PreparedStatement pstmt = null;

SelectS ss = new SelectS();
Connection DBPoolconn = ss.getDBPoolcon();
Connection dbcpconn = ss.getDBCPcon();
//System.out.println(dbcpconn);

for(int i=0; i<10; i++){
String selectSQL = "SELECT * FROM tablea_1 where num =?";
pstmt = dbcpconn.prepareStatement(selectSQL);
//pstmt = DBPoolconn.prepareStatement(selectSQL);
pstmt.setInt(1, i);
rs = pstmt.executeQuery();

while(rs.next()){
System.out.print(rs.getString(1)+" ");
System.out.println(rs.getString(2));
}
}
}
}





程序可以正常运行,但就是不知道为什么得不到记录,用DBPool就可以得到记录;
但使用System.out.println(dbcpconn);打印的结果是:
jdbc:sqlserver://192.168.168.104:1433;responseBuffering=full;encrypt=false;databaseName=LTest;selectMethod=direct;trustServerCertificate=false;lastUpdateCount=true;, UserName=sa, Microsoft SQL Server 2005 JDBC Driver

...全文
189 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
GGMMYQL 2008-09-27
  • 打赏
  • 举报
回复
顶啊
!!哈哈
liuzhengkang 2008-09-27
  • 打赏
  • 举报
回复
准备结贴了,帮顶呀!
liuzhengkang 2008-09-27
  • 打赏
  • 举报
回复
呜呜,还没有人来啊,
帮顶的有分!
liuzhengkang 2008-09-24
  • 打赏
  • 举报
回复
路过的朋友帮帮忙啊,要是分不够的话我再加
liaoyi_ipanel 2008-09-23
  • 打赏
  • 举报
回复
up

62,612

社区成员

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

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