批处理 编程式事务
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;
//時間為:415216try {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 blocke.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;
//時間為:415216try {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 blocke.printStackTrace();}finally{Jdbcutil.colse(null, ps, null);}}
}
把批處理如果執行不了就,放進事務中,并且jdbc.properties文件的url改為jdbc:mysql://localhost:3306/test?rewriteBatchedStatments=true"
?
?這是轉錢中途出現問題,用事務解決
?
?
?
轉載于:https://www.cnblogs.com/zhaosong-0102/p/7460623.html
總結