在 java 中关闭 MySQL 个数据库连接

Closing MySQL Database Connections in java

我的软件在使用几分钟后变慢了。我发现问题可能是因为我有很多我没有关闭的连接。我有一个 java class 与我的数据库连接,我从中调用连接函数到我需要执行查询的软件的各个部分。我不知道应该在什么时候关闭我的连接,因为如果我在每次查询后尝试关闭,我将无法重做查询,因为我收到错误 'Query cant be done after database connection is closed'。当我将它放入我的数据库连接 class 时,登录后立即无法执行任何查询。我应该在什么时候关闭数据库功能? 以下是我的连接class.

public class databaseConnection {
   public static Connection connection(){
       Connection con = null;
       try{
           //test server
          //con = DriverManager.getConnection("jdbc:mysql://194.5.156.94:3306/u843360242_tukule?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","user","password");
          //real server 
        con = DriverManager.getConnection("jdbc:mysql://254gamers2.softether.net:3306/tukule_kwanza?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","user","password");
       //localhost server 
         //con = DriverManager.getConnection("jdbc:mysql://localhost:3306/tukule?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","password");
       }catch (SQLException ex) {
            try{
           //test server
          //con = DriverManager.getConnection("jdbc:mysql://194.5.156.94:3306/u843360242_tukule?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","user","password");
          //real server 
          con = DriverManager.getConnection("jdbc:mysql://kwanzatukule.ddns.net:3306/tukule_kwanza?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","user","password");
       }catch (SQLException x) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
       return con;
   } 
}

我需要软件各部分的查询如下。

public ArrayList categoriesQuery() {
       String query2 = "SELECT * FROM category";
        ArrayList categories = new ArrayList();
    try {
            pst = connect.prepareStatement(query2);
            rs=pst.executeQuery();
            while(rs.next()){
                Object o[]={rs.getInt("id"),
                            rs.getString("Category_Name")
                            };
                categories.add(o);         
            }
          } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    return categories;
    }

我应该在什么时候放置我的数据库连接函数。

关闭连接的代码应该写在finally块中,这样如果在执行数据库操作时出现错误,那么连接也会被关闭。您可以按如下方式进行:

public ArrayList categoriesQuery() {
       //your code
       Connection connect=null;
    try {
            connect = databaseConnection.connection();
            if(connect!=null){   
                //your code for database operations      
            }
          } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            if(connect!=null){
                connect.close();
            }
        }
    return categories;
}