81,116
社区成员
发帖
与我相关
我的任务
分享StringBuilder b = new StringBuilder("select * from table where id in (");
int[] ids = .....;
for(id id : ids){
b.append(id+",");
}
b = b.deleteCharAt(b.length()-1); // 去掉最后一个,
b.append(")";
String sql = b.toString(); // 去执行吧! 不过如果数组太大,则产生的sql语句长度会超过最大允许的SQL长度的。一般在1000左右。 Connection conn=null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
.....
conn = DriverManager.getConnection(...);
pstmt = conn.prepareStatement("select * from table where id=?");
for(int i:a){
pstmt.setInt(1, i);
rs=pstmt.executeQuery();
if(rs.next())
.....
}
} catch (Exception e) {
} finally {
try {if (rs != null) rs.close();} catch (Exception e) {}
try {if (pstmt != null) pstmt.close();} catch (Exception e) {}
try {if (conn != null) conn.close();} catch (Exception e) {}
}