批处理 addBatch (提高大批量的处理效率)

package 批处理;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Arrays;

import JDBC.SqlHelper;

public class AddBatchDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String sql1 = "CREATE TABLE tmp_table_1(id NUMBER)";
		String sql2 = "CREATE TABLE tmp_table_2(id NUMBER)";
		String sql3 = "CREATE TABLE tmp_table_3(id NUMBER)";
		
		String sql4 = "INSERT INTO tmp_table_1(id) VALUES (1)";
		String sql5 = "INSERT INTO tmp_table_1(id) VALUES (2)";
		String sql6 = "INSERT INTO tmp_table_1(id) VALUES (3)";
		
		Connection conn = null;
		try {
			conn = SqlHelper.getConnection();
			Statement st = conn.createStatement();
			st.addBatch(sql4);
			st.addBatch(sql5);
			st.addBatch(sql6);
			int[] ary = st.executeBatch(); // 大于等于 0 则为成功,sucess 也为成功, -2也为成功, -3 为失败
			System.out.println(Arrays.toString(ary));
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			SqlHelper.close(conn);
		}
	}

}

 

你可能感兴趣的:(JDBC)