.NET的ADO.NET的本身包含连接池功能,而java是在第三方开发包中提高的连接池功能因此,需要去下载第三方的连接池包,但是java的连接池一般都是在EJB或者B/S系统中使用的。
作者:我就喜欢 来源:CSDN 2008年3月3日
关键字: java 连接池
private static PoolConnection getNewConnection() throws SQLException {
try
{
Class.forName( DriverName);
}catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
PoolConnection con = new PoolConnection(URL, User, Password);
con.setOnClose(new theOnClose(pConnectionVector));
return con;
}
synchronized public static void SetJDBC(String url, String user, String password) {
URL = url;
User = user;
Password = password;
}
synchronized public static void setURL(String url) {
URL = url;
}
synchronized public static String getUrl() {
return URL;
}
synchronized public static void setUser(String user)
{
User=user;
}
synchronized public static String getUser()
{
return User;
}
synchronized public static void setPassword(String password)
{
Password=password;
}
synchronized public static String getPassword()
{
return Password;
}
synchronized public static void setDriverName(String dName)
{
DriverName=dName;
}
synchronized public static String getDriverName()
{
return DriverName;
}
}
class theOnClose
implements OnConnectionClose {
private Vector v;
public theOnClose(Vector vt) {
v = vt;
}
public void Action(PoolConnection sender) {
v.remove(sender);
}
}
class PoolConnection
implements Connection , Serializable{
private Connection aCon = null;
private boolean closed = false;
private boolean inUse = false;
private String DriverName;
private OnConnectionClose onClose = null;
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
}
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
}
protected PoolConnection() {
}
public PoolConnection(String Url, String User, String Password) throws
SQLException {
aCon = DriverManager.getConnection(Url, User, Password);
closed = false;
inUse=true;
}
public PoolConnection(String Url) throws Exception {
aCon = DriverManager.getConnection(Url);
closed = false;
inUse=true;
}
public Statement createStatement() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method createStatement() not yet implemented.");
return aCon.createStatement();
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented.");
return aCon.prepareStatement(sql);
}
public CallableStatement prepareCall(String sql) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method prepareCall() not yet implemented.");
return aCon.prepareCall(sql);
}
public String nativeSQL(String sql) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method nativeSQL() not yet implemented.");
return aCon.nativeSQL(sql);
}