首先...创建代码以在包含静态方法或变量的类中启动连接,如下所示..
private static ComboPooledDataSource cpds = new ComboPooledDataSource();
public static void MakePool()
{
try
{
cpds=new ComboPooledDataSource();
cpds.setDriverClass("com.mysql.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql://localhost:3306/att_db");
cpds.setUser("root");
cpds.setPassword("dragon");
cpds.setMaxPoolSize(MaxPoolSize);
cpds.setMinPoolSize(MinPoolSize);
cpds.setAcquireIncrement(Accomodation);
}
catch (PropertyVetoException ex)
{
//handle exception...not important.....
}
}
public static Connection getConnection()
{
return cpds.getConnection();
}
一旦你完成创建另一个用于服务器操作的类......
并从池中获取连接...
try{
con=DatabasePool.getConnection();
// DatabasePool is the name of the Class made earlier....
.
.
.
. // Server operations as u wanted.....
.
.
}
catch(SQL EXCEPTION HERE)
{
.....
}
finally
{
if(con!=null)
{
con.close();
}
}