1

Why is there no batchSelect in JDBC?

Is there some good way to handle select for multiple keys or ids?

Creating a query matching the length of all possible keys seem silly, because the database couldn't reuse prepared statements. Using stored procedures is very database dependant.

Are their any better ways?

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
stock steif
  • 21
  • 1
  • 4

1 Answers1

3

Use the IN clause. E.g.

SELECT 
    id, name, value 
FROM 
    entity
WHERE 
    id IN (1, 13, 42)

This returns the entities having an id of 1, 13 and 42 in a ResultSet with 3 rows.

Using the IN clause in a JDBC PreparedStatement is however not exactly trivial. As to how to do this, check this question: What is the best approach using JDBC for parameterizing an IN clause?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I hope you dint answer the OP question(my question too) "Why is there no batchSelect in JDBC?"... – Stunner Oct 10 '13 at 10:45