Solved

Problem in SQL delete in a while loop

Posted on 2004-08-27
3
192 Views
Last Modified: 2010-03-31
String sql = "SELECT group_code FROM user_group WHERE staff_id= '" + userId + "'";

ResultSet rs = stmt.executeQuery(sqlGrpCode);

while(rs.next()){
      System.out.println("--------START--------");
      String grpCode = (rs.getInt("group_code"))+ "";
      System.out.println("grpCode: " + grpCode);
                System.out.println("sqlDelCode: "+ sqlDelCode);
      System.out.println("---------END-------");
}


/************************OUTPUT***********************/
--------START--------
grpCode: 6
sqlDelCode: DELETE FROM usergrp_recipient WHERE group_code= 6
---------END-------
--------START--------      
grpCode: 8
sqlDelCode: DELETE FROM usergrp_recipient WHERE group_code= 8
---------END-------

/********************END OF OUTPUT******************/




/************************But if I do this: *************************/

String sql = "SELECT group_code FROM user_group WHERE staff_id= '" + userId + "'";

ResultSet rs = stmt.executeQuery(sqlGrpCode);

while(rs.next()){
      System.out.println("--------START--------");
      String grpCode = (rs.getInt("group_code"))+ "";
      System.out.println("grpCode: " + grpCode);
                System.out.println("sqlDelCode: "+ sqlDelCode);
                int res = stmt.executeUpdate(sqlDelCode);
      System.out.println("---------END-------");
}

/************************OUTPUT***********************/

--------START--------
grpCode: 6
sqlDelCode: DELETE FROM usergrp_recipient WHERE group_code= 6
---------END-------

/********************END OF OUTPUT******************/

Why is it that I cannot delete 2 rows? I tried in my database and it works fine....
0
Comment
Question by:mwcmp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
talvio earned 500 total points
ID: 11919563
Hi,

answer to your question straight from the Java API:
"By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists. " (http://java.sun.com/j2se/1.4.2/docs/api/index.html)

I would suggest that you either first collect all IDs used in the update to a list (ListArray for example) and then build a single sql statement: "...WHERE group_code IN (x, y, z ...)" or maybe embed the whole thing to a single SQL update, meaning the first query and then the updates: "...WHERE group_code IN ([insert here you first query])".

It seems to me that when the first result set is large, using many update calls is quite a bit overhead. Of course you might still want to keep updates as they are in you code and fix your problem simply by creating an own instance of Statement for the updates.

Hope this helps,
-jT

0
 
LVL 92

Expert Comment

by:objects
ID: 11919689
try:

while(rs.next()){
     System.out.println("--------START--------");
     String grpCode = (rs.getInt("group_code"))+ "";
     System.out.println("grpCode: " + grpCode);
                System.out.println("sqlDelCode: "+ sqlDelCode);
                Staement ustmt = connection.createStatement();
                int res = ustmt.executeUpdate(sqlDelCode);
                ustmt.close();
     System.out.println("---------END-------");
}
0
 
LVL 1

Expert Comment

by:talvio
ID: 11949455
Thanks for the points,
Glad I could help you!

best regards,
-jT
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question