java用PrepareStatement实现多sql语句执行

try{

//连接数据库

conn = ConnMysqlUtil.getConnection();
//禁止自动提交(默认为自动提交,所以需要修改)
conn.setAutoCommit(false);
prst = conn.prepareStatement("update tb_admin set username = '123'  where id = 11 ");
prst.executeUpdate();//加载到缓冲区而不执行
prst = conn.prepareStatement("update tb_admin set userpass = '456' where id = 12 ");
prst.executeUpdate();//同上
prst = conn.prepareStatement("update tb_admin set userage = '789' where id = 13 ");
prst.executeUpdate();//同上
conn.commit();//提交处于缓冲区的sql
conn.setAutoCommit(true);

}catch (Exception e) {

e.printStackTrace();

}finally{

//关闭数据库连接

ConnMysqlUtil.release(rest, prst, conn);

}


try{

//连接数据库
conn = ConnMysqlUtil.getConnection();
prst = conn.prepareStatement("update tb_admin  set adminpass = ? where id = ? ");
prst.setString(1, "123");
prst.setString(2, "11");
prst.addBatch();
prst.setString(1, "123");
prst.setString(2, "12");
prst.addBatch();
prst.setString(1, "123");
prst.setString(2, "13333");
prst.addBatch();
prst.executeBatch();

}catch (Exception e) {
e.printStackTrace();

}finally{

//关闭数据库连接

ConnMysqlUtil.release(rest, prst, conn);

}


以上是个人整理的PrepareStatement对多条数据处理情况,供各位朋友参考

你可能感兴趣的:(Java,sql,多条sql执行)