JDBC之addBatch运用实例

来源:互联网 发布:电气常用数据手册 编辑:程序博客网 时间:2024/05/22 17:39

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 blocke.printStackTrace();try {conn.rollback();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();try {conn.rollback();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}finally{try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();try {conn.rollback();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();try {conn.rollback();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}finally{try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}    return count;    }}


0 0
原创粉丝点击