流程是这样的
set autocommit = 0;
delete from student ...;
insert into student ...;
commit;
set autocomit = 1;给你个例子:
import java.sql.*;public class Test
{
public static void main(String[] args) 
{
try
{
String query = ""; // Load Mysql's JDBC Driver
String driver="com.mysql.jdbc.Driver";
Class.forName(driver).newInstance(); // Remote mysql server of SiChuan
String url = "jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=gbk";
String user = "root";
String password = "password";
Connection connRemoteMysql = DriverManager.getConnection(url, user, password); // set AutoCommit = false
connRemoteMysql.setAutoCommit(false); // Define Statement
Statement stmt = connRemoteMysql.createStatement(); query = "delete from Student where class=5";
stmt.executeUpdate(query);
query = "insert into student(...) values(...)";
stmt.executeUpdate(query); // commit the translate
connRemoteMysql.commit();

if (stmt!=null){
stmt.close();
}
if (connRemoteMysql!=null){
connRemoteMysql.close();
} }
catch (Exception e) {
e.printStackTrace();
try {
if (connRemoteMysql != null && !connRemoteMysql.isClosed())
{
prt("Rolling back transaction");
connRemoteMysql.rollback();
}
}
catch(SQLException rx) {
System.out.println("SQLException - rollback() failed: " + rx.getMessage());
}
}
finally {
if (connRemoteMysql != null && !connRemoteMysql.isClosed()) {
connRemoteMysql.setAutoCommit(true);
}
}
}
}