0

This code is showing this error message when the button is clicked:

Operation not allowed after resultset closed

Code:

case 4:
                BufferedReader choosest=new BufferedReader(new InputStreamReader(System.in));
                System.out.print("enter your id");
                int idst=Integer.parseInt(choosest.readLine());
                String SQL = "SELECT * FROM student";
                ResultSet ps = stmt.executeQuery(SQL);
                while (ps.next()) {
                    int iddb = ps.getInt("id");
                if(idst==iddb) {
                    BufferedReader lessonuser = new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("choose lesson ");
                    String lest =lessonuser .readLine();
                    String SQL1 = "SELECT * FROM lesson";
                    ResultSet qs = stmt.executeQuery(SQL1);
                    while (qs.next()) {
                        String ledb = ps.getString("namel");
                        if (lest == ledb) {
                            String insertTableSQL1 ="INSERT INTO chooselesson (id, lesson)  VALUES ("+idst+",'"+lest+"')";
                            stmt.executeUpdate(insertTableSQL1);
                            System.out.print("your unit sucssesfully add ");

                        }
                        }

                    }
                    }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
reihane
  • 39
  • 7

2 Answers2

0

ResultSet is closed when you make another query. For this code, you could first collect all data of ps. And after that iterate the result to perform other querys.

And I have to say making query in loops is bad performance operation. There are lots ways to avoid this. For example you could fetch all student records as well as all lessons records, each in one query. Then use lessonuser to find out what records you should insert into chooselesson table, and run the insert SQL in batch mode.

John
  • 1,654
  • 1
  • 14
  • 21
0

The problem is at the line ResultSet qs = stmt.executeQuery(SQL1); .Using statement to execute the other query closes the previous ResultSet. Modifying the above code to might work, I didn't test the solution but it should generally work.

BufferedReader choosest=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("enter your id");
            int idst=Integer.parseInt(choosest.readLine());
            String SQL = "SELECT * FROM student";
            ResultSet ps = stmt.executeQuery(SQL);
            while (ps.next()) {
                int iddb = ps.getInt("id");
            if(idst==iddb) {
                BufferedReader lessonuser = new BufferedReader(new InputStreamReader(System.in));
                System.out.print("choose lesson ");
                String lest =lessonuser .readLine();
                String SQL1 = "SELECT * FROM lesson";
                ResultSet qs = stmtSelect.executeQuery(SQL1);
                while (qs.next()) {
                    String ledb = ps.getString("namel");
                    if (lest == ledb) {
                        String insertTableSQL1 ="INSERT INTO chooselesson (id, lesson)  VALUES ("+idst+",'"+lest+"')";
                        stmtInsert.executeUpdate(insertTableSQL1);
                        System.out.print("your unit sucssesfully add ");

                    }
                    }

                }
                }

PreparedStatement is better choice for performance, https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

YAP
  • 66
  • 2
  • 3