Con.setAutoCommit(false);
Stmt = con.createStatement();
Stmt.executeUpdate(“update account set monery=monery-1000 where name=’zhangsan’”);
stmt.executeUpdate(“update account set monery=monery+1000 where name=’lisi’”);
con.commit();
con.setAutoCommit(false);
Statement stmt=con.createStatement();
stmt.addBatch(“update account set monery=monery-1000 where name=’zhangsan’”);
stmt.addBatch(“update account set monery=monery+1000 where name=’lisi’);
stmt.executeBatch();
con.commit();
这个是statement的 两种批量执行的方法 普通的executeUpdate()和批处理addBatch()
statement能不能用类似PrepareStatement的setString()来对预编译对象的占位符赋值?还是只能拼接?PrepareStatement如何用事务批量执行增删改?(用setXXX系列的方法进行预编译参数进行赋值)
事物用beginTracation 和commit 具体的你去搜下。
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, "aaa");
ps.addBatch();
ps.setString(1, "bbb");
ps.addBatch();
ps.setString(1, "ccc");
ps.addBatch();ps.executeBatch();con.commit();