📄 dbconnectionmanage.java
字号:
package com.study.database;
import java.sql.*;
import java.util.*;
public class DBConnectionManage {
private Hashtable connectionpool;
private int maxconnection;
private int currentconn;
public DBConnectionManage() {
init();
}
public static DBConnectionManage getInstance() {
return new DBConnectionManage();
}
public void init() {
connectionpool = new Hashtable();
maxconnection = 20;
currentconn = 0;
initConnection();
}
public void initConnection() {
DBConnection addconn = new DBConnection();
for (int i = 1; i <= maxconnection / 2; i++) {
addconn.setKey(i);
connectionpool.put(i, addconn);
}
}
synchronized public DBConnection getConnection() {
DBConnection dbconn = null;
try {
String url = "jdbc:sqlserver://localhost:2435;DatabaseName=exam";
String user = "sa";
String pwd = "sa2005";
String forName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
dbconn = new DBConnection(url, user, pwd, forName, 0);
currentconn++;
} catch (Exception ex) {
System.err.println("");
}
return dbconn;
}
public Connection getFreeConnection() {
Connection conn = null;
DBConnection model = null;
int key = 0;
boolean foundconnection = false;
for (int i = 0; i < connectionpool.size(); i++) {
Enumeration e = connectionpool.keys();
while (e.hasMoreElements()) {
key = Integer.parseInt(e.nextElement().toString());
model = (DBConnection) connectionpool.get(key);
conn = model.getConnection();
connectionpool.remove(key);
while (connectionpool.size() < maxconnection / 2) {
connectionpool.put(getMaxkey(connectionpool) + 1,
getConnection());
}
foundconnection = true;
break;
}
}
if(!foundconnection){
release();
initConnection();
model = getConnection();
conn = model.getConnection();
}
return conn;
}
private int getMaxkey(Hashtable cp) {
int maxkey = 0;
int tmpkey = 0;
Enumeration e = connectionpool.keys();
while (e.hasMoreElements()) {
try {
tmpkey = Integer.parseInt(e.nextElement().toString());
} catch (Exception ex) {
tmpkey = 0;
}
if (maxkey < tmpkey) {
maxkey = tmpkey;
}
}
return maxkey;
}
public void closeConnection(Connection conn){
if(conn!=null){
try{
conn.close();
}catch(Exception ex){
System.err.println(ex.getMessage());
}
}
}
public void release(){
Enumeration e = connectionpool.elements();
DBConnection model = null;
Connection modelconn = null;
while(e.hasMoreElements()){
model = (DBConnection)e.nextElement();
modelconn = model.getConnection();
closeConnection(modelconn);
}
}
public int executeUpdate(Connection conn,String query) throws SQLException{
Statement st = conn.createStatement();
return st.executeUpdate(query);
}
public ResultSet getResultSet(Connection conn,String query) throws SQLException{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
return rs;
}
public void closeStatement(Statement st) throws SQLException{
if(st!=null){
st.close();
}
}
public void closeResult(ResultSet rs) throws SQLException{
if(rs!=null){
rs.close();
}
}
public static void main(String args[]){
DBConnectionManage dbmanage = DBConnectionManage.getInstance();
Connection conn = dbmanage.getFreeConnection();
ResultSet rs = null;
String query = "select * from [student] ";
Statement stmt = null;
try{
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
if(rs.next()){
System.out.println("true");
}else{
System.out.println("false");
}
}catch(Exception ex){
System.err.print(ex.getLocalizedMessage());
}finally{
try{
rs.close();
stmt.close();
dbmanage.closeConnection(conn);
}catch(Exception ex){
System.err.print(ex.getLocalizedMessage());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -