简体   繁体   中英

SQL queries through Java, “Illegal operation on empty result set”

I'm making a db call as follows:

String sqlAlert = "SELECT * FROM demotable where demo_no ='"
                                +rsDemo.getString("demo_no") + "'";

ResultSet rsAlert = db.GetSQL(sqlAlert);

        if (rsAlert.next()) { 

            String newAlert = rsAlert.getString("cust3")+"1";

            String newAlertSql = "UPDATE demotable SET cust3 = '" + newAlert + "' where demo_no='" + rsDemo.getString("demo_no") + "'";
            System.out.println("Update alert msg: " + newAlertSql);
            db.RunSQL(newAlertSql);
        }  else {
            System.out.println("empty result. Demo_no = "+rsDemo.getString("demo_no"));
               String sqlAlertinsert = "INSERT INTO demotable VALUES('" + rsDemo.getString("demo_no") + "','','','','','<unotes></unotes>')";
              db.RunSQL(sqlAlertinsert);
              System.out.println("insert demo done");

              String sqlAlert2 = "SELECT * FROM demotable where demo_no ='"rsDemo.getString("demo_no") + "'";
              ResultSet rsAlert2 = db.GetSQL(sqlAlert2);

            if (rsAlert2.next()) {

                String newAlert = rsAlert2.getString("cust3")+"1";

                String newAlertSql = "UPDATE demotable SET cust3 = '" + newAlert+ "' where demo_no='" + rsDemo.getString("demo_no") + "'";
                              System.out.println("Update alert msg: " + newAlertSql);
                         db.RunSQL(newAlertSql);
            }
            rsAlert2.close(); 

        }

rsAlert.close();

rs.close();

I am trying to insert rows into demographiccust if rsAlert returns an empty set and then access values from it. But my code returns this exception "Illegal operation on empty result set" around "if (rsAlert2.next()) { ". Why does it return an empty set even after inserting values into the table? Please help. Thank you.

It may be because of the open cursor. You must close your first Statement , prior trying the second. ResultSet is a connected thing, when you close the Statement it get closed too. I can't see the implementation of your db.RunSQL() and db.GetSQL() methods.

However, I am having the suggestion on how you should do it, in the first place. Here you go,

  1. Update it without querying the database
  2. Check how many rows updated. If none, then step 3, otherwise completed
  3. Insert the record with the correct values in the first place. No need to update it after inserting.

Tips:

  1. Try using PreparedStatement , instead
  2. Try to stick with Java Naming Convention
  3. Try using meaningful names, ie for example your method db.GetSQL() is not returning an SQL, but contrarily asking one, and in fact returning a ResultSet .
  4. Never return a ResultSet . This may lead to bloated code and a lot of open cursors. Don't make the user of your method to close it. Close it yourself in your method where you are performing any database query, and return the result as a bean or a list of beans.

It's just a guess, but because you are interpolating rsDemo.getString("demo_no") directly into the SQL, you may be passing an SQL statement that isn't what you want. Try using the parameter binding api.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM