JDBC之addBatch运用实例

JDBC之addBatch运用实例:

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class AddBatch {

	/**
	 * @Title: main
	 * @Description:
	 * @param:
	 * @return void 
	 * @user: wangzg
	 * @Date:2014-7-7
	 * @throws
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//int[] count = statementAddBatch();
		int[] count = preparedStatementAddBatch();
		if(count != null){
			for(int i : count){
				System.out.println(i);
//				SUCCESS_NO_INFO -2
//				EXECUTE_FAILED -3
			}
			
		}

	}
	
	/**
	 * 
	 * @Title: getConnection
	 * @Description:
	 * @param:
	 * @return Connection 
	 * @user: wangzg
	 * @Date:2014-7-7
	 * @throws
	 */
	public static Connection getConnection() throws ClassNotFoundException, SQLException{
		
		Connection conn = null;
		
		//加载oracle驱动
		Class.forName("oracle.jdbc.driver.OracleDriver");
		
		//连接数据库
		String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
		String user ="wzg";
		String password = "wzg";
		conn = DriverManager.getConnection(url, user, password);
		
		return conn;
		
	}

	/**
	 * 
	 * @Title: statementAddBatch
	 * @Description:
	 * @param:
	 * @return int[] 
	 * @user: wangzg
	 * @Date:2014-7-7
	 * @throws
	 */
	public static int[] statementAddBatch(){
		Connection conn = null;
		Statement stm = null;
		int[] count = null;
		try {
			conn = getConnection();
			
			conn.setAutoCommit(false);
			
			stm = conn.createStatement();
			
			stm.addBatch("insert into t_user(user_id,user_name,user_password) values(TEST_SEQUENCE.NEXTVAL,'wzg1','wzg1')");
			stm.addBatch("insert into t_user(user_id,user_name,user_password) values(TEST_SEQUENCE.NEXTVAL,'wzg2','wzg2')");
			stm.addBatch("insert into t_user(user_id,user_name,user_password) values(TEST_SEQUENCE.NEXTVAL,'wzg3','wzg3')");
			
			count = stm.executeBatch();
			
			conn.commit();
			conn.setAutoCommit(true);
			stm.close();
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return count;
	}

	/**
	 * 
	 * @Title: preparedStatementAddBatch
	 * @Description:
	 * @param:
	 * @return int[] 
	 * @user: wangzg
	 * @Date:2014-7-8
	 * @throws
	 */
    public static int[] preparedStatementAddBatch(){
    	int[] count = null;
    	Connection conn = null;
    	PreparedStatement pstm = null;
    	
    	try {
    		
			conn = getConnection();
			conn.setAutoCommit(false);
			pstm = conn.prepareStatement("insert into t_user(user_id,user_name,user_password) values(TEST_SEQUENCE.NEXTVAL,?,?)");
			
			pstm.setString(1, "lp1");
			pstm.setString(2, "lp1");
			pstm.addBatch();
			
			pstm.setString(1, "lp2");
			pstm.setString(2, "lp2");
			pstm.addBatch();
			
			pstm.setString(1, "lp3");
			pstm.setString(2, "lp3");
			pstm.addBatch();
			
			count = pstm.executeBatch();
			
			conn.commit();
			conn.setAutoCommit(true);
			pstm.close();
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    	return count;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值