Skip to content
Advertisement

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)

Advertisement

Answer

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();
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement