求一正确的操作连接池的类 哪位大侠能提供一个比较好的操作连接池的类?考虑到资源释放等因素。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 瞎写的,不知道对不对。DataSource ds = null;Connection conn = null;Statement stat = null;ResultSet rst = null;try { DataSource ds = ctx.lookup("name"); conn = ds.getConnection(); //这里操作statement rst.close(); statement.close(); rst.close(); } catch (SQLException e) { try { rst.close(); rst = null; } finally { if (null != rst) { rst.close(); } } try { stat.close(); stat = null; } finally { if (null != stat) { stat.close(); } } try { conn.close(); conn = null; } finally { if (null != rst) { conn.close(); } }}接分 import java.sql.*;import javax.naming.*;import javax.sql.*;public class CreateDBO { private static InitialContext initialcontext = null; private static DataSource datasource = null; public CreateDBO() { } public static Connection getCenterConnection(boolean autocommit) { Connection connection = null; try { if (initialcontext == null) initialcontext = new InitialContext(); if (datasource == null) { /** * 相关参数在.xml文件里面设置 */ datasource = (DataSource) initialcontext .lookup("java:/datasource"); if (datasource == null) datasource = (DataSource) initialcontext .lookup("java:comp/env/jdbc/datacenter"); } connection = datasource.getConnection(); connection.setAutoCommit(false); } catch (Exception e) { e.printStackTrace(); } return connection; } public static Connection getCenterConnection() { Connection connection = getCenterConnection(false); return connection; } public static PreparedStatement getPreparedStatement(Connection conn, String StatementString) { PreparedStatement preparedstatement = null; try { preparedstatement = conn.prepareStatement(StatementString); } catch (Exception e) { e.printStackTrace(); } return preparedstatement; } public static Statement getStatement(Connection conn) { Statement statement = null; try { statement = conn.createStatement(); } catch (Exception e) { e.printStackTrace(); } return statement; } public void Commit(Connection conn) { try { conn.commit(); } catch (Exception e) { e.printStackTrace(); } } public void Rollback(Connection conn) { try { conn.rollback(); } catch (Exception e) { e.printStackTrace(); } } public void CloseConnection(Connection connection, boolean isSuccess) { try { // Rollback or commit the transaction try { if (isSuccess == false) { connection.rollback(); } else { if (connection != null) { if (!connection.getAutoCommit()) { try { connection.commit(); } catch (Exception e) { connection.rollback(); } } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (connection != null && !connection.isClosed()) { connection.close(); connection = null; } } catch (Exception e) { e.printStackTrace(); } } } public void CloseConnection(Connection connection) { try { // commit the transaction try { if (connection != null) { if (!connection.getAutoCommit()) { try { connection.commit(); } catch (Exception e) { connection.rollback(); } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (connection != null && !connection.isClosed()) { connection.close(); connection = null; } } catch (Exception e) { e.printStackTrace(); } } } public void CloseStatement(Statement stmt) { try { if (stmt != null) { stmt.close(); stmt = null; } } catch (Exception e) { e.printStackTrace(); } } public void CloseResultSet(ResultSet rst) { try { if (rst != null) { rst.close(); rst = null; } } catch (Exception e) { e.printStackTrace(); } }} WEB Service传递附件如何做啊(axis)? Hibernate连接数据库问题 jsp中submit问题(急!在线等待……) 请问一个正则表达式的写法 怎么才能得到ie能识别的空格? 工资涨了四百,散分 請教高手,如何配置tomcat 5.5.6連接mssql 2000 请问这么写怎么出错啊?为什么啊 高分求救:谁用过JSP处理ORACLE数据库中的LONG RAW类型字段 javaweb如何实现图片上传,数据库只存储路径,服务器存储图片 高手救命呀!!!!!!!!!!!!!!!!!!!!! 关于网上租房系统的开发
DataSource ds = null;
Connection conn = null;
Statement stat = null;
ResultSet rst = null;
try {
DataSource ds = ctx.lookup("name");
conn = ds.getConnection();
//这里操作statement
rst.close();
statement.close();
rst.close();
} catch (SQLException e) {
try {
rst.close();
rst = null;
} finally {
if (null != rst) {
rst.close();
}
}
try {
stat.close();
stat = null;
} finally {
if (null != stat) {
stat.close();
}
}
try {
conn.close();
conn = null;
} finally {
if (null != rst) {
conn.close();
}
}
}接分
import java.sql.*;import javax.naming.*;
import javax.sql.*;public class CreateDBO { private static InitialContext initialcontext = null; private static DataSource datasource = null; public CreateDBO() {
} public static Connection getCenterConnection(boolean autocommit) {
Connection connection = null;
try {
if (initialcontext == null)
initialcontext = new InitialContext();
if (datasource == null) { /**
* 相关参数在.xml文件里面设置
*/
datasource = (DataSource) initialcontext
.lookup("java:/datasource");
if (datasource == null)
datasource = (DataSource) initialcontext
.lookup("java:comp/env/jdbc/datacenter");
}
connection = datasource.getConnection();
connection.setAutoCommit(false); } catch (Exception e) {
e.printStackTrace();
}
return connection;
} public static Connection getCenterConnection() {
Connection connection = getCenterConnection(false);
return connection;
} public static PreparedStatement getPreparedStatement(Connection conn,
String StatementString) {
PreparedStatement preparedstatement = null;
try {
preparedstatement = conn.prepareStatement(StatementString);
} catch (Exception e) {
e.printStackTrace();
}
return preparedstatement;
} public static Statement getStatement(Connection conn) {
Statement statement = null;
try {
statement = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
return statement;
} public void Commit(Connection conn) {
try {
conn.commit();
} catch (Exception e) {
e.printStackTrace();
}
} public void Rollback(Connection conn) {
try {
conn.rollback();
} catch (Exception e) {
e.printStackTrace();
}
} public void CloseConnection(Connection connection, boolean isSuccess) {
try {
// Rollback or commit the transaction try {
if (isSuccess == false) {
connection.rollback();
} else {
if (connection != null) {
if (!connection.getAutoCommit()) {
try {
connection.commit();
} catch (Exception e) {
connection.rollback();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} } catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
connection = null;
}
} catch (Exception e) {
e.printStackTrace();
}
} } public void CloseConnection(Connection connection) {
try {
// commit the transaction try { if (connection != null) {
if (!connection.getAutoCommit()) {
try {
connection.commit();
} catch (Exception e) {
connection.rollback();
}
}
} } catch (Exception e) {
e.printStackTrace();
} } catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
connection = null;
}
} catch (Exception e) {
e.printStackTrace();
}
} } public void CloseStatement(Statement stmt) {
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch (Exception e) {
e.printStackTrace();
}
} public void CloseResultSet(ResultSet rst) {
try {
if (rst != null) {
rst.close();
rst = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}}