Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void makeTransaction(int from_acc_ID, int to_acc_ID, double money) throws Exception {
- try {
- conn.setAutoCommit(false);
- out.println("AUTO COMMIT = FALSE");
- Statement st = conn.createStatement();
- st.executeQuery("select balance from tova9159.account where account_id = " + from_acc_ID);
- ResultSet rs = st.getResultSet();
- rs.next();
- double currentFromBalance = rs.getDouble(1);
- st.executeQuery("select balance from tova9159.account where account_id = " + to_acc_ID);
- rs = st.getResultSet();
- rs.next();
- double currentToBalance = rs.getDouble(1);
- if (money > currentFromBalance)
- throw new Exception("Not enough money in the account");
- st.executeQuery("select currency_id from tova9159.account where account_id = " + from_acc_ID);
- rs = st.getResultSet();
- rs.next();
- int fromCurrencyID = rs.getInt(1);
- st.executeQuery("select currency_id from tova9159.account where account_id = " + to_acc_ID);
- rs = st.getResultSet();
- rs.next();
- int toCurrencyID = rs.getInt(1);
- st.executeQuery("select rate from tova9159.currency where currency_id = " + fromCurrencyID);
- rs = st.getResultSet();
- rs.next();
- double fromRate = rs.getDouble(1);
- st.executeQuery("select rate from tova9159.currency where currency_id = " + toCurrencyID);
- rs = st.getResultSet();
- rs.next();
- double toRate = rs.getDouble(1);
- double newFromBalance = currentFromBalance - money;
- double newToBalance = currentToBalance + (money/toRate*fromRate);
- out.println("START QUERIES" + money + " " + newFromBalance + " " + newToBalance);
- st.executeUpdate("update tova9159.account set balance = " + newFromBalance + " "
- + "where account_id = " + from_acc_ID);
- st.executeUpdate("update tova9159.account set balance = " + newToBalance + " "
- + "where account_id = " + to_acc_ID);
- st.executeUpdate("insert into tova9159.transaction values (DEFAULT, DEFAULT, "
- + money + ", " + fromCurrencyID + ", " + from_acc_ID + ", " + to_acc_ID
- + ")");
- out.println("END QUERIES");
- conn.commit();
- } catch (SQLException ex) {
- try {
- conn.rollback();
- } catch (SQLException ex1) {
- out.println(ex.getMessage());
- }
- out.println(ex.getMessage());
- } finally {
- try {
- conn.setAutoCommit(true);
- out.println("AUTO COMMIT = TRUE");
- } catch (SQLException ex) {
- out.println(ex.getMessage());
- }
- }
- }
Add Comment
Please, Sign In to add comment