This chapter covers all JDBC topics for the OCP Java 17 certification exam. Each file focuses on a specific aspect of JDBC with comprehensive documentation, examples, and exam tips.
Topics:
- CRUD operations and SQL keywords
- JDBC interfaces (Driver, Connection, PreparedStatement, CallableStatement, ResultSet)
- JDBC URL structure (jdbc:subprotocol:subname)
- Getting a Connection with DriverManager (all 3 signatures)
- Factory pattern
- SQLException handling
- Try-with-resources
Run: java -cp src ch15jdbc.JDBCBasics
Topics:
- Creating PreparedStatement from Connection
- Execute methods: executeUpdate(), executeQuery(), execute()
- Which SQL works with which method (table)
- Return types for each method
- Binding variables (?) and 1-based indexing
- Setter methods: setInt, setString, setDouble, setBoolean, setLong, setNull, setObject
- Reusing PreparedStatement with different parameters
- Common SQLException causes
Run: java -cp src ch15jdbc.PreparedStatementExamples
Topics:
- ResultSet cursor basics (starts BEFORE first row)
- Navigating with next()
- Loop patterns (while and if)
- Getter methods: getInt, getString, getDouble, getBoolean, getLong, getObject
- Accessing columns by index (1-based) vs by name
- Common SQLException causes with ResultSet
Run: java -cp src ch15jdbc.ResultSetExamples
- IN, OUT, INOUT parameter types
- Parameter comparison table (what to set, what to register)
- Setting parameters by index vs name
- executeQuery() with CallableStatement
- ResultSet type and concurrency options
Run: java -cp "src;lib/*" ch15jdbc.CallableStatementExamples
- What transactions, commit, and rollback are
- AutoCommit mode (default true, edge cases)
- setAutoCommit(true) triggers commit (EXAM TRAP!)
- Closing without commit = undefined behavior
- Savepoints - basic usage and invalidation rules
- Valid vs invalid savepoint order (with exceptions)
- Named vs unnamed savepoints
- Cascading close behavior clarification
Run: java -cp "src;lib/*" ch15jdbc.TransactionsAndSavepoints
- Proper closing order (reverse of creation)
- ResultSet → PreparedStatement → Connection
- Cascading close behavior (HEAVILY TESTED!)
- Closing Connection closes PreparedStatement AND ResultSet
- Closing PreparedStatement closes ResultSet
- Try-with-resources automatic closing
- Manual closing with finally blocks
- Common exam traps about closing order
Run: java -cp "src;lib/*" ch15jdbc.ClosingResources
Quick reference guide with:
- CRUD to SQL mapping table
- JDBC URL format
- DriverManager signatures
- Execute method comparison tables
- PreparedStatement setters list
- ResultSet getters list
- Common SQLException causes
- Top 10 exam traps
- Exam day checklist
- What's NOT on the exam
Note: This is a reference guide with no executable code.
You need the PostgreSQL JDBC driver to run these examples.
Download:
# Create lib directory
mkdir -p lib
# Download PostgreSQL JDBC driver
curl -o lib/postgresql-42.7.1.jar https://jdbc.postgresql.org/download/postgresql-42.7.1.jarOr download manually from: https://jdbc.postgresql.org/download/
docker-compose up -dRun this ONCE before running CallableStatementExamples:
# From project root directory
docker exec -i jdbc-practice-db psql -U ocpuser -d ocp_practice < src/ch15jdbc/stored-procedures.sqlThis creates the stored procedures needed for CallableStatement demonstrations.
Note: You are NOT tested on creating stored procedures on the exam, only on using them with CallableStatement!
docker ps | grep jdbc-practice-dbdocker-compose down- URL:
jdbc:postgresql://localhost:5432/ocp_practice - Username:
ocpuser - Password:
ocppass123 - Tables: employees, departments, projects
- Stored Procedures: See stored-procedures.sql
javac -cp "lib/*" src/ch15jdbc/*.java# JDBC Basics
java -cp "src;lib/*" ch15jdbc.JDBCBasics
# PreparedStatement Examples
java -cp "src;lib/*" ch15jdbc.PreparedStatementExamples
# ResultSet Examples
java -cp "src;lib/*" ch15jdbc.ResultSetExamples
# CallableStatement Examples (requires stored procedures setup)
java -cp "src;lib/*" ch15jdbc.CallableStatementExamples
# Transactions and Savepoints
java -cp "src;lib/*" ch15jdbc.TransactionsAndSavepoints
# Closing Resources
java -cp "src;lib/*" ch15jdbc.ClosingResourcesNote: On Windows use ; to separate classpath entries. On Linux/Mac use :
Linux/Mac version:
java -cp "src:lib/*" ch15jdbc.JDBCBasics- JDBC URL Format:
jdbc:subprotocol:subname(3 parts, always starts with "jdbc") - Execute Methods:
executeUpdate()→ int (INSERT, UPDATE, DELETE)executeQuery()→ ResultSet (SELECT only)execute()→ boolean (any SQL)
- JDBC uses 1-based indexing (NOT 0-based like arrays!)
- SQLException is CHECKED - must handle or declare
- ResultSet cursor starts BEFORE first row - must call next() first
- Closing Order: ResultSet → PreparedStatement → Connection (reverse of creation)
- Cascading Close: Closing Connection closes everything; closing PreparedStatement closes ResultSet
- Using index 0 for parameters or columns ❌
executeUpdate()with SELECT ❌executeQuery()with INSERT/UPDATE/DELETE ❌- Accessing ResultSet before calling next() ❌
- Forgetting SQLException is checked ❌
- SQL syntax errors (won't be tested)
- Statement interface (only PreparedStatement and CallableStatement)
- DataSource (only DriverManager)
- Batch updates, scrollable ResultSets
- Day 1-2: Read all documentation comments in each file
- Day 3-4: Run examples and observe output
- Day 5: Review JDBCExamReference.java (all tables and traps)
- Day 6: Test yourself on the Exam Day Checklist
- Day 7: Run all examples again and review any weak areas
If you encounter issues:
- Ensure Docker is running:
docker ps - Check database is accessible:
docker logs jdbc-practice-db - Verify PostgreSQL JDBC driver is available (should be handled by Docker)
Good luck on your exam! 🍀