日期:2014-05-16  浏览次数:20406 次

jdbc批处理使用
package Jdbcday02;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class TestBach {
	public static void main(String[] args) {
		batch();
	}
	public static void batch(){
		String sql1="insert into sun_student values(2,'rose',19,'F')";
		String sql2="insert into sun_student values(5,'rosess',20,'F')";
		try {
			Connection conn=ConnectionUtils.openConnection();
			conn.setAutoCommit(false);
			Statement stmt=conn.createStatement();
			stmt.addBatch(sql1);				//向处理中放进sql语句			
			stmt.addBatch(sql2);				//向处理中放进sql语句		
			int []result=stmt.executeBatch();//执行批处理,每条批处理语句执行所所影响的条数放在result集合中			
			conn.commit();
			for (int i : result) {
				System.out.println(i);			//如果插入成功是1	,	1更改了一条
			}
			conn.close();
			ConnectionUtils.closeStatement(stmt);
		}catch (SQLException e) {
			e.printStackTrace();
		}
	}
}