This guide will help you set up PostgreSQL with Docker and run JDBC practice examples.
- Docker and Docker Compose installed
- Java 17 (already configured)
- Maven (IntelliJ IDEA has Maven bundled)
Open a terminal in the project directory and run:
docker-compose up -dThis will:
- Start a PostgreSQL 16 container
- Create a database named
ocp_practice - Initialize tables:
employees,departments,projects - Insert sample data
- Expose PostgreSQL on port 5432
To verify the database is running:
docker-compose psTo view logs:
docker-compose logs postgres- Open the project in IntelliJ IDEA
- IntelliJ should automatically detect the
pom.xmland prompt you to import the Maven project - If not, right-click on
pom.xml→ "Add as Maven Project" - Wait for Maven to download the PostgreSQL JDBC driver dependency
You can connect to the database using:
- Host: localhost
- Port: 5432
- Database: ocp_practice
- Username: ocpuser
- Password: ocppass123
Using psql (if installed):
docker exec -it ch16JDBC-practice-db psql -U ocpuser -d ocp_practiceOr use IntelliJ's Database tool:
- View → Tool Windows → Database
- Click "+" → Data Source → PostgreSQL
- Enter the connection details above
The examples are organized by topic and located in src/ch16JDBC/:
mvn compile exec:java -Dexec.mainClass="ch16JDBC.Example01_BasicConnection"Topics covered:
- Opening database connections
- Using try-with-resources
- Statement vs PreparedStatement
- Basic ResultSet iteration
mvn compile exec:java -Dexec.mainClass="ch16JDBC.Example02_CRUDOperations"Topics covered:
- INSERT with auto-generated keys
- SELECT with PreparedStatement
- UPDATE operations
- DELETE operations
- executeUpdate() return values
mvn compile exec:java -Dexec.mainClass="ch16JDBC.Example03_ResultSetNavigation"Topics covered:
- Forward-only vs scrollable ResultSets
- Navigation methods: first(), last(), absolute(), relative()
- Position checking: isFirst(), isLast(), isBeforeFirst()
- Updatable ResultSets
mvn compile exec:java -Dexec.mainClass="ch16JDBC.Example04_Transactions"Topics covered:
- Auto-commit mode
- Manual transactions with commit() and rollback()
- Savepoints for partial rollbacks
- Transaction error handling
mvn compile exec:java -Dexec.mainClass="ch16JDBC.Example05_BatchProcessing"Topics covered:
- addBatch() and executeBatch()
- Batch inserts with PreparedStatement
- Mixed batch operations
- BatchUpdateException handling
mvn compile exec:java -Dexec.mainClass="ch16JDBC.Example06_MetaData"Topics covered:
- DatabaseMetaData for database information
- ResultSetMetaData for column information
- Getting table and column details
- Dynamic result processing
mvn compile exec:java -Dexec.mainClass="ch16JDBC.Example07_ExecuteMethod"Topics covered:
- execute() method returning boolean
- Checking if result is ResultSet or update count
- getResultSet() and getUpdateCount() methods
- Handling unknown SQL types at runtime
- Difference between execute(), executeQuery(), and executeUpdate()
First, load the stored procedures into the database:
PowerShell:
Get-Content stored-procedures.sql | docker exec -i ch16JDBC-practice-db psql -U ocpuser -d ocp_practiceBash/Git Bash:
docker exec -i ch16JDBC-practice-db psql -U ocpuser -d ocp_practice < stored-procedures.sqlThen run the example:
mvn compile exec:java -Dexec.mainClass="ch16JDBC.Example08_CallableStatement"Topics covered:
- CallableStatement for calling stored procedures/functions
- Functions with NO parameters
- IN parameters only
- OUT parameters
- INOUT parameters (same parameter used for input and output)
- Mixed IN and OUT parameters
- Syntax patterns: {? = call func()} vs {call proc(?)}
- Navigate to the example file (e.g.,
Example01_BasicConnection.java) - Right-click on the file or the main method
- Select "Run 'Example01_BasicConnection.main()'"
docker-compose downdocker-compose down -vdocker-compose restartdocker-compose down -v
docker-compose up -dThis setup covers key JDBC topics for the Java SE 17 Developer (1Z0-829) exam:
- Connecting to databases using JDBC URLs and DriverManager
- Constructing and using RowSet objects
- Performing CRUD operations (Create, Read, Update, Delete)
- Processing query results using ResultSet
- Using PreparedStatement and CallableStatement
- Controlling transactions
- Using batch updates
- Working with database metadata
- Modify the examples to use different SQL queries
- Create your own tables and practice with them
- Experiment with different ResultSet types and concurrency modes
- Practice error handling and exception scenarios
- Try using connection pooling libraries (HikariCP, C3P0)
- Make sure Docker is running:
docker ps - Check if PostgreSQL container is up:
docker-compose ps - Verify port 5432 is not used by another application
- Ensure Maven dependencies are downloaded
- In IntelliJ: Right-click on
pom.xml→ Maven → Reload Project
- Verify the database is accessible:
docker-compose logs postgres - Check connection string in
DatabaseConfig.java - Ensure no firewall is blocking port 5432