以前用JDBC处理多个Statement的时候,总会因为异常而无法完美的解决Statement正常关闭,今天无意看到老外写了以下一段代码,和大家分享一下:
Java代码
private PreparedStatement psStmt1;
private PreparedStatement psStmt2;
private PreparedStatement psStmt3;
........................
........................
/* 关闭代码 */
public void cleanup() throws SQLException {
SQLException exception = null;
if (psStmt1 != null) {
try {
psStmt1.close();
} catch (SQLException e) {
exception = e;
} finally {
psStmt1 = null;
}
}
if (psStmt2 != null) {
try {
psStmt2.close();
} catch (SQLException e) {
if (exception != null) e.setNextException(exception);
exception = e;
} finally {
psStmt2 = null;
}
}
if (psStmt3 != null) {
try {
psStmt3.close();
} catch (SQLException e) {
if (exception != null) e.setNextException(exception);
exception = e;
} finally {
psStmt3 = null;
}
}
if (exception != null) {
throw exception;
}
}
private PreparedStatement psStmt1;
private PreparedStatement psStmt2;
private PreparedStatement psStmt3;
........................
........................
/* 关闭代码 */
public void cleanup() throws SQLException {
SQLException exception = null;
if (psStmt1 != null) {
try {
psStmt1.close();
} catch (SQLException e) {
exception = e;
} finally {
psStmt1 = null;
}
}
if (psStmt2 != null) {
try {
psStmt2.close();
} catch (SQLException e) {
if (exception != null) e.setNextException(exception);
exception = e;
} finally {
psStmt2 = null;
}
}
if (psStmt3 != null) {
try {
psStmt3.close();
} catch (SQLException e) {
if (exception != null) e.setNextException(exception);
exception = e;
} finally {
psStmt3 = null;
}
}
if (exception != null) {
throw exception;
}
}
分享到:
相关推荐
NULL 博文链接:https://rickgong.iteye.com/blog/2354177
加载JDBC驱动程序,提供JDBC连接的URL ,创建数据库的连接,创建一个Statement ,执行SQL语句 ,处理结果,关闭JDBC对象
星期五 15:00SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure ...
1. 开启和关闭自动提交 connection.setAutoCommit(false/true); 2. 提交事务 connection.commit(); 3. 回滚 connection.rollback(); - 实现转账: 超人 500 蝙蝠侠 5000 蝙蝠侠给超人转2000 执行第一次成功 ...
使用JDBC连接Oracle 1.重新下载、指定驱动程序 2.重新指定驱动名称 3.修改URL,重新指定数据库连接信息 4.创建对象(不变) 5.关闭连接(不变)
什么时候用statement,什么时候用preparedstatement
7.关闭连接 `ResultSet 、Statement 、Connection` 3.SQL注入问题与解决(Statement 、PreparedStatement) 3.1.模拟SQL注入 3.2.PreparedStatement解决 3.3.Statement 与 PreparedStatement 4.完整源码
public static void close(ResultSet resultSet, Statement statement, Connection connection) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace();...
// MySql 数据库要求必需关闭 statement 对象, 否则释放不掉资源 // - 此观点错误, 因为关闭此对象后有时数据无法读出 //statement.close(); return rs; } catch (Exception ex) { System.out.println("Error ...
Hibernate用于读取并解析配置文件,读取并解析映射信息,工作原理:创建SessionFactory;打开Sesssion;创建事务Transation;持久化操作;提交事务;关闭Session;关闭SesstionFactory
一.jdbc访问数据库步骤 1.载入驱动 将jar包加入工程,Class.forname(""); 2.建立连接 使用connection对象的getConnection方法 url(jdbc:oracle:thin:@...6.关闭连接 按照 ResultSet ,Statement,Connection顺序来关闭
JDBC访问数据库记录可以采用下面的几个步骤: 1、 通过驱动器管理器获取连接接口。 2、 获得Statement或它的子类。 3、 限制Statement中的参数...6、 关闭Statement。 7、 处理其它的Statement 8、 关闭连接接口。
使用JDBC-ODBC桥驱动程序连接数据库 ...o(6)关闭连接,释放资源 2.下面进行代码演示 1.注册数据库驱动程序的语法格式如下: DriverManager.registerDriver(Driver driver) 或者 Class.forName("DriverName");
JDBC连接所有数据库步骤 1 将数据库的JDBC驱动加载到classpath中,在基于JAVAEE的WEB应用实际开发过程中,通常要把目标...7 依次将ResultSet、Statement、PreparedStatement、Connection对象关闭,释放所占用的资源。
一:工作量比较大,需要连接,然后处理jdbc底层事务,处理数据类型,还需要操作Connection,Statement对象和ResultSet对象去拿数据并关闭他们。 二:我们对jdbc编程可能产生的异常进行捕捉处理并正确关闭资源 ...
如果不是在客户端创建、预备、执行PreparedStatement任务需要的时间长于Statement任务,我会建议在除动态SQL命令之外的所有情况下使用PreparedStatement对象。 6、在成批处理重复的插入或更新操作中使用...
该类整理了连接数据库的常用方法,包括打开连接,关闭连接,关闭Statement,关闭resultset以及数据库配置文件
获取Statement对象,通过Statement对象执行SQL语句: Statement stmt=con.createStatement(); 执行SQL查询,返回给结果集对象: ResultSet rs=stmt. executeQuery(“select * from 表名”); 或 表名”+条件); ...
JDBC应用步骤 1,注册加载一个driver驱动... 2,创建数据库连接(Connection) 3,创建一个Statement(发送sql) 4,执行sql语句 5,处理sql结果(select语句) 6,关闭Statement 7,关闭连接Connection。
JdbcTemplate基本使用 JDBC已经能够满足大部分用户最基本的需求,但是在使用JDBC时,必须自己来管理数据库资源如:获取...他运行核心的JDBC工作流,如Statement的建立和执行,而我们只需要提供SQL语句和提取结果。