Java – two different preparation statements in a single batch

I want to send two different prepared statements in a batch

I'm currently doing two things. You can see its role in the comment line, but that's not the main purpose here Can anyone tell me what to do to replace these opinions?

import java.lang.ClassNotFoundException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.sqlException;
import java.sql.DriverManager;

public class Main
{
    public static void main(String[] args)
    {
        Connection connection = null;
        PreparedStatement preparedStatementWithdraw = null;
        PreparedStatement preparedStatementDeposit = null;

        try
        {
            Class.forName("com.MysqL.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:MysqL://localhost:3306/youtube","root","root");

            preparedStatementWithdraw = withdrawFromChecking(connection,preparedStatementWithdraw,new BigDecimal(100),1);
            preparedStatementDeposit = depositIntoSaving(connection,preparedStatementDeposit,new BigDecimal(300),1);

            //preparedStatementDeposit.executeBatch();
            //preparedStatementWithdraw.executeBatch();
            System.out.println("Account Modified!");
        }
        catch(ClassNotFoundException error)
        {
            System.out.println("Error: " + error.getMessage());
        }
        catch(sqlException error)
        {
            System.out.println("Error: " + error.getMessage());
        }
        finally
        {
            if(connection != null) try{connection.close();} catch(sqlException error) {}
            if(preparedStatementDeposit != null) try{preparedStatementDeposit.close();} catch(sqlException error) {}
        }
    }

    public static PreparedStatement withdrawFromChecking(Connection connection,PreparedStatement preparedStatement,BigDecimal balance,int id) throws sqlException
    {
        preparedStatement = connection.prepareStatement("UPDATE bankAccount SET checkingBalance = checkingBalance - ? WHERE id = ?");
        preparedStatement.setBigDecimal(1,balance);
        preparedStatement.setInt(2,id);
        preparedStatement.addBatch();

        return preparedStatement;
    }

    public static PreparedStatement depositIntoSaving(Connection connection,int id) throws sqlException
    {
        preparedStatement = connection.prepareStatement("UPDATE bankAccount SET savingBalance = savingBalance + ? WHERE id = ?");
        preparedStatement.setBigDecimal(1,id);
        preparedStatement.addBatch();

        return preparedStatement;
    }
}

Solution

You cannot execute two different statements in a single batch As @ Dan mentioned, you can – and must – execute them in a single transaction

Another option is to use stored procedures that can complete all operations in a single round trip to the server while maintaining the benefits of a single transaction

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>