JDBC Specification 4.0描述了批量更新机制。因此,JDBC中的批处理功能可用于插入或更新目的。这在规范的第14章中有描述。
AFAIK没有选择批次的机制,可能是因为没有明显的需求,因为正如其他人所建议的那样,您可以通过正确构建查询来简单检索所需的所有行。
int[] ids = { 1, 2, 3, 4 };
StringBuilder sql = new StringBuilder();
sql.append("select jedi_name from jedi where id in(");
for (int i = 0; i < ids.length; i++) {
sql.append("?");
if(i+1 < ids.length){
sql.append(",");
}
}
sql.append(")");
System.out.println(sql.toString());
try (Connection con = DriverManager.getConnection(...)) {
PreparedStatement stm = con.prepareStatement(sql.toString());
for(int i=0; i < ids.length; i++){
stm.setInt(i+1, ids[i]);
}
ResultSet rs = stm.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("jedi_name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
输出
select jedi_name from jedi where id in(?,?,?,?)
Luke, Obiwan, Yoda, Mace Windu
是否有任何理由,你为什么会认为你需要像分批select语句的事情?
你的意思是什么“批量查询”?你可以使用'where c.id in(...)'获得多个客户。 – 2012-03-24 16:18:28
我不知道你的意思是“为客户提供多个结果集”。 – 2012-03-24 16:38:36
@a_horse_with_no_name我的意思是为一个客户设置一个结果集,不要混合返回行。 – superche 2012-03-24 16:45:44