在 Java中关闭数据库连接的后续问题
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();
}
conn.close();
我知道 conn.close() 是必要的,但不知道为什么。一旦方法调用结束,垃圾收集器不会释放连接对象(并释放存储在其中的每个指向数据库的处理程序)吗?