Advertisement
Guest User

Untitled

a guest
May 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. @Override
  2.     public void newAccount(Account account, User user) {
  3.         try(Connection conn = ConnectionFactory.getInstance().getConnection()){
  4.             conn.setAutoCommit(false);
  5.             String account_insert = "insert into accounts (TYPE, BALANCE) values (?,?)";
  6.             String returnedCol[] = { "ACCOUNT_ID" };
  7.             PreparedStatement ps1 = conn.prepareStatement(account_insert, returnedCol);
  8.             ps1.setInt(1, account.getAccountType());
  9.             ps1.setDouble(2, account.getBalance());
  10.             int affectedrows = ps1.executeUpdate();
  11.             if(affectedrows!=0) {
  12.                 ResultSet pk = ps1.getGeneratedKeys();
  13.                 if (pk.next()) {
  14.                     account.setAccountId((pk.getInt(1)));
  15.                 } else {
  16.                     throw new SQLException("Creating account failed, no ID obtained.");
  17.                 }
  18.             }
  19.             conn.commit();
  20.             conn.close();
  21.             insertToJunction(account,user);
  22.         } catch (SQLException e) {
  23.             e.printStackTrace();
  24.         }
  25.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement