从preparedStatements列表转换为PreparedStatement批处理?

时间:2015-02-03 06:23:43

标签: java java-ee prepared-statement

我有一个PreparedStatement的数组列表

ArrayList<PreparedStatement> prestmtBatchList = new ArrayList<PreparedStatement>();

随着时间的推移,我补充说,没有。其中的preparedStatements。

PreparedStatement ps = DBUtility.returnInsertQueryAsString("ind_it_decl_sec10log", keysToSaveInLog, itdeclsec10loginfo, con);
                                    if(!ErmUtil.isNull(ps)){
                                        prestmtBatchList.add(ps);
                                    }

我想立即执行它们,因此需要批量转换。我知道这样做听起来很傻,但你的帮助将受到高度赞赏。请帮助我。

3 个答案:

答案 0 :(得分:0)

你仍然需要PreparedStatement,将函数的逻辑放在PreparedStatement代码本身。下面的解决方案将帮助你。

conn.setAutoCommit(false);

PreparedStatement ps = //instantiate
pstmt.setInt( 1, 1243);
pstmt.setString( 2, "test" );
pstmt.setString( 3, "test" );
pstmt.setInt( 4, 123 );

ps.addBatch();

pstmt.setInt( 1, 1243);
pstmt.setString( 2, "12334" );
pstmt.setString( 3, "21423" );
pstmt.setInt( 4, 123 );

ps.addBatch();

int[] count = ps.executeBatch();
conn.commit();

答案 1 :(得分:0)

对于批处理执行,您可以使用Prepared语句的本机executeBatch()方法。 为了提高Java数据库应用程序的性能,使用setAutoCommit(false)运行查询。默认情况下,新的JDBC连接具有自动提交模式ON,这意味着每个单独的SQL语句将在其自己的事务中执行。如果没有自动提交,您可以将SQL语句分组为逻辑事务,可以通过调用commit()或rollback()来提交或回滚。

下面的代码进一步解释: -

PreparedStatement preparedStatement = null;
            preparedStatement = dbConnection.prepareStatement(insertTableSQL);

            dbConnection.setAutoCommit(false);

            preparedStatement.setInt(1, 101);
            preparedStatement.setString(2, "test1");
            preparedStatement.addBatch();

            preparedStatement.setInt(1, 102);
            preparedStatement.setString(2, "test2");
            preparedStatement.addBatch();

            preparedStatement.setInt(1, 103);
            preparedStatement.setString(2, "test3");
            preparedStatement.addBatch();

            preparedStatement.executeBatch();

            dbConnection.commit();

答案 2 :(得分:0)

而不是怀疑一个人的帮助更好地建议解决方案。无论如何都喜欢全班。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

/**
 *
 * @author vaibhav.kashyap
 */


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

public class PreparedBatchJDBC {

    private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
    private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/mydb";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "root";

    public static void main(String[] argv) {

        try {

            batchInsertRecordsIntoTable();

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        }

    }

    private static void batchInsertRecordsIntoTable() throws SQLException {

        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;

        String insertTableSQL = "INSERT INTO DBUSER"
                + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
                + "(?,?,?,?)";

        try {
            dbConnection = getDBConnection();
            preparedStatement = dbConnection.prepareStatement(insertTableSQL);

            dbConnection.setAutoCommit(false);

            preparedStatement.setInt(1, 101);
            preparedStatement.setString(2, "test1");
            preparedStatement.setString(3, "system");
            preparedStatement.setTimestamp(4, getCurrentTimeStamp());
            preparedStatement.addBatch();

            preparedStatement.setInt(1, 102);
            preparedStatement.setString(2, "test2");
            preparedStatement.setString(3, "system");
            preparedStatement.setTimestamp(4, getCurrentTimeStamp());
            preparedStatement.addBatch();

            preparedStatement.setInt(1, 103);
            preparedStatement.setString(2, "test3");
            preparedStatement.setString(3, "system");
            preparedStatement.setTimestamp(4, getCurrentTimeStamp());
            preparedStatement.addBatch();

            preparedStatement.executeBatch();

            dbConnection.commit();

            System.out.println("Record is inserted into DBUSER table!");

        } catch (SQLException e) {

            System.out.println(e.getMessage());
            dbConnection.rollback();

        } finally {

            if (preparedStatement != null) {
                preparedStatement.close();
            }

            if (dbConnection != null) {
                dbConnection.close();
            }

        }

    }

    private static Connection getDBConnection() {

        Connection dbConnection = null;

        try {

            Class.forName(DB_DRIVER);

        } catch (ClassNotFoundException e) {

            System.out.println(e.getMessage());

        }

        try {

            dbConnection = DriverManager.getConnection(
                              DB_CONNECTION, DB_USER,DB_PASSWORD);
            return dbConnection;

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        }

        return dbConnection;

    }

    private static java.sql.Timestamp getCurrentTimeStamp() {

        java.util.Date today = new java.util.Date();
        return new java.sql.Timestamp(today.getTime());

    }

}

希望这也会有所帮助。