Im using Statement
s batchs to query my data base.
Iv'e done some research now and i want to rewrite my application to use preparedStatement
instead but i'm having hard time to figure out how to add queries to a preparedStatement
batch.
This is what i'm doing now:
private void addToBatch(String sql) throws SQLException{
sttmnt.addBatch(sql);
batchSize++;
if (batchSize == elementsPerExecute){
executeBatches();
}
}
where sttmnt
is a class member of type Statement
.
What i want to do is to use the preparedStatement
's setString(int, String)
method to set some dynamic data and then add it to the batch.
Unfortunately, i don't fully understand how it works, and how i can use setString(int, String)
to a specific sql in the batch OR create a new preparedStatemnt
for every sql i have and then join them all to one batch.
is it possible to do that? or am i really missing something in my understanding of preparedStatement
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…