62,612
社区成员
发帖
与我相关
我的任务
分享
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));
}
}
}
}