关闭 DAO 对象中的连接

Close connection in DAO object

我的 DAO 对象

package com.myselect;

import java.sql.*;
import java.sql.DriverManager;

import java.util.logging.Level;
import java.util.logging.Logger;

public class DataAccess {

    Connection conn;
    PreparedStatement pst;
    ResultSet rs;

    public DataAccess() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/db", "root", "root");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public String getAge(String name) {
        String userage = null;
        try {
            pst = conn.prepareStatement("select age from mydb where name= ?");
            pst.setString(1, name);

            rs = pst.executeQuery();

            while (rs.next()) {
                userage = rs.getString("age");
                System.out.println(userage);
            }

        } catch (Exception ex) {
            Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
        }

        return userage;
    }

    public int insertRecord(String name, int addage) {
        int b = 0;
        try {
            pst = conn.prepareStatement("insert into mydb values(?,?)");
            pst.setString(1, name);
            pst.setInt(2, addage);
            b = pst.executeUpdate();

        } catch (Exception ex) {
            Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
        }

        return b;
    }


}

我想关闭连接。我有几个调用 insertRecordgetAge methods.What 的 servlet 是关闭我的连接的最佳方式吗?创建另一个方法并从 insertRecordgetAge 方法或在构造函数中调用?

应在需要时打开和关闭连接。在你的情况下,你在一个点打开一个连接,然后在另一个点使用它。这意味着您打开了一个连接并且可能有一段时间没有使用它。

您需要做的是使方法自包含,这意味着方法应该打开连接,完成后关闭它。关闭部分应在 finally 块内完成,这将确保无论发生什么情况,集合都将始终关闭。

大概是这样:

public String getAge(String name) {
        String userage = null;
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/db", "root", "root");

            PreparedStatement pst = conn.prepareStatement("select age from mydb where name= ?");
            pst.setString(1, name);

            rs = pst.executeQuery();

            while (rs.next()) {
                userage = rs.getString("age");
                System.out.println(userage);
            }

        } catch (Exception ex) {
            Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally {
            if(conn != null) {
                conn.close();
            }

            if(pst != null) {
                pst.close();
            }

            if(rs != null) {
                rs.close();
            }
        }

        return userage;
    }

编辑:根据@virtualpathum 的建议,您还可以查看第 3 方框架,它们为您提供了管理连接池的方法,通常是通过实施单例编程模式。

在考虑连接之前,您还应该始终关闭ResultSetStatement对象,每次使用它们。不要将它们作为 DataAccess 对象中的状态。将它们作为局部变量并在使用后关闭它们:

public String getAge(String name) {
    PreparedStatement pst = null;
    ResultSet rs = null;
    String userage = null;
    try {
        pst = conn.prepareStatement("select age from mydb where name= ?");
        pst.setString(1, name);

        rs = pst.executeQuery();

        while (rs.next()) {
            userage = rs.getString("age");
            System.out.println(userage);
        }

    } catch (Exception ex) {
        Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
       // closing JDBC resources
       if(rs != null) {
          rs.close();
       }

      if(pst != null) {
          pst.close();
       }

    return userage;
}

连接也一样:理想情况下,每个方法都应该创建一个 Connection 对象并在使用后关闭它。根据您在方法中打开和关闭连接的频率,您可能希望使用连接池来共享打开的连接以提高效率。

要关闭连接及其资源,您可以创建一个方法来执行此操作:

public void closeConnection(Connection connection, Statement statement, ResultSet resultSet) {
     ... // close resources here   
}