Solved

Problem in SQL delete in a while loop

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
array220 challenge 8 46
XML Paring  Error - Premature end of file. 7 56
Java Message handling in Service Layer 3 37
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now