开发者

Select in batch mode JDBC SQL

开发者 https://www.devze.com 2023-03-13 11:52 出处:网络
Why is there no batchSelect in JDBC? Is there some good way to ha开发者_运维百科ndle select for multiple keys or ids?

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消