JDBC连接MYSQL,批量执行SQL语句或在执行一个SQL语句之前执行一个SQL语句

时间:2022-04-15 04:32:34
conn = MysqlJdbcUtils.getConnection();
Statement ps=conn.createStatement();
ps.addBatch("truncate QB_ShowCount_Asite_copy");
ps.executeBatch();
String SrcSql = "select convert(unhex(hex(convert(Community using latin1))) using utf8) as Community ,PID from Qb_MenberCmmSets_List where site=7";
stmt = conn.prepareStatement(SrcSql);
rs = stmt.executeQuery(); 上面是在下面的SQL语句执行之前执行清空命令 //批量执行SQL语句 方案一
Statement ps=conn.createStatement();
ps.addBatch("update user set money=money-100 where name='张三'");
ps.addBatch("update user set money=money+100 where name='李四'");
ps.addBatch("update temp set count=count+1 where name='张三'");
ps.executeBatch(); 不过这种使用的是Statement,只能是使用拼接字符串的方式,来传值;
方案二
如果多条语句重复,只是参数不变的话可以这样 PreparedStatement ps=conn.prepareStatement("insert into temp values(?)");
ps.setInt(1, 100);
ps.addBatch();
ps.setInt(1, 200);
ps.addBatch();
ps.executeBatch(); 方案三
写成存储过程大笑 参考http://blog.csdn.net/cnmcxiari/article/details/6632975