2つの異なるプリペアドステートメントを1つのバッチで送信したいと思います。
コメント行でわかるように、現在私はこれを2つに分けて行っており、機能しますが、それはここでの主な目的ではありません。誰かがこのことを機能させるためにそれらのコメントの代わりに何を置くべきか教えてもらえますか?
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, PreparedStatement preparedStatement, BigDecimal balance, int id) throws SQLException
{
preparedStatement = connection.prepareStatement("UPDATE bankAccount SET savingBalance = savingBalance + ? WHERE id = ?");
preparedStatement.setBigDecimal(1, balance);
preparedStatement.setInt(2, id);
preparedStatement.addBatch();
return preparedStatement;
}
}