Solved

Problem in SQL delete in a while loop

Posted on 2004-08-27
3
190 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
  • 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 182
Error in @AspectJ Based AOP with Spring 2 18
Java: anonymous class 4 29
Notify sent to other threads in Java 9 34
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

828 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