当前位置: 技术问答>java相关
高手看看这样写的连接池有没有用?
来源: 互联网 发布时间:2015-11-09
本文导语: //利用Oracle自带的连接池类的一例 /** 封装了对数据库的连接,用于处理SQL语句。 @author:yancheng(sharetop studio) @version:1.0.0 */ package poolConn; import java.sql.*; import java.io.*; import javax.sql.*; import javax.naming.*; import oracle.jdbc....
//利用Oracle自带的连接池类的一例
/**
封装了对数据库的连接,用于处理SQL语句。
@author:yancheng(sharetop studio)
@version:1.0.0
*/
package poolConn;
import java.sql.*;
import java.io.*;
import javax.sql.*;
import javax.naming.*;
import oracle.jdbc.pool.*;
public class OraPooledSQL
{
private PooledConnection dbpool;
/**
@param ConnectionURL 连接名 如: jdbc:odbc:myODBC
@param UserID 用户名
@param PassWord 用户密码
*/
public OraPooledSQL(String ConnectionURL,String UserID,String PassWord)
{
try{
OracleConnectionPoolDataSource ocpds = new OracleConnectionPoolDataSource();
ocpds.setURL(/tech-qa-java/ConnectionURL/index.html);
ocpds.setUser(UserID);
ocpds.setPassword(PassWord);
dbpool = ocpds.getPooledConnection();
} catch(Exception ex) {
System.err.println("Error in PooledSQL-construct : ");
ex.printStackTrace(System.err);
}
}//end OraPooledSQL
//close dbpool
protected void finalize()
{
if( dbpool != null )
{
try
{
dbpool.close();
}
catch(Exception ex) { ex.printStackTrace() ; }
}
}
/**
用于更新、添加或删除的SQL语句
@param SQL SQL语句字串,如:insert into tablename values(id,......)
*/
public int Update(String SQL)
{
Statement stmt = null;
int rc = 0;
Connection connection = null;
try
{
connection = dbpool.getConnection();
stmt = connection.createStatement();
rc = stmt.executeUpdate(SQL);
}
catch( Exception ex )
{
System.err.println("Error in Update - OraPooledSQL : ");
ex.printStackTrace(System.err);
}
return rc;
} //end Update()
/**
用于查询的SQL语句
@param SQL SQL语句字串,如:select * from tablename
*/
public ResultSet Query(String SQL)
{
Statement stmt = null;
ResultSet rs = null;
Connection connection = null;
try
{
connection = dbpool.getConnection();
stmt = connection.createStatement();
rs = stmt.executeQuery(SQL);
} catch( Exception ex )
{
System.err.println("Error in Query - SQLBean : ");
ex.printStackTrace(System.err);
}
return rs;
} //end Query
} //end Class
/**
封装了对数据库的连接,用于处理SQL语句。
@author:yancheng(sharetop studio)
@version:1.0.0
*/
package poolConn;
import java.sql.*;
import java.io.*;
import javax.sql.*;
import javax.naming.*;
import oracle.jdbc.pool.*;
public class OraPooledSQL
{
private PooledConnection dbpool;
/**
@param ConnectionURL 连接名 如: jdbc:odbc:myODBC
@param UserID 用户名
@param PassWord 用户密码
*/
public OraPooledSQL(String ConnectionURL,String UserID,String PassWord)
{
try{
OracleConnectionPoolDataSource ocpds = new OracleConnectionPoolDataSource();
ocpds.setURL(/tech-qa-java/ConnectionURL/index.html);
ocpds.setUser(UserID);
ocpds.setPassword(PassWord);
dbpool = ocpds.getPooledConnection();
} catch(Exception ex) {
System.err.println("Error in PooledSQL-construct : ");
ex.printStackTrace(System.err);
}
}//end OraPooledSQL
//close dbpool
protected void finalize()
{
if( dbpool != null )
{
try
{
dbpool.close();
}
catch(Exception ex) { ex.printStackTrace() ; }
}
}
/**
用于更新、添加或删除的SQL语句
@param SQL SQL语句字串,如:insert into tablename values(id,......)
*/
public int Update(String SQL)
{
Statement stmt = null;
int rc = 0;
Connection connection = null;
try
{
connection = dbpool.getConnection();
stmt = connection.createStatement();
rc = stmt.executeUpdate(SQL);
}
catch( Exception ex )
{
System.err.println("Error in Update - OraPooledSQL : ");
ex.printStackTrace(System.err);
}
return rc;
} //end Update()
/**
用于查询的SQL语句
@param SQL SQL语句字串,如:select * from tablename
*/
public ResultSet Query(String SQL)
{
Statement stmt = null;
ResultSet rs = null;
Connection connection = null;
try
{
connection = dbpool.getConnection();
stmt = connection.createStatement();
rs = stmt.executeQuery(SQL);
} catch( Exception ex )
{
System.err.println("Error in Query - SQLBean : ");
ex.printStackTrace(System.err);
}
return rs;
} //end Query
} //end Class
|
这只是对数据库的操作,不是连接池
我给你个连接池
package myclasses;
import java.sql.*;
import java.util.*;
public class ConnectionPoolBean
{
static String IP="192.168.0.101";
private int checkedOut; //the number of connections being used
private Vector freeConnections=new Vector();//the store of connections;
//always use the first one
private int maxConn;//the up-bound of the connections available;
// 0 denotes no up-bound
private String sDBDriver = "oracle.jdbc.driver.OracleDriver";
private String sConnStr = "jdbc:oracle:thin:@billing:1521:hanzhou1";
public static String getWebIP() //initialization
{
return IP;
}
public ConnectionPoolBean() //initialization
{
checkedOut=0;
maxConn=1;
}
public synchronized void freeConnection(Connection con)
//return a connection to the ConnectionPool
{
freeConnections.addElement(con);
checkedOut--;
}
public synchronized Connection getConnection() throws Exception
//to get a usable connection;
// if returnvalue=null, then no connection available
{
Connection con=null;
if (freeConnections.size()>0) //there are connections which can be reused
{
con=(Connection)freeConnections.firstElement();
//get the first available connection
freeConnections.removeElementAt(0);
//remove it from the connectionPool
try
{
if (con.isClosed()) //if the connection is closed
{
con=getConnection();
}
}
catch(SQLException e) //if the connection is not valid
{
con=getConnection();
}
}
else //if there are no existing connections, then create a new one
if (maxConn==0 || checkedOut
我给你个连接池
package myclasses;
import java.sql.*;
import java.util.*;
public class ConnectionPoolBean
{
static String IP="192.168.0.101";
private int checkedOut; //the number of connections being used
private Vector freeConnections=new Vector();//the store of connections;
//always use the first one
private int maxConn;//the up-bound of the connections available;
// 0 denotes no up-bound
private String sDBDriver = "oracle.jdbc.driver.OracleDriver";
private String sConnStr = "jdbc:oracle:thin:@billing:1521:hanzhou1";
public static String getWebIP() //initialization
{
return IP;
}
public ConnectionPoolBean() //initialization
{
checkedOut=0;
maxConn=1;
}
public synchronized void freeConnection(Connection con)
//return a connection to the ConnectionPool
{
freeConnections.addElement(con);
checkedOut--;
}
public synchronized Connection getConnection() throws Exception
//to get a usable connection;
// if returnvalue=null, then no connection available
{
Connection con=null;
if (freeConnections.size()>0) //there are connections which can be reused
{
con=(Connection)freeConnections.firstElement();
//get the first available connection
freeConnections.removeElementAt(0);
//remove it from the connectionPool
try
{
if (con.isClosed()) //if the connection is closed
{
con=getConnection();
}
}
catch(SQLException e) //if the connection is not valid
{
con=getConnection();
}
}
else //if there are no existing connections, then create a new one
if (maxConn==0 || checkedOut