分享
 
 
 

JDBC的一个连接池,在我们项目中用到的。

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

package dbconnection;

import javax.servlet.http.*;

import java.io.*;

import java.sql.*;

import javax.naming.*;

import javax.sql.*;

import org.apache.commons.dbcp.BasicDataSource;

import org.apache.commons.dbutils.DbUtils;

public class DBConnection implements Serializable{

private java.lang.String dbUserID;

private java.lang.String dbPassword;

private java.lang.String dbDriver;

private java.lang.String dbURL;

private java.lang.String sqlString;

private java.sql.ResultSet result;

private java.sql.ResultSetMetaData meta;

private java.sql.Connection conn;

private java.sql.Statement sqlStmt;

private java.lang.String drcName;

private final static java.lang.String className = "dbconnection.DBConnection";

private java.lang.String initialContext;

/**

* DBConnection constructor comment.

*/

public DBConnection (){

super ();

}

/**

* Insert the method's description here.

* Creation date: (10/31/00 3:44:45 PM)

*/

public void close (){

String methodName = "close()";

DBConnection.closeResultSetAndStatement (sqlStmt, result);

DBConnection.closeConnection (conn);

}

/**

* Insert the method's description here.

* Creation date: (7/25/2003 5:34:40 PM)

*/

public void closeStatement (){

try{

if (sqlStmt != null){

sqlStmt.clearBatch ();

sqlStmt.clearWarnings ();

sqlStmt.close ();

}

} catch (SQLException e){

}

}

/**

* Insert the method's description here.

* Creation date: (11/2/00 10:51:57 AM)

* @exception java.sql.SQLException The exception description.

*/

public void commit (){

String methodName = "commit()";

try{

if (conn != null && !conn.isClosed ()){

conn.commit ();

}

} catch (SQLException e){

System.out.println (className + "." + methodName + " ==> " +

e.getClass ().getName () + ": " + e.getMessage ());

}

}

/**

* Insert the method's description here.

* Creation date: (10/6/00 4:04:34 PM)

*/

public void connect (){

String methodName = "connect()";

try{

if (conn == null){

if (!registerDriver (dbDriver)){

throw new SQLException ("Can't load JDBC Driver - " + dbDriver + ".");

}

/*

InitialContext context = new InitialContext ();

DataSource ds = (DataSource)context.lookup (drcName);

*/

DataSource ds = setupDataSource ();

Connection connection = ds.getConnection ();

conn = connection;

}

} catch (SQLException e){

System.out.println (className + "." + methodName + " ==> " +

e.getClass ().getName () + ": " + e.getMessage ());

}

/*

catch (NamingException e){

System.out.println (className + "." + methodName + " ==> " +

e.getClass ().getName () + ": " + e.getMessage ());

}

*/

}

/**

* Insert the method's description here.

* Creation date: (10/16/00 2:17:02 PM)

* @return dbconnection.DBConnection

*/

public static DBConnection dbconnect (){

String methodName = "dbconnect(String, String)";

DBConnection dbconn = null;

try{

// initialize dbconnection

dbconn = new DBConnection ();

// Get DB connection parameters from central getDBConnectionParm bean

getParm getDBConnectionParm = new getParm ();

getDBConnectionParm.init ();

dbconn.setDbUserID (getDBConnectionParm.getParameter ("userID"));

// out.println("After setDbUserID");

dbconn.setDbPassword (getDBConnectionParm.getParameter ("password"));

dbconn.setDbURL (getDBConnectionParm.getParameter ("URL"));

dbconn.setDbDriver (getDBConnectionParm.getParameter ("driver"));

dbconn.setDrcName (getDBConnectionParm.getParameter (

"ConnectionPoolLocation"));

dbconn.setInitialContext (getDBConnectionParm.getParameter (

"initial_context"));

System.out.println (dbconn.getDbUserID () + ", " + dbconn.getDbPassword () +

", " + dbconn.getDbURL () + ", " + dbconn.getDbDriver () +

", " + dbconn.getDrcName () + ", " +

dbconn.getInitialContext ());

dbconn.connect ();

System.out.println ("After dbconn.connect()");

} catch (SQLException e){

System.out.println (className + "." + methodName + " ==> " +

e.getClass ().getName () + ": " + e.getMessage ());

} catch (Exception e){

System.out.println (className + "." + methodName + " ==> " +

e.getClass ().getName () + ": " + e.getMessage ());

e.printStackTrace ();

}

return dbconn;

}

public static DBConnection dbconnect (String filename){

String methodName = "dbconnect(String, String)";

DBConnection dbconn = null;

try{

// initialize dbconnection

dbconn = new DBConnection ();

// Get DB connection parameters from central getDBConnectionParm bean

getParm getDBConnectionParm = new getParm ();

getDBConnectionParm.init (filename);

dbconn.setDbUserID (getDBConnectionParm.getParameter ("userID"));

// out.println("After setDbUserID");

dbconn.setDbPassword (getDBConnectionParm.getParameter ("password"));

dbconn.setDbURL (getDBConnectionParm.getParameter ("URL"));

dbconn.setDbDriver (getDBConnectionParm.getParameter ("driver"));

dbconn.setDrcName (getDBConnectionParm.getParameter (

"ConnectionPoolLocation"));

dbconn.setInitialContext (getDBConnectionParm.getParameter (

"initial_context"));

System.out.println (dbconn.getDbUserID () + ", " + dbconn.getDbPassword () +

", " + dbconn.getDbURL () + ", " + dbconn.getDbDriver () +

", " + dbconn.getDrcName () + ", " +

dbconn.getInitialContext ());

dbconn.connect ();

System.out.println ("After dbconn.connect()");

} catch (SQLException e){

System.out.println (className + "." + methodName + " ==> " +

e.getClass ().getName () + ": " + e.getMessage ());

} catch (Exception e){

System.out.println (className + "." + methodName + " ==> " +

e.getClass ().getName () + ": " + e.getMessage ());

}

return dbconn;

}

/**

* This method have Response Object to allow you to print some information on the browser.

* Creation date: (9/29/00 4:35:14 PM)

*/

private void execute (String sqltype) throws SQLException{

if (conn != null && !conn.isClosed ()){

sqlStmt = conn.createStatement ();

sqlStmt.executeQuery (getSqlString ());

if (sqltype.equals ("s") || sqltype.equals ("S")){

result = sqlStmt.getResultSet ();

meta = result.getMetaData ();

}

}

}

/**

* This method have Response Object to allow you to print some information on the browser.

* Creation date: (9/29/00 4:35:14 PM)

*/

public void execute (String sqlString, String sqlType) throws SQLException{

this.sqlString = sqlString;

execute (sqlType);

}

/**

* Insert the method's description here.

* Creation date: (10/6/00 4:04:06 PM)

* @return java.sql.Connection

*/

public java.sql.Connection getConn (){

return conn;

}

/**

* return the driver of db

* Creation date: (9/29/00 4:29:45 PM)

* @return java.lang.String

*/

public java.lang.String getDbDriver (){

return dbDriver;

}

/**

* return the password of db

* Creation date: (9/29/00 4:29:25 PM)

* @return java.lang.String

*/

public java.lang.String getDbPassword (){

return dbPassword;

}

/**

* Insert the method's description here.

* Creation date: (9/29/00 4:34:01 PM)

* @return java.lang.String

*/

public java.lang.String getDbURL (){

return dbURL;

}

/**

* Insert the method's description here.

* Creation date: (9/29/00 4:29:04 PM)

* @return java.lang.String

*/

public java.lang.String getDbUserID (){

return dbUserID;

}

/**

* Insert the method's description here.

* Creation date: (7/17/2002 12:17:51 PM)

* @return java.lang.String

*/

public java.lang.String getDrcName (){

return drcName;

}

/**

* Insert the method's description here.

* Creation date: (10/8/02 10:07:36 AM)

* @return java.lang.String

*/

public java.lang.String getInitialContext (){

return initialContext;

}

/**

* Insert the method's description here.

* Creation date: (10/2/00 1:35:04 PM)

* @return java.sql.ResultSetMetaData

*/

public java.sql.ResultSetMetaData getMeta (){

return meta;

}

/**

* Insert the method's description here.

* Creation date: (9/29/00 5:05:13 PM)

* @return java.sql.ResultSet

*/

public java.sql.ResultSet getResult (){

return result;

}

/**

* Insert the method's description here.

* Creation date: (10/6/00 4:06:40 PM)

* @return java.sql.Statement

*/

public java.sql.Statement getSqlStmt (){

return sqlStmt;

}

/**

* Insert the method's description here.

* Creation date: (9/29/00 4:55:48 PM)

* @return java.lang.String

*/

public java.lang.String getSqlString (){

return sqlString;

}

/**

* Insert the method's description here.

* Creation date: (7/18/02 5:05:30 PM)

*/

public void rollback (){

try{

if (conn != null && !conn.isClosed ()){

conn.rollback ();

}

} catch (SQLException e){

}

}

/**

* Insert the method's description here.

* Creation date: (7/18/02 5:04:34 PM)

* @param autoCommit boolean

*/

public void setAutoCommit (boolean autoCommit){

String methodName = "setAutoCommit()";

try{

if (conn != null && !conn.isClosed ()){

conn.setAutoCommit (autoCommit);

}

} catch (SQLException e){

System.out.println (className + "." + methodName + " ==> " +

e.getClass ().getName () + ": " + e.getMessage ());

}

}

/**

* Insert the method's description here.

* Creation date: (10/6/00 4:04:06 PM)

* @param newConn java.sql.Connection

*/

public void setConn (java.sql.Connection newConn){

conn = newConn;

}

/**

* Insert the method's description here.

* Creation date: (9/29/00 4:29:45 PM)

* @param newDbDriver java.lang.String

*/

public void setDbDriver (java.lang.String newDbDriver){

dbDriver = newDbDriver;

}

/**

* Insert the method's description here.

* Creation date: (9/29/00 4:29:25 PM)

* @param newDbPassword java.lang.String

*/

public void setDbPassword (java.lang.String newDbPassword){

dbPassword = newDbPassword;

}

/**

* Insert the method's description here.

* Creation date: (9/29/00 4:34:01 PM)

* @param newDbURL java.lang.String

*/

public void setDbURL (java.lang.String newDbURL){

dbURL = newDbURL;

}

/**

* Insert the method's description here.

* Creation date: (9/29/00 4:29:04 PM)

* @param newDbUserID java.lang.String

*/

public void setDbUserID (java.lang.String newDbUserID){

dbUserID = newDbUserID;

}

/**

* Insert the method's description here.

* Creation date: (7/17/2002 12:17:51 PM)

* @param newDrcName java.lang.String

*/

public void setDrcName (java.lang.String newDrcName){

drcName = newDrcName;

}

/**

* Insert the method's description here.

* Creation date: (10/8/02 10:07:36 AM)

* @param newInitialContext java.lang.String

*/

public void setInitialContext (java.lang.String newInitialContext){

initialContext = newInitialContext;

}

/**

* Insert the method's description here.

* Creation date: (10/2/00 1:35:04 PM)

* @param newMeta java.sql.ResultSetMetaData

*/

public void setMeta (java.sql.ResultSetMetaData newMeta){

meta = newMeta;

}

/**

* Insert the method's description here.

* Creation date: (9/29/00 5:05:13 PM)

* @param newResult java.sql.ResultSet

*/

void setResult (java.sql.ResultSet newResult){

result = newResult;

}

/**

* Insert the method's description here.

* Creation date: (10/6/00 4:06:40 PM)

* @param newSqlStmt java.sql.Statement

*/

public void setSqlStmt (java.sql.Statement newSqlStmt){

sqlStmt = newSqlStmt;

}

/**

* Insert the method's description here.

* Creation date: (9/29/00 4:55:48 PM)

* @param newSqlString java.lang.String

*/

private void setSqlString (java.lang.String newSqlString){

sqlString = newSqlString;

}

public DataSource setupDataSource (){

BasicDataSource ds = new BasicDataSource ();

ds.setDriverClassName (dbDriver);

ds.setUsername (dbUserID);

ds.setPassword (dbPassword);

ds.setUrl (dbURL);

return ds;

}

public boolean registerDriver (String driverName){

return DbUtils.loadDriver (driverName);

}

public static void closeResultSetAndStatement (java.sql.ResultSet rs){

String methodName = "closeResultSetAndStatement(java.sql.ResultSet)";

java.sql.Statement stmt = null;

try{

stmt = rs.getStatement ();

} catch (java.sql.SQLException e){

System.err.println ("com.netstarmacau.sampleorder.util.DbConnection.closeResultSetAndStatement(Resultset) ==> " +

e.getMessage ());

} finally{

closeResultSetAndStatement (stmt, rs);

}

}

public static void closeResultSetAndStatement (java.sql.Statement stmt){

String methodName = "closeResultSetAndStatement(java.sql.Statement)";

java.sql.ResultSet rs = null;

try{

rs = stmt.getResultSet ();

} catch (java.sql.SQLException e){

System.err.println ("com.netstarmacau.sampleorder.util.DbConnection.closeResultSetAndStatement(Resultset) ==> " +

e.getMessage ());

} finally{

closeResultSetAndStatement (stmt, rs);

}

}

public static void closeResultSetAndStatement (java.sql.Statement stmt,

java.sql.ResultSet rs){

String methodName =

"closeResultSetAndStatement(java.sql.Statement, ava.sql.ResultSet)";

closeResultSet (rs);

closeStatement (stmt);

}

public static void closeStatement (java.sql.Statement stmt){

DbUtils.closeQuietly (stmt);

}

public static void closeResultSet (java.sql.ResultSet rs){

DbUtils.closeQuietly (rs);

}

public static void closeConnection (Connection conn){

DbUtils.closeQuietly (conn);

}

public static void closeAll (Connection conn, Statement stmt, ResultSet rs){

DbUtils.closeQuietly (conn, stmt, rs);

}

public static void closeAll (DBConnection dbconn, Statement stmt,

ResultSet rs){

if (dbconn != null){

dbconn.close ();

}

closeResultSetAndStatement (stmt, rs);

}

public static void commit (Connection conn){

String methodName = "commit()";

try{

if (conn != null && !conn.isClosed ()){

conn.commit ();

}

} catch (SQLException e){

System.out.println (className + "." + methodName + " ==> " +

e.getClass ().getName () + ": " + e.getMessage ());

}

}

public static void setAutoCommit (Connection conn, boolean autoCommit){

String methodName = "setAutoCommit()";

try{

if (conn != null && !conn.isClosed ()){

conn.setAutoCommit (autoCommit);

}

} catch (SQLException e){

System.out.println (className + "." + methodName + " ==> " +

e.getClass ().getName () + ": " + e.getMessage ());

}

}

public static void rollback (Connection conn){

String methodName = "rollback()";

try{

if (conn != null && !conn.isClosed ()){

conn.rollback ();

}

} catch (SQLException e){

System.out.println (className + "." + methodName + " ==> " +

e.getClass ().getName () + ": " + e.getMessage ());

}

}

public static void closePrePareStatement (java.sql.PreparedStatement pstmt){

DbUtils.closeQuietly (pstmt);

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有