2

I'm trying to insert a value with out have batch execution will return the value that i mentions in query but it not works in the batch execution. It only return a integer array with status 1 or 0.My sample code are give below.

Connection connection = ConnectionManager.getInstance().getDBConnection();

        String query = "insert into custom_attribute_mapping (product_id,attribute_id,attribute_values) values (49,22,'yyyyyyyyyyyyy') RETURNING attribute_mapping_id";

        try (PreparedStatement prepStmt2 = connection
                .prepareStatement(query )) {
            int i =0;       
            ResultSet rs = prepStmt2.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getInt(1));

            }
        } catch (Exception e) {

        }

In the above code i got the value of the attribute_mapping_id. but in the below code the return type of the executeBatch is an integer array and it only have the insertion status.

Connection connection = ConnectionManager.getInstance().getDBConnection();

    String query = "insert into custom_attribute_mapping (product_id,attribute_id,attribute_values) values (49,22,'yyyyyyyyyyyyy') RETURNING attribute_mapping_id";

    try (PreparedStatement prepStmt2 = connection
            .prepareStatement(query )) {
        int i =0;
        while(i<5){
            i++;
            prepStmt2.addBatch();
        }           
        int[] rs = prepStmt2.executeBatch();
        for(int r:rs){
            System.out.println(r);
        }

    } catch (Exception e) {

    }

can any one please help me.

2 Answers 2

2

You can't get the generated values when using executeBatch(). It will only return update counts and even those aren't very clearly defined:

The elements in the array returned by the method executeBatch may be one of the following:

A number greater than or equal to zero -- indicates that the command was processed successfully and is an update count giving the number of rows in the database that were affected by the command's execution

A value of SUCCESS_NO_INFO -- indicates that the command was processed successfully but that the number of rows affected is unknown

If one of the commands in a batch update fails to execute properly, this method throws a BatchUpdateException, and a JDBC driver may or may not continue to process the remaining commands in the batch. However, the driver's behavior must be consistent with a particular DBMS, either always continuing to process commands or never continuing to process commands. If the driver continues processing after a failure, the array returned by the method BatchUpdateException.getUpdateCounts will contain as many elements as there are commands in the batch, and at least one of the elements will be the following:

A value of EXECUTE_FAILED -- indicates that the command failed to execute successfully and occurs only if a driver continues to process commands after a command fails

Sign up to request clarification or add additional context in comments.

Comments

0

Use

            PreparedStatement.RETURN_GENERATED_KEYS
            PreparedStatement.getGeneratedKeys();

may help get primary keys after executeBatch(), return whole data is not supposed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.