jdbc批量更新并回滚

package com.kepler.test;

import java.sql.*;
public class TestNumsUpdate {
	public static void main(String[] args) throws Exception{
		Class.forName("org.gjt.mm.mysql.Driver");
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/auction", "root", "19890511");
		conn.setAutoCommit(false);// 將隨後的批量更新作為一個事務,進行更新操作
		try{
		Statement stat = conn.createStatement();
		stat.addBatch("update kind set kind_name = 'gggggg9999d9d9d99d9d9d9d999d9d9d9d9d9d9d9d9d9d9d9d9d9d9d99d9d' where kind_id=2 ");
		stat.addBatch("update kind set kind_desc = 'gggggg' where kind_id=2 ");
		stat.executeBatch();
		conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			conn.rollback();//如果更新出錯,回滾
		}
	}
}

你可能感兴趣的:(jdbc批量更新并回滚)