Solved

Problem in SQL delete in a while loop

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java stored proc example 9 32
tomcat administrtor 12 48
hibernate jars 4 32
Java string replace 11 48
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

803 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