en Lopes
en Lopes

Reputation: 2153

Creating JDBC Application

I have this piece of code:

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();
        }

but I have this error:

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)

Upvotes: 0

Views: 96

Answers (3)

Naruto
Naruto

Reputation: 4329

Instead of manually closing connection its better to use benefit of jdk 7 to automatically close resources after try. Link for reference for 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));

}

Even if you don't want to use benefit of jdk 7, its better to close resolurces in finally block so that if there is any exception, it would still be closed correctly.

    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) {  }
   }

 }

Upvotes: 1

soumya-kole
soumya-kole

Reputation: 1375

It should be something like :

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

You are missing {}

Upvotes: 1

Lukas Eder
Lukas Eder

Reputation: 221370

Since you commented out your print statement, your loop is now closing the connection (and all of its dependent resources). Your code, without the commented out line:

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

Upvotes: 1

Related Questions