Imagine that I have 100 SELECT queries that differ by one input. A PreparedStatement can be used for the value.
All the documentation I see on the Web is for batch insert/update/delete. I have never seen batches used for select statements.
Can this be done? If so, please help me when the below sample code.
I suppose this can be done using an "IN" clause, but I would prefer to use batched select statements.
Sample code:
public void run(Connection db_conn, List value_list) {
String sql = "SELECT * FROM DATA_TABLE WHERE ATTR = ?";
PreparedStatement pstmt = db_conn.prepareStatement(sql);
for (String value: value_list) {
pstmt.clearParameters();
pstmt.setObject(1, value);
pstmt.addBatch();
}
// What do I call here?
int[] result_array = pstmt.executeBatch()
while (pstmt.getMoreResults()) {
ResultSet result_set = pstmt.getResultSet();
// do work here
}
}
I suppose this may also be driver-dependent behaviour. I am writing queries against IBM AS/400 DB2 database using their JDBC driver.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…