/** create a instance pool */ public static synchronized ConnectionPool instance(){ if(!singleFlag){ singleFlag = true; pool = new ConnectionPool(); } return pool; }
/** init connection pool */ public void createConnection(){ for(int i = 0; i<DB_MAX;i++){ try{ Class.forName(sqlDriver); connection[i] = DriverManager.getConnection( url,user,passwd); status[i] = STATUS_PREPARED; }catch(SQLException sqle){sqle.printStackTrace(); }catch(Exception e){e.printStackTrace();} }//for }
/** get connection from connection pool*/ public synchronized Hashtable getConnection(){ Hashtable hash = new Hashtable(); for(int i = 0; i<DB_MAX;i++){ if(status[i] == STATUS_PREPARED){ status[i] = STATUS_INUSED; hash.put(new Integer(i),connection[i]); return hash; } } return null; }
/** put connection into connection pool*/ public synchronized void putConnection(Hashtable hash){ if(hash.keys().hasMoreElements()){ String s = hash.keys().nextElement().toString(); int i = new Integer(s).intValue(); status[i] = STATUS_PREPARED; } }
package classnotepad;
import java.sql.*;
import java.util.*;
public final class ConnectionPool{
private static int DB_MAX = 4;
private int STATUS_UNINIT = 1;//未初始化状态
private int STATUS_PREPARED = 2;//已经初始化状态
private int STATUS_INUSED = 3;//正在使用状态
private int STATUS_CLOSED = 4;//关闭状态
private int status[] = {STATUS_UNINIT,STATUS_UNINIT,STATUS_UNINIT,STATUS_UNINIT};
private static boolean singleFlag = false;
private static Connection[] connection = new Connection[DB_MAX];
private final String sqlDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
private final String url = "jdbc:odbc:liuyan";
private final String user = "administrator";
private final String passwd = "";
private static ConnectionPool pool;
/** singleton design pattern */
private ConnectionPool(){
for(int i =0;i < DB_MAX;i++){
connection[i] = null;
}
// createConnection();
}
/** create a instance pool */
public static synchronized ConnectionPool instance(){
if(!singleFlag){
singleFlag = true;
pool = new ConnectionPool();
}
return pool;
}
/** init connection pool */
public void createConnection(){
for(int i = 0; i<DB_MAX;i++){
try{
Class.forName(sqlDriver);
connection[i] = DriverManager.getConnection( url,user,passwd);
status[i] = STATUS_PREPARED;
}catch(SQLException sqle){sqle.printStackTrace();
}catch(Exception e){e.printStackTrace();}
}//for
}
/** get connection from connection pool*/
public synchronized Hashtable getConnection(){
Hashtable hash = new Hashtable();
for(int i = 0; i<DB_MAX;i++){
if(status[i] == STATUS_PREPARED){
status[i] = STATUS_INUSED;
hash.put(new Integer(i),connection[i]);
return hash;
}
}
return null;
}
/** put connection into connection pool*/
public synchronized void putConnection(Hashtable hash){
if(hash.keys().hasMoreElements()){
String s = hash.keys().nextElement().toString();
int i = new Integer(s).intValue();
status[i] = STATUS_PREPARED;
}
}
public synchronized void closeConnection(){
for(int i = 0; i<DB_MAX;i++){
try{
if(connection[i] != null){
connection[i].close();
connection[i] = null;
status[i] = STATUS_UNINIT;
}
}catch(SQLException sqle){sqle.printStackTrace();
}catch(Exception e){e.printStackTrace();}
}//for
}
}
比如:
String s=new String("zzzz") 问:建立了几个对象?
String 可以被继承么?为什么?
之类的东西。变态得很阿。