从PreparedStatement列表转换为PreparedStatement批处理

问题描述 投票:0回答:3

我有一个PreparedStatement的arrayList

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

随着时间的流逝,我在其中添加了许多prepareStatement:

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

我想立即执行它们,因此需要批量转换。我知道这样做听起来很愚蠢。

java jakarta-ee prepared-statement
3个回答
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();

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();

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());

    }

}

希望这也会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.