Solved

Problem in SQL delete in a while loop

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

919 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

14 Experts available now in Live!

Get 1:1 Help Now