比如一次执行下面的几条sql:
insert into test values("2","name","password");
insert into test values("3","www","123");
update test set password="321" where id='2';
delete from test where id='1';我这样写的不知道问题在那
con.setAutoCommit(false);
statement=con.createStatement();
String sql="";
for(int i=0;i<list.size();i++){
sql=(String) list.get(i);
Util.println("执行脚本:"+sql);
statement.addBatch(sql);
}
statement.executeBatch();
statement.close();
if(autoCommit){
con.commit();
}
求大神代码,
insert into test values("2","name","password");
insert into test values("3","www","123");
update test set password="321" where id='2';
delete from test where id='1';我这样写的不知道问题在那
con.setAutoCommit(false);
statement=con.createStatement();
String sql="";
for(int i=0;i<list.size();i++){
sql=(String) list.get(i);
Util.println("执行脚本:"+sql);
statement.addBatch(sql);
}
statement.executeBatch();
statement.close();
if(autoCommit){
con.commit();
}
求大神代码,
con.commit();
} autoCommit 被设成false了, 你这里的代码还能执行吗?
con.commit();
}修改为
if(!autoCommit){
con.commit();
}
即可
insert into test values("2","name","password");
insert into test values("3","www","123");
update test set password="321" where id='2';
delete from test where id='1';
end;
-----------------------------以上就是sql语句,加上begin end后就不需要批处理了,而且,
就执行这么一条语句,跟其他表也没什么联系,把自动提交就不需要设置false了
问题中的sql语句是动态的每一次的是不一样的,我是把sql写入一个.sql文件中的,要执行的时候在去读这个文件,里面的sql不是同一张表的,有可能是几张表的增删改。谢谢