关于PreparedStatement和Connection关闭的问题 如果PreparedStatement对象是同过DriverManager.getConnection().Preparedstatement(sql)获得的,那最后需要关闭conn吗?理论上讲连接是打开的啊,需要DriverManager.getConnection().close();吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 只要执行你就打开启了连接,连接是比较占用资源的,不用的话,必须要要关闭他,而且一定要关闭他。你这里不应该这样,应该是Connection conn = DriverManager.getConnection();然后 conn.Preparedstatement(sql);最后在程序的finally里面关闭连接(conn.close()),一定要在finally里面,因为就算出现其他异常,finally里面的代码也会执行。 public static void free(ResultSet resultSet,Statement statemen,Connection connection){ try { if(resultSet != null) resultSet.close(); } catch (SQLException e) { e.printStackTrace(); }finally{ if(statemen != null) try { statemen.close(); } catch (SQLException e) { e.printStackTrace(); }finally{ if(connection != null) try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } 调用完要关闭、先关闭ResultSet 再关闭PreparedStatement 后关闭 DriverManager.getConnection() 实力参考:http://blog.csdn.net/hzc543806053/article/details/7395998 struts2二级联动下拉框 编写JAVA 邮件收发 在MYSQL中varchar转换成double进行比较问题 jsp / Servelt 问题,在线等~! 菜鸟开发的一个小型Ioc容器,恳求大家帮忙看看,先谢谢了 JSP调用Java中的类为什么总是调用不了呢? struts按钮事件 TOMCAT配置已经成功,但我如何测试我自己的JSP页面? 修改哪个参数让weblogic不对大小写敏感 有没有用javamail做邮件发送和接受比较完整的例子啊,小弟在这谢过师兄们了! jquery 显示添加input 框疑问 IE9有时不支持document.location.reload();怎么解决
你这里不应该这样,应该是Connection conn = DriverManager.getConnection();
然后 conn.Preparedstatement(sql);
最后在程序的finally里面关闭连接(conn.close()),一定要在finally里面,因为就算出现其他异常,finally里面的代码也会执行。
public static void free(ResultSet resultSet,Statement statemen,Connection connection){
try {
if(resultSet != null)
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(statemen != null)
try {
statemen.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(connection != null)
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}