【问题标题】:Creating JDBC Application创建 JDBC 应用程序
【发布时间】:2021-01-12 15:38:46
【问题描述】:

我有这段代码:

try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection con= DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8","root","Icdjoil100");
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery("select * from t_user");
            while(rs.next())
                //System.out.println(rs.getInt(0)+"  "+rs.getString(1));
            con.close();
        }catch(Exception e){
            e.printStackTrace();
        }

但我有这个错误:

java.sql.SQLException: Operation not allowed after ResultSet closed
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
    at com.mysql.jdbc.ResultSet.checkClosed(ResultSet.java:666)
    at com.mysql.jdbc.ResultSet.next(ResultSet.java:7274)
    at Test.main(Test.java:19)

【问题讨论】:

  • 为了清楚起见,您应该在 while 语句中使用大括号 {} - 并查看 this tutorial 中关于结果集迭代的示例代码(以及之后的关闭语句)。

标签: java mysql sql database jdbc


【解决方案1】:

由于您注释掉了您的打印语句,您的循环现在正在关闭连接(及其所有相关资源)。您的代码,没有注释掉的行:

while(rs.next())
    con.close();

【讨论】:

    【解决方案2】:

    应该是这样的:

    while(rs.next()){
        System.out.println(rs.getInt(0)+"  "+rs.getString(1));
    }
    con.close();
    

    你错过了{}

    【讨论】:

      【解决方案3】:

      与其手动关闭连接,不如利用 jdk 7 的优势在尝试后自动关闭资源。 Link 供 try-with-resources 参考。

      Class.forName("com.mysql.jdbc.Driver");
      
      
        try (
              Connection con= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8","root","Icdjoil100");
              Statement stmt=con.createStatement();
              ResultSet rs=stmt.executeQuery("select * from t_user")) {
                    while(rs.next())
                  //System.out.println(rs.getInt(0)+"  "+rs.getString(1));
      
      }
      

      即使你不想使用jdk 7的好处,最好在finally块中关闭resolurces,这样如果有任何异常,它仍然会正确关闭。

          Connection con = null;
          Statement stmt= null;
          ResultSet rs = null;
          try{
                  Class.forName("com.mysql.jdbc.Driver");
                  con= DriverManager.getConnection(
                          "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8","root","Icdjoil100");
                  stmt=con.createStatement();
                  rs=stmt.executeQuery("select * from t_user");
                  while(rs.next())
                      System.out.println(rs.getInt(0)+"  "+rs.getString(1));
                  
              }catch(Exception e){
                  e.printStackTrace();
              } finally {
             if (rs != null) {
                try {
                   rs.close();
                 } catch (SQLException e) {  }
               }
            if (ps != null) {
               try {
                   ps.close();
               } catch (SQLException e) {  }
            }
            if (conn != null) {
               try {
                 conn.close();
            } catch (SQLException e) {  }
         }
      
       }
      

      【讨论】:

        猜你喜欢
        • 2021-08-07
        • 2016-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-29
        • 2017-08-16
        • 2020-02-21
        • 1970-01-01
        相关资源
        最近更新 更多