用Java关闭数据库连接我有点糊涂了,我正在读下面的文章http:/en.wikipara.org/wiki/java_Database_ConnectivityConnection 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();}不需要关闭Conn连接吗?如果连接()没有发生,到底会发生什么呢?我有一个私人网络应用程序,我认为它目前没有关闭任何一种形式,但重要的是真正的一个,康涅狄格,还是两者兼而有之?该网站一直断断续续地关闭,但服务器一直说这是一个数据库连接问题,我怀疑它没有被关闭,但我不知道谁关闭,如果有的话。
3 回答
米琪卡哇伊
TA贡献1998条经验 获得超6个赞
Connectionclose()
ResultSet, StatementConnectionfinally
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
} finally {
try { rs.close(); } catch (Exception e) { /* ignored */ }
try { ps.close(); } catch (Exception e) { /* ignored */ }
try { conn.close(); } catch (Exception e) { /* ignored */ }}finally
} finally {
DbUtils.closeQuietly(rs);
DbUtils.closeQuietly(ps);
DbUtils.closeQuietly(conn);}DbUtils
千万里不及你
TA贡献1784条经验 获得超9个赞
StatementConnectionResultSet
java.sql.ResultSet:
当语句对象关闭、重新执行或用于从多个结果序列检索下一个结果时,生成该语句对象的语句对象将自动关闭ResultSet对象。
添加回答
举报
0/150
提交
取消
