0

I have a stored procedure call

call get(id);

in my database that would return a single row in a ResultSet object (it is basically a very long SELECT statement inside). Now in Java, I have a

int[] ids = {1,2,3}

and for each id in the list I want to call the stored procedure to get the record. So eventually I want a list of records corresponding to each id in ids. I attempted

Connection conn = ...;
PreparedStatement ps = conn.prepareCall("call get(?)");
for(int id : ids) {
    ps.setInt(1, id); ps.addBatch();
}
ps.executeBatch();

with only the result of call

get(3);

is kept.

I also tried to move the SELECT statement directly as a String in Java, so it looks like

String stmt = "SELECT ... WHERE id=?"
PreparedStatement ps = conn.prepareStatement(stmt);
... // the for loop same as above
ps.executeBatch();

with a "Can not issue executeUpdate() for SELECTs". I tried

ps.executeQuery();

as the last line, but it also only has the result of the last SELECT.

I want to avoid a for loop that executes a SELECT a time because transferring data from the database to my computer has a very heavy overhead.

Can anyone help? Thank you in advance!

EDIT: I read Running the same select query multiple times with different parameters: Mysql , but in my case it is a little bit different, because in my select statement the id actually needs to be referenced several times; the code looks like this

SELECT * FROM
  (SELECT ... FROM ... WHERE id=? AND ...),
  (SELECT ... FROM ... WHERE id=? AND ...);

so it seems impractical to use the IN method. Also I have another call

call get2(aid, userId)

and a list of {{1, 1000}, {2, 2000}} which also does not fit this issue. Can anyone help? Thank you!

Community
  • 1
  • 1
Yifan Hong
  • 41
  • 6

0 Answers0