`
qjbagu
  • 浏览: 22906 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
  • wjl1174: String 不是基本的类型,所有他的变量就是一个对象,放在m ...
    静态字段问题
  • laojiang: 数据库设置缺省值或者类修改成Integer原因null是没办法 ...
    JDBC ORM
  • yin_bp: 补充说明一下连接池的配置     bboss persiste ...
    JDBC 批处理
  • yin_bp: 再贴点稍微有点点技术含量的,呵呵 看看开源j2ee 框架bbo ...
    JDBC 批处理
  • yin_bp: 贴点稍微有点点技术含量的,呵呵 有空看看开源bbossgrou ...
    JDBC 批处理

Statement关闭(转)

阅读更多
以前用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;
  }
} 


分享到:
评论

相关推荐

    怎样实现关闭connection时自动关闭Statement和ResultSet (下篇)

    NULL 博文链接:https://rickgong.iteye.com/blog/2354177

    MySQL.rar_JDBC程序_statement_完整java开发中JDBC连接数据库代码

    加载JDBC驱动程序,提供JDBC连接的URL ,创建数据库的连接,创建一个Statement ,执行SQL语句 ,处理结果,关闭JDBC对象

    SQL Server 阻止了对组件 'Ad Hoc Distributed Queries'

    星期五 15:00SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure ...

    MySQL数据库

    1. 开启和关闭自动提交 connection.setAutoCommit(false/true); 2. 提交事务 connection.commit(); 3. 回滚 connection.rollback(); - 实现转账: 超人 500 蝙蝠侠 5000 蝙蝠侠给超人转2000 执行第一次成功 ...

    Jdbc_oracle_statement.rar_4 3 2 1

    使用JDBC连接Oracle 1.重新下载、指定驱动程序 2.重新指定驱动名称 3.修改URL,重新指定数据库连接信息 4.创建对象(不变) 5.关闭连接(不变)

    connection 和 preparedstatement 的关闭问题

    什么时候用statement,什么时候用preparedstatement

    Java-JDBC【源码】JDBC概述、获取连接、SQL注入问题与解决、查询解析

    7.关闭连接 `ResultSet 、Statement 、Connection` 3.SQL注入问题与解决(Statement 、PreparedStatement) 3.1.模拟SQL注入 3.2.PreparedStatement解决 3.3.Statement 与 PreparedStatement 4.完整源码

    DruidJDBCUtils.java

    public static void close(ResultSet resultSet, Statement statement, Connection connection) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace();...

    数据库工具类DatabaseUtil.java

    // MySql 数据库要求必需关闭 statement 对象, 否则释放不掉资源 // - 此观点错误, 因为关闭此对象后有时数据无法读出 //statement.close(); return rs; } catch (Exception ex) { System.out.println("Error ...

    Hibernte使用及配置说明

    Hibernate用于读取并解析配置文件,读取并解析映射信息,工作原理:创建SessionFactory;打开Sesssion;创建事务Transation;持久化操作;提交事务;关闭Session;关闭SesstionFactory

    mysql-connector-java-5.1.46-bin

    一.jdbc访问数据库步骤 1.载入驱动 将jar包加入工程,Class.forname(""); 2.建立连接 使用connection对象的getConnection方法   url(jdbc:oracle:thin:@...6.关闭连接 按照 ResultSet ,Statement,Connection顺序来关闭

    java 笔记 JDBC

    JDBC访问数据库记录可以采用下面的几个步骤: 1、 通过驱动器管理器获取连接接口。 2、 获得Statement或它的子类。 3、 限制Statement中的参数...6、 关闭Statement。 7、 处理其它的Statement 8、 关闭连接接口。

    Java使用JDBC连接数据库.docx

    使用JDBC-ODBC桥驱动程序连接数据库 ...o(6)关闭连接,释放资源 2.下面进行代码演示 1.注册数据库驱动程序的语法格式如下: DriverManager.registerDriver(Driver driver) 或者 Class.forName("DriverName");

    JDBC连接所有数据库步骤

    JDBC连接所有数据库步骤 1 将数据库的JDBC驱动加载到classpath中,在基于JAVAEE的WEB应用实际开发过程中,通常要把目标...7 依次将ResultSet、Statement、PreparedStatement、Connection对象关闭,释放所占用的资源。

    删掉,复制别人东西,怕侵权,原本只想收藏的

    一:工作量比较大,需要连接,然后处理jdbc底层事务,处理数据类型,还需要操作Connection,Statement对象和ResultSet对象去拿数据并关闭他们。 二:我们对jdbc编程可能产生的异常进行捕捉处理并正确关闭资源 ...

    jdbc连接数据库的方式2

    如果不是在客户端创建、预备、执行PreparedStatement任务需要的时间长于Statement任务,我会建议在除动态SQL命令之外的所有情况下使用PreparedStatement对象。  6、在成批处理重复的插入或更新操作中使用...

    连接数据库的工具类及其配置文件

    该类整理了连接数据库的常用方法,包括打开连接,关闭连接,关闭Statement,关闭resultset以及数据库配置文件

    JDBC访问数据库的步骤

    获取Statement对象,通过Statement对象执行SQL语句: Statement stmt=con.createStatement(); 执行SQL查询,返回给结果集对象: ResultSet rs=stmt. executeQuery(“select * from 表名”); 或 表名”+条件); ...

    JDBC开发过程与原理

    JDBC应用步骤 1,注册加载一个driver驱动... 2,创建数据库连接(Connection) 3,创建一个Statement(发送sql) 4,执行sql语句 5,处理sql结果(select语句) 6,关闭Statement 7,关闭连接Connection。

    JDBCtemplate封装类的jar包.zip

    JdbcTemplate基本使用 ​ JDBC已经能够满足大部分用户最基本的需求,但是在使用JDBC时,必须自己来管理数据库资源如:获取...他运行核心的JDBC工作流,如Statement的建立和执行,而我们只需要提供SQL语句和提取结果。

Global site tag (gtag.js) - Google Analytics