Java模拟.NET的连接池

开发者在线 Builder.com.cn 更新时间:2008-03-03作者:我就喜欢 来源:CSDN

本文关键词: .NET 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);
 }

用户评论

  • 用户名
  • 评论内容