G
George
I need to reset a database into blank by delete several tables. I
would like to put all those "delete ..." query into one transaction.
My understanding is that addBatch() for preparedstatement is a
transaction and I do not need a rollback. How about executeBatch() in
the statement? The javadoc about this method says that it will return
an array about each one and it might be failure, which seems to
indicate it is not executed as one transaction. Is that the case?
Here is my code now. Is the rollback in the catch necessary?
try {
boolean commit = conn.getAutoCommit();
conn.setAutoCommit(false);
Statement deleteState = conn.createStatement();
for (String table : tableNames) {
deleteState.addBatch("DELETE FROM " + table);
}
deleteState.executeBatch();
conn.commit();
conn.setAutoCommit(commit);
} catch (SQLException e) {
log.error(e, e);
conn.rollback();
throw new DAOException();
}
BTW: Can the preparedStatement use table names as parameters?
conn.setAutoCommit(false);
PreparedStatement st=new PreparedStatement("Delete From ?");
for (String table:tableNames){
st.setString(1, table);
st.addBatch();
}
conn.commint();
would like to put all those "delete ..." query into one transaction.
My understanding is that addBatch() for preparedstatement is a
transaction and I do not need a rollback. How about executeBatch() in
the statement? The javadoc about this method says that it will return
an array about each one and it might be failure, which seems to
indicate it is not executed as one transaction. Is that the case?
Here is my code now. Is the rollback in the catch necessary?
try {
boolean commit = conn.getAutoCommit();
conn.setAutoCommit(false);
Statement deleteState = conn.createStatement();
for (String table : tableNames) {
deleteState.addBatch("DELETE FROM " + table);
}
deleteState.executeBatch();
conn.commit();
conn.setAutoCommit(commit);
} catch (SQLException e) {
log.error(e, e);
conn.rollback();
throw new DAOException();
}
BTW: Can the preparedStatement use table names as parameters?
conn.setAutoCommit(false);
PreparedStatement st=new PreparedStatement("Delete From ?");
for (String table:tableNames){
st.setString(1, table);
st.addBatch();
}
conn.commint();