Guest User

Untitled

a guest
Apr 26th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. private void makeTransaction(int from_acc_ID, int to_acc_ID, double money) throws Exception {
  2.         try {
  3.             conn.setAutoCommit(false);
  4.             out.println("AUTO COMMIT = FALSE");
  5.             Statement st = conn.createStatement();
  6.             st.executeQuery("select balance from tova9159.account where account_id = " + from_acc_ID);
  7.             ResultSet rs = st.getResultSet();
  8.             rs.next();
  9.             double currentFromBalance = rs.getDouble(1);
  10.             st.executeQuery("select balance from tova9159.account where account_id = " + to_acc_ID);
  11.             rs = st.getResultSet();
  12.             rs.next();
  13.             double currentToBalance = rs.getDouble(1);
  14.             if (money > currentFromBalance)
  15.                 throw new Exception("Not enough money in the account");
  16.            
  17.             st.executeQuery("select currency_id from tova9159.account where account_id = " + from_acc_ID);
  18.             rs = st.getResultSet();
  19.             rs.next();
  20.             int fromCurrencyID = rs.getInt(1);
  21.            
  22.             st.executeQuery("select currency_id from tova9159.account where account_id = " + to_acc_ID);
  23.             rs = st.getResultSet();
  24.             rs.next();
  25.             int toCurrencyID = rs.getInt(1);
  26.            
  27.             st.executeQuery("select rate from tova9159.currency where currency_id = " + fromCurrencyID);
  28.             rs = st.getResultSet();
  29.             rs.next();
  30.             double fromRate = rs.getDouble(1);
  31.            
  32.             st.executeQuery("select rate from tova9159.currency where currency_id = " + toCurrencyID);
  33.             rs = st.getResultSet();
  34.             rs.next();
  35.             double toRate = rs.getDouble(1);
  36.            
  37.             double newFromBalance = currentFromBalance - money;
  38.             double newToBalance = currentToBalance + (money/toRate*fromRate);
  39.             out.println("START QUERIES" + money + " " + newFromBalance + " " + newToBalance);
  40.             st.executeUpdate("update tova9159.account set balance = " + newFromBalance + " "
  41.                     + "where account_id = " + from_acc_ID);
  42.             st.executeUpdate("update tova9159.account set balance = " + newToBalance + " "
  43.                     + "where account_id = " + to_acc_ID);
  44.             st.executeUpdate("insert into tova9159.transaction values (DEFAULT, DEFAULT, "
  45.                     + money + ", " + fromCurrencyID + ", " + from_acc_ID + ", " + to_acc_ID
  46.                     + ")");
  47.             out.println("END QUERIES");
  48.             conn.commit();
  49.         } catch (SQLException ex) {
  50.             try {
  51.                 conn.rollback();
  52.             } catch (SQLException ex1) {
  53.                 out.println(ex.getMessage());
  54.             }
  55.             out.println(ex.getMessage());
  56.         } finally {
  57.             try {
  58.                 conn.setAutoCommit(true);
  59.                 out.println("AUTO COMMIT = TRUE");
  60.             } catch (SQLException ex) {
  61.                 out.println(ex.getMessage());
  62.             }
  63.         }
  64.        
  65.     }
Add Comment
Please, Sign In to add comment