Statement stmt = con.createStatement(); stmt.executeUpdate( "INSERT INTO COFFEES " + "VALUES ('Colombian', 101, 7.99, 0, 0)");The code that follows inserts a second row into the table COFFEES . Note that we can just reuse the Statement object stmt rather than having to create a new one for each execution.stmt.executeUpdate("INSERT INTO COFFEES " + "VALUES ('French_Roast', 49, 8.99, 0, 0)");Values for the remaining rows can be inserted as follows:stmt.executeUpdate("INSERT INTO COFFEES " + "VALUES ('Espresso', 150, 9.99, 0, 0)"); stmt.executeUpdate("INSERT INTO COFFEES " + "VALUES ('Colombian_Decaf', 101, 8.99, 0, 0)"); stmt.executeUpdate("INSERT INTO COFFEES " + "VALUES ('French_Roast_Decaf', 49, 9.99, 0, 0)");
然后写出你所有要执行的SQL语句执行每一句,如果执行全部成功,connection.commit();
在catch里执行connection.rollback();有依据不成功就回滚
stmt.executeUpdate(
"INSERT INTO COFFEES " +
"VALUES ('Colombian', 101, 7.99, 0, 0)");The code that follows inserts a second row into the table COFFEES . Note that we can just reuse the Statement object stmt rather than having to create a new one for each execution.stmt.executeUpdate("INSERT INTO COFFEES " +
"VALUES ('French_Roast', 49, 8.99, 0, 0)");Values for the remaining rows can be inserted as follows:stmt.executeUpdate("INSERT INTO COFFEES " +
"VALUES ('Espresso', 150, 9.99, 0, 0)");
stmt.executeUpdate("INSERT INTO COFFEES " +
"VALUES ('Colombian_Decaf', 101, 8.99, 0, 0)");
stmt.executeUpdate("INSERT INTO COFFEES " +
"VALUES ('French_Roast_Decaf', 49, 9.99, 0, 0)");
我觉得这个不错,可是该怎么写呢?能给个例子吗!谢谢大家的回复!
sql = "insert into..."; //要执行的SQL
stmt.add(sql);
sql = "insert into..."; //要执行的SQL
stmt.add(sql);
stmt.executeBatch();还可以加入事务处理
“首先connection.setAutoCommit(false);
然后写出你所有要执行的SQL语句
执行每一句,如果执行全部成功,connection.commit(); ”如何判断知道是不是所有的执行都成功了呢?
我做测试:
connection.setAutoCommit(false);
操作一般插入语句
没有commit,但执行后他都自动commit,有些困惑。用的是MySql数据库。