使用JDBC过程中如何正确关闭connection
来源:互联网 发布:seo超级外链 编辑:程序博客网 时间:2024/05/22 02:08
来看一段代码:
import java.sql.*;/** * Created by N3verL4nd on 2017/4/17. */public class JdbcDemo{ public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; String url = "jdbc:mysql://localhost:3306/weibo?" + "user=root&password=lgh123&useUnicode=true&characterEncoding=UTF8&useSSL=true"; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { conn = DriverManager.getConnection(url); stmt = conn.createStatement(); /* System.out.println(sql); */ rs = stmt.executeQuery("SELECT * FROM t_account"); while (rs.next()){ System.out.println(rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4)); } rs.close();System.out.println("ResultSet closed"); stmt.close();System.out.println("Statement closed"); conn.close();System.out.println("Connection closed"); } catch (SQLException e) { e.printStackTrace(); } }}
假如rs = stmt.executeQuery("SELECT * FROM s_account");我们查找一个不存在的表,就会出现异常:
而此时,
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;都没有被正确关闭。
正确的处理方式应该是:在finally语句块中执行关闭操作。
Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;try { // Do stuff ...} catch (SQLException ex) { // Exception handling stuff ...} finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { /* ignored */} } if (ps != null) { try { ps.close(); } catch (SQLException e) { /* ignored */} } if (conn != null) { try { conn.close(); } catch (SQLException e) { /* ignored */} }}当然如果可以确保不是空指针,则可以简写为:
} finally { try { rs.close(); } catch (Exception e) { /* ignored */ } try { ps.close(); } catch (Exception e) { /* ignored */ } try { conn.close(); } catch (Exception e) { /* ignored */ }}这对于我们来时还是过于冗长,我们可以使用Apache Commons DbUtils 里的 DbUtils
} finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(ps); DbUtils.closeQuietly(conn);}内部实现:
public static void close(ResultSet rs) throws SQLException { if (rs != null) { rs.close(); }}public static void close(Statement stmt) throws SQLException { if (stmt != null) { stmt.close(); }}public static void close(Connection conn) throws SQLException { if (conn != null) { conn.close(); }}public static void closeQuietly(Connection conn) { try { close(conn); } catch (SQLException e) { //e.printStackTrace(); }}public static void closeQuietly(ResultSet rs) { try { close(rs); } catch (SQLException e) { //e.printStackTrace(); }}public static void closeQuietly(Statement stmt) { try { close(stmt); } catch (SQLException e) { //e.printStackTrace(); }}public static void closeQuietly(Connection conn, Statement stmt, ResultSet rs) { try { closeQuietly(rs); } finally { try { closeQuietly(stmt); } finally { closeQuietly(conn); } }}
参考:
http://stackoverflow.com/questions/2225221/closing-database-connections-in-java
http://www.cnblogs.com/hongten/archive/2011/03/29/1998311.html
http://www.cnblogs.com/jfqiu/p/3197014.html
https://shinesolutions.com/2007/08/04/how-to-close-jdbc-resources-properly-every-time/
0 0
- 使用JDBC过程中如何正确关闭connection
- jdbc connection 关闭
- JDBC- 正确关闭数据库连接
- JDBC中Connection、Statement、ResultSet的关闭方法
- C# 中 使用Connection 打开/关闭数据库
- 在JDBC技术中如何使用存储过程(SQLSERVER数据库
- 如何在存储过程以及JDBC中使用Array
- JDBC中Connection解惑
- JDBC中Connection解惑
- 如何正确关闭linux
- 如何正确关闭MongoDB
- 如何正确关闭MongoDB
- 如何正确关闭树莓派
- 如何正确关闭MongoDB
- JDBC中使用存储过程
- JDBC中使用存储过程
- 关于JDBC中Connection的使用的几个details
- JDBC中关于Connection, PreparedStatement, ResultSet是否关闭的一些思考
- leetcode 485. Max Consecutive Ones(easy)
- benchmarksql在postgresql上的安装、使用
- 一道关于深度搜索入门的网易笔试题
- AS收集
- javascript高级程序设计-读书笔记
- 使用JDBC过程中如何正确关闭connection
- Android 同时设置多个闹钟
- Python-第三方库requests详解
- 数据解读《人民的名义》到底有多火,你看了吗?
- C与Lua的交互
- android studio win10系统中打开Terminal获取sha1的方法
- struts2教程(2)--配置
- XTU1225 仙剑奇侠传
- storm部署Topology出现的错误