import java.sql.*; import com.caucho.sql.*;public class db { Connection c = null; DBPool db = null; public db() { db = new DBPool(); } public Connection getConnection() throws SQLException { if (c == null) { return c = db.getPool("databaseName").getConnection(); } else { return c; } } public void clsConnection() throws SQLException { if(c != null) c.close(); //close Connection ; if(db != null) { /*if not setName(),the class will throw NullPointerException , *this is a bug in com.caucho.sql.* package maybe; *System.out.println(db.toString()); */ db.setName("poolName"); db.close(); //close DBPool; //System.out.println(db.toString()); not remove the db.setName(); //this operation will print: [DBPool poolName] } } }
说得对
要加上一个方法
public synchronized ConnPool getInstance()
{
return instance;
}
1、这个javabean的实例中只有一个connection对象;
2、这个javabean的实例中只有一个statement对象;
private static ConnPool instance=null;
public static synchronized ConnPool getInstance()
{
if (instance==null){
return new ConnPool();
}else{
return instance;
}
}
import com.caucho.sql.*;public class db { Connection c = null;
DBPool db = null;
public db() {
db = new DBPool();
} public Connection getConnection() throws SQLException {
if (c == null) {
return c = db.getPool("databaseName").getConnection();
}
else {
return c;
}
} public void clsConnection() throws SQLException { if(c != null) c.close(); //close Connection ;
if(db != null) {
/*if not setName(),the class will throw NullPointerException ,
*this is a bug in com.caucho.sql.* package maybe;
*System.out.println(db.toString());
*/
db.setName("poolName");
db.close(); //close DBPool;
//System.out.println(db.toString()); not remove the db.setName();
//this operation will print: [DBPool poolName]
}
}
}