Code Room
Code reviewHard
Question
Review this Java (JDBC) method that debits a wallet balance.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix — specific and kind.
Learn the concepts
void withdraw(Connection conn, long acctId, BigDecimal amt) throws SQLException { PreparedStatement read = conn.prepareStatement( "SELECT balance FROM accounts WHERE id = ?"); read.setLong(1, acctId); ResultSet rs = read.executeQuery(); rs.next(); BigDecimal bal = rs.getBigDecimal("balance"); if (bal.compareTo(amt) < 0) throw new InsufficientFundsException(); BigDecimal next = bal.subtract(amt); PreparedStatement write = conn.prepareStatement( "UPDATE accounts SET balance = ? WHERE id = ?"); write.setBigDecimal(1, next); write.setLong(2, acctId); write.executeUpdate();}Run or narrate your approach, then ask the coach.