Why is there no batchSelect in JDBC?
Is there some good way to ha开发者_运维百科ndle 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?
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?
精彩评论