Advertisement
Guest User

Untitled

a guest
May 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. try(Connection conn = ConnectionFactory.getInstance().getConnection()){
  2.             conn.setAutoCommit(false);
  3.             String account_insert = "insert into accounts (TYPE, BALANCE) values (?,?)";
  4.             String junctiontable_insert = "insert into useraccountsjunction (USER_ID, ACCOUNT_ID) values (?,?)";
  5.             PreparedStatement ps1 = conn.prepareStatement(account_insert);
  6.             PreparedStatement ps2 = conn.prepareStatement(junctiontable_insert);
  7.             ps1.setInt(1, account.getAccountType());
  8.             ps1.setDouble(2, account.getBalance());
  9.             int rows = ps1.executeUpdate();
  10.             if(rows != 0) {
  11.                 ResultSet pk = ps1.getGeneratedKeys();
  12.                 while(pk.next()) {
  13.                     user.setId(pk.getInt(1));
  14.                     System.out.println(pk.getInt(1));
  15.                 }
  16.             }
  17.             ps2.setInt(1, user.getId());
  18.             ps2.setInt(2, account.getAccountId());
  19.             ps2.executeUpdate();
  20.             conn.commit();
  21.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement