I want to delete three different tables at once and I have create a pretty simple SQL statement for that matter as following :
DELETE FROM tbl1;DELETE FROM tbl2;DELETE FROM tbl3;
this statement is correct when I run in mysql directly but from java no!
my java code :
public boolean clearTables()
{
boolean ans = false;
if (con != null)
{
try
{
String deleteQuery = " DELETE FROM tbl1;DELETE FROM tbl2;DELETE FROM tbl3;";
Statement st = con.createStatement();
st.execute(deleteQuery);
ans = true;
}
catch (Exception e)
{
e.printStackTrace();
ans= false;
}
}
return ans;
}
how can I run multiple SQL statements at once in java?
Use addBatch
then executeBatch
:
Statement st = con.createStatement();
st.addBatch("DELETE FROM tbl1");
st.addBatch("DELETE FROM tbl2");
st.addBatch("DELETE FROM tbl3");
int[] results = st.executeBatch();
Then results
will contain an array with the number of rows deleted from each table.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.