做程序离不开连接数据库,所以一些打开,关闭数据库是经常要执行的操作,打开数据库后,在程序用完后要及时关闭数据库连接资源,以释放内存,避免资源耗尽.但现在有一个问题,即当我们关闭了Connection对象后,Statement,ResultSet对象是否会自动关闭问题,对于这个问题,之前我在网上也找了相关资料,说会自动关闭,所以一段时间以来,我都是只关闭Connection对象,而没有关闭Statement,ResultSet对象,但程序也能正常运行,程序也没有因为资源耗尽而崩溃,对于这一点,其实是有原因的:
1)首先,关闭了Connection对象后,是不会自动关闭Statement,ResultSet对象的:
try {
Connection con = null;
Statement st = null;
ResultSet rs = null;
con = getConnection();
st = con.createStatement();
rs = st.executeQuery(sql);
}
catch(Exception e) {
System.out.println("ocurr error");
}
finally {
con.close();con=null;
try {
con.close();
}
catch(SQLException se) {
System.out.println("ocurr close error");
}
}
System.out.println("statement object:"+st);
System.out.println("resultset object:"+rs);
上面的代码先获取了连接,然后只关闭了Connection对象,而没有关闭Statement,ResultSet对象,最后两行代码输出Statement,ResultSet对象,是有结果的,表明关闭了Connection对象,而没有关闭Statement,ResultSet对象.
2)Statement对象将由Java垃圾收集程序自动关闭,而作为一种好的编程风格,应在不需要Statement对象时显式地关闭它们,这将立即释放DBMS资源,有助于避免潜在的内存问题.
3)ResultSet维护指向其当前数据行的光标.每调用一次next方法,光标向下移动一行.最初它位于第一行之前,因此第一次调用next将把光标置于第一行上,使它成为当前行.随着每次调用next导致光标向下移动一行.按照从上至下的次序获取ResultSet行,在ResultSet对象或其父辈Statement对象关闭之前,光标一直保持有效.
所以在打开数据库资源后,尽量手工关闭Connection对象和Statement,ResultSet对象,要养成一种良好的编程风格.
注:他们三者之间关闭没有任何关联,即先关闭谁没有任何先后顺序,可以先关闭他们中的任何一个,且关闭其中的任何一个对象都不会关闭其他其他对象,但一般养成按关闭ResultSet,Statement,Connection的顺序关闭资源.
分享到:
相关推荐
NULL 博文链接:https://rickgong.iteye.com/blog/2354177
描述Connection、Statement、ResultSet接口的作用
public static void close(ResultSet resultSet, Statement statement, Connection connection){ if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } }...
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();...
* Close a jdbc resource, such as ResultSet, Statement, Connection.... All * these objects must have a method signature is void close(). * * @param resource - * jdbc resouce to close */ public ...
这个问题其实也很简单,因为PreparedStatement接口必须通过Connection接口来获取实例,而ResultSet接口又必须从Statement或者PreparedStatement接口来获取实例,有了这样的级联关系,问题也就迎刃而解了。...
但如果对数据库资源的没有很好管理(如:没有及时回收数据库的游标(ResultSet)、Statement、连接 (Connection)等资源),会直接导致系统的稳定性能。这类不稳定因素,不单单由数据库或者系统本身一方引起,所以在开发...
JDBC连接所有数据库步骤 1 将数据库的JDBC驱动加载到classpath中,在基于JAVAEE的WEB应用实际开发过程中,通常要把目标...7 依次将ResultSet、Statement、PreparedStatement、Connection对象关闭,释放所占用的资源。
Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(sql); if (rs.next()) { name = rs.getString(1); userid = rs.getInt(2); return true; } return false; } //添加...
Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(sql); if (rs.next()) { name = rs.getString(1); userid = rs.getInt(2); return true; } return false; } //添加...
Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(sql); if(rs.next()){ System.out。println("该学号已经存在,请更换"); return; } } catch (SQLException ...
this.resultSet = this.statement.executeQuery(sql); if (this.resultSet.next()) { row = this.resultSet.getInt("count"); } if (row == 0) { return false; } } catch (Exception e) { e....
public static void ReleaseSoucre(ResultSet rs, Statement stmt, Connection conn) { try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch ...
一.jdbc访问数据库步骤 1.载入驱动 将jar包加入工程,Class.forname(""); 2.建立连接 使用connection对象的getConnection方法 url(jdbc:oracle:thin:@...6.关闭连接 按照 ResultSet ,Statement,Connection顺序来关闭
目前比较广泛使用的分页方式是将查询结果缓存在HttpSession或有状态bean中,翻页的时候从缓存中取出一页...因为ResultSet在Statement或Connection关闭时也会被关闭,如果要使ResultSet有效势必长时间占用数据库连接。
booleanisClosed()throwsSQLException 查询此ResultSet对象是否已经被关闭 StatementgctStatcmcnt()throwsSQLException 获取生成此ResultSet对象的Statement对象 (4)数据库的查询和更新操作 Java数据库应用程序中...
一:工作量比较大,需要连接,然后处理jdbc底层事务,处理数据类型,还需要操作Connection,Statement对象和ResultSet对象去拿数据并关闭他们。 二:我们对jdbc编程可能产生的异常进行捕捉处理并正确关闭资源 ...
目前比较广泛使用的分页方式是将查询结果缓存在HttpSession或有状态bean中,翻页的时候从缓存中取出一页...因为ResultSet在Statement或Connection关闭时也会被关闭,如果要使ResultSet有效势必长时间占用数据库连接。
目录 简介什么是JDBC JDBC是一套连接和操作数据库的标准,规范。通过提供DriverManager , Connection , Statement , ResultSet等接口将开发人员与数据库...另外, ResultSet和Statement的关闭都不会导致Connection的