Callable Statement
1. What Is CallableStatement? (beginner)
CallableStatement = JDBC caller for stored procedures ({call name(?,?)}). Handles IN (give!), OUT (take!), INOUT (both!). Extends PreparedStatement (safe params!).
2. Code — IN + OUT (exam pattern!)
-- DB: bonus adds 10% and returns total!
CREATE PROCEDURE bonus(IN id INT, OUT total INT)
BEGIN SELECT salary * 1.1 INTO total FROM emp WHERE eid = id; END
3. Param Kinds (exam table!)
| Kind | Java | Example |
|---|---|---|
| IN | setInt(1, x) |
input only! |
| OUT | registerOut + getInt |
output only! |
| INOUT | set + register + get |
both! |
4. Traps
- Index from 1 (not 0!).
{call ...}braces REQUIRED! registerOutParameterBEFOREexecute(else ERROR!).executeQuery(rows!) vsexecuteUpdate(count!) vsexecute(either/procedure!).
5. Exam Points
- IN+OUT program — 7 marks, very frequent.
- Register + index-1 + braces — 3-5 marks, favourite traps.