package com.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import Utils.Jdbcutil; public class insert { public void batche(Connection conn) { PreparedStatement ps = null; //时间为:415216 try { conn = Jdbcutil.getConnection(); String sql = "INSERT INTO battch(name,money) VALUES(?,?)"; ps = conn.prepareStatement(sql); for (int i = 0; i < 10000; i++) { ps.setString(1, "马召松"); ps.setInt(2, 10000); ps.addBatch(); } long start = System.currentTimeMillis();//时间戳 ps.executeBatch(); long end = System.currentTimeMillis();//时间戳 System.out.println("时间为:"+(end-start)); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ Jdbcutil.colse(null, ps, null); } } }
package com.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import Utils.Jdbcutil; public class money { public void cout(Connection conn,Integer money,Integer id) { PreparedStatement ps = null; //时间为:415216 try { conn = Jdbcutil.getConnection(); String sql = "UPDATE battch set money=money+? where id=?"; ps = conn.prepareStatement(sql); ps.setInt(1, money); ps.setInt(2, id); ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ Jdbcutil.colse(null, ps, null); } } }
把批处理如果执行不了就,放进事务中,并且jdbc.properties文件的url改为jdbc:mysql://localhost:3306/test?rewriteBatchedStatments=true"
这是转钱中途出现问题,用事务解决