微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – DAO对象中的关闭连接

参见英文答案 > Is it safe to use a static java.sql.Connection instance in a multithreaded system?                                    2个
我的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;
    }


}

我想关闭连接.我有几个servlet调用insertRecord和getAge方法.什么是关闭我的连接的最佳方法?创建另一个方法并从insertRecord和getAge方法或构造函数调用

解决方法:

在考虑连接之前,每次使用它们时也应始终关闭ResultSet和Statement对象.不要将它们保存为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   
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐