据说在使用后关闭所有JDBC资源是一个好习惯.但是,如果我有以下代码,是否有必要关闭Resultset和Statement?
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = // Retrieve connection
stmt = conn.prepareStatement(// Some SQL);
rs = stmt.executeQuery();
} catch(Exception e) {
// Error Handling
} finally {
try { if (rs != null) rs.close(); } catch (Exception e) {};
try { if (stmt != null) stmt.close(); } catch (Exception e) {};
try { if (conn != null) conn.close(); } catch (Exception e) {};
}
Run Code Online (Sandbox Code Playgroud)
问题是连接的关闭是否完成了工作,或者是否使用了一些资源.
我有点困惑,我正在阅读以下http://en.wikipedia.org/wiki/Java_Database_Connectivity
Connection conn = DriverManager.getConnection(
"jdbc:somejdbcvendor:other data needed by some jdbc vendor",
"myLogin",
"myPassword" );
Statement stmt = conn.createStatement();
try {
stmt.executeUpdate( "INSERT INTO MyTable( name ) VALUES ( 'my name' ) " );
} finally {
//It's important to close the statement when you are done with it
stmt.close();
}
Run Code Online (Sandbox Code Playgroud)
你不需要关闭连接吗?如果没有发生conn.close(),会发生什么?
我有一个私人网络应用程序,我正在维护,目前没有关闭任何一种形式,但重要的是真正的stmt one,conn one或者两者兼而有之?
该网站间歇性地停止,但服务器一直说这是一个数据库连接问题,我怀疑它没有关闭,但我不知道哪个关闭.
我有一个看起来像这样的ArrayList:
[
1 2011-05-10 1 22.0,
2 2011-05-10 2 5555.0,
3 2011-05-11 3 123.0,
4 2011-05-11 2 212.0,
5 2011-05-30 1 3000.0,
6 2011-05-30 1 30.0,
7 2011-06-06 1 307.0,
8 2011-06-06 1 307.0,
9 2011-06-06 1 307.0,
10 2011-06-08 2 3070.0,
11 2011-06-03 2 356.0,
12 2011-05-10 2 100.0,
13 2011-05-30 1 3500.0,
14 2011-05-10 3 1000.0,
15 2011-05-10 3 1000.0,
16 2011-05-07 1 5000.0,
17 2011-05-07 4 500.0,
18 2011-08-07 3 1500.0,
19 2011-08-08 6 11500.0,
20 2011-08-08 …Run Code Online (Sandbox Code Playgroud)