No operations allowed after connection closed MYSQL

后端 未结 3 1044
长情又很酷
长情又很酷 2021-01-06 16:48

Basically I have:

String query = \"SELECT * FROM table WHERE UNIQUEID=? AND DIR IS NOT NULL AND NAME IS NOT NULL AND PAGETYPE IS NOT NULL\";
DBConnect Databa         


        
3条回答
  •  孤街浪徒
    2021-01-06 17:20

    It's because of two reasons:

    • Connection is closed after the execution of first PreparedStatement
    • getcon uses an instance variable instead of local variable. Due to this, same conn variable (the one that was closed earlier) gets returned when getcon is called next time.

    To fix this, getcon and DBConnect need to be modified to declare a local conn variable and return it (in fact, you don't need DBConnect at all), e.g.:

    public Connection getcon(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String unicode="useSSL=false&autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8";
            return DriverManager.getConnection("jdbc:mysql://localhost:15501/duckdb?"+unicode, "root", "_PWD");
        }catch(Exception ex){
            System.out.println(ex.getMessage());
            System.out.println("couldn't connect!");
            throw new RuntimeException(ex);
        }
    }
    

提交回复
热议问题