Bank account system with JDBC
A banking console app with accounts, deposits, withdrawals and transfers stored in MySQL, using transactions so money is never lost.
What you'll learn
- âJDBC connections and PreparedStatement
- âDatabase transactions (commit/rollback)
- âHashing PINs
What you'll build
- âOpen account with a hashed 4-digit PIN
- âDeposit, withdraw, check balance
- âTransfer between accounts atomically
- âMini statement of last 10 transactions
Step-by-step guide
- 1
Create tables
accounts(acc_no PK, name, pin_hash, balance DECIMAL(12,2)) and transactions(id PK AUTO_INCREMENT, acc_no, type, amount, created_at).
- 2
Connect
Add the MySQL Connector/J dependency; DriverManager.getConnection(url, user, pass) using try-with-resources.
- 3
Hash the PIN
Never store the PIN itself. Use a salted hash (for example PBKDF2 via SecretKeyFactory "PBKDF2WithHmacSHA256").
- 4
Deposit and withdraw
UPDATE accounts SET balance = balance + ? WHERE acc_no = ?; for withdraw check balance >= amount in the same statement's WHERE.
- 5
Transfer with a transaction
conn.setAutoCommit(false); debit, credit, insert two transaction rows; commit(); on any exception rollback().
- 6
Mini statement
SELECT ... ORDER BY created_at DESC LIMIT 10.
- 7
Layer the code
AccountDao (SQL only), BankService (rules), Main (menu). This is how real backends are organised.
ð Make it stand out
- +Add interest calculation each month
- +Lock an account after 3 wrong PINs
- +Turn it into a Spring Boot REST API