如果PreparedStatement对象是同过DriverManager.getConnection().Preparedstatement(sql)获得的,那最后需要关闭conn吗?理论上讲连接是打开的啊,需要DriverManager.getConnection().close();吗?

解决方案 »

  1.   

    只要执行你就打开启了连接,连接是比较占用资源的,不用的话,必须要要关闭他,而且一定要关闭他。
    你这里不应该这样,应该是Connection conn = DriverManager.getConnection();
    然后 conn.Preparedstatement(sql);
    最后在程序的finally里面关闭连接(conn.close()),一定要在finally里面,因为就算出现其他异常,finally里面的代码也会执行。
      

  2.   


    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();
    }
    }
    }
    }
      

  3.   

    调用完要关闭、先关闭ResultSet  再关闭PreparedStatement 后关闭 DriverManager.getConnection()
      

  4.   

    实力参考:http://blog.csdn.net/hzc543806053/article/details/7395998