Updating SQL problem

Hi guys,

Ok i have an array which collects 'quantity' values from the previous jsp and the aim is to update my DB with these values. The updating of the specific records is working fine (in the sense that it finds the right recorded to update.

However the problem is that it updates all the records with the last value in the array. for example, if the values 1,2,3 go into the array i think it is updating them ALL with 1, then all with 2 then all with 3. the intension is to have the first record with value 1, the second with 2 and third with 3 but it seems to be running through all and obviously 3 is the last number in the array so thats all im seeing in the DB.

I can see that the problem lies within that loop but cannot figure out how to update the records with the different array values.

here's my code:

String[] vals = request.getParameterValues("Quantity");

Statement statement = null;

ResultSet rs = null;

try{

PreparedStatement ps = connection.prepareStatement("UPDATE SelectedItems SET Quantity=? WHERE LotID=(SELECT max(LotID) FROM SelectedItems)");

for (int i = 0; i<vals.length; i++){

ps.setString(1, vals[ i ]);

ps.executeUpdate();

}

}

catch (Exception e)

{

e.printStackTrace();

System.err.println(

"TS_ERROR: Problem with inserting selected items");

}

....

any suggestions?

Thanks>

[1454 byte] By [haggard17a] at [2007-11-27 10:48:51]
# 1

I think that for each iteration you must make a new prepared statement. Something like this

String[] vals = request.getParameterValues("Quantity");

Statement statement = null;

ResultSet rs = null;

try{

for (int i = 0; i<vals.length; i++){

PreparedStatement ps = connection.prepareStatement("UPDATESelectedItems SET Quantity=? WHERE LotID=(SELECT max(LotID) FROMSelectedItems)");

ps.setString(1, vals[ i ]);

ps.executeUpdate();

}

}

catch (Exception e)

{

e.printStackTrace();

System.err.println(

"TS_ERROR: Problem with inserting selected items");

}

>

manuel.leiriaa at 2007-7-28 23:03:40 > top of Java-index,Java Essentials,Java Programming...
# 2

hi manuel,

Thanks for the reply. I tried it that way but it gave exactly the same results. The DB query pulls out all the records with the highest 'lotID' and the number of records will be equivalent to the number of items stored in 'vals'. it should then pop the 'vals' into the records 1 by one.

haggard17a at 2007-7-28 23:03:40 > top of Java-index,Java Essentials,Java Programming...
# 3

> I think that for each iteration you must make a new prepared statement.

That's rubbish.

To the OP: Please don't cross post, and please read my reply in your other thread.

dcmintera at 2007-7-28 23:03:40 > top of Java-index,Java Essentials,Java Programming...