Featured resource
2026 Tech Forecast
2026 Tech Forecast

Stay ahead of what’s next in tech with predictions from 1,500+ business leaders, insiders, and Pluralsight Authors.

Get these insights
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Core Tech
Labs

Guided: Lightweight Data Persistence with JDBC and Records

In this Code Lab, you'll apply modern Java SE practices to implement a data access layer with JDBC. You will use Java Records as data carriers and interact with an H2 relational database using a clean, testable architecture.

Lab platform
Lab Info
Level
Intermediate
Last updated
Oct 29, 2025
Duration
35m

Contact sales

By clicking submit, you agree to our Privacy Policy and Terms of Use.
Table of Contents
  1. Challenge

    Introduction

    Welcome to this Code Lab on lightweight data persistence with JDBC and Java Records! While Object-Relational Mapping (ORM) frameworks like Hibernate are powerful, they can sometimes be overkill for simpler applications. Understanding how to use JDBC directly is a fundamental skill for any Java developer.

    In this lab, you'll learn modern JDBC techniques, including using try-with-resources for safe resource management, PreparedStatement to prevent SQL injection, and Java Records for creating clean, immutable Data Transfer Objects (DTOs).

    The Scenario

    You'll build the data access layer for a simple product catalog application. You are provided with a basic Maven project structure. Your job is to implement the ProductDao (Data Access Object) to perform Create, Read, Update, and Delete (CRUD) operations against an in-memory H2 database. info> If you get stuck on a task, there are complete code solutions for each task located in the solution folder. >This lab experience was developed by the Pluralsight team using Forge, an internally developed AI tool utilizing Gemini technology. All sections were verified by human experts for accuracy prior to publication. For issue reporting, please contact us.

  2. Challenge

    Step 2: Defining the Data Model

    Before you interact with the database, you need a way to represent your data in Java. Traditionally, this was done with verbose JavaBean classes. With modern Java, you can use Records.

    What are Records?

    Introduced in Java 16, a record is a special kind of class designed to be a transparent, immutable data carrier. When you declare a record, the Java compiler automatically generates:

    • A canonical constructor (taking all components as arguments).
    • public accessor methods for each component (e.g., product.name()).
    • Implementations of equals(), hashCode(), and toString().

    This makes them perfect for representing data retrieved from a database.

  3. Challenge

    Step 3: Connecting to the Database

    The foundation of any JDBC application is the Connection. A Connection represents a session with the database. It's best practice to centralize the logic for creating these connections.

    You will use the H2 database engine in its in-memory mode. This is extremely convenient for development and testing because the database exists only as long as the application is running, and it requires no external setup.

    You will also create a utility to set up our database schema (the tables and their structure) when the application starts.

  4. Challenge

    Step 4: Creating and Reading Data (CRUD)

    Now that you can connect to the database and have a table, you can start implementing your CRUD operations in the ProductDao class. Begin with 'Read' and 'Create'.

    Key JDBC Concepts

    • PreparedStatement: This is the preferred way to execute parameterized SQL queries. It pre-compiles the SQL and allows you to safely bind values to parameters, which is your primary defense against SQL injection attacks.
    • ResultSet: When you execute a query that returns data (like SELECT), the results are contained in a ResultSet object. You can iterate through it to access each row and retrieve column values by name or index.
    • try-with-resources: This Java feature is essential for working with JDBC. Resources like Connection, Statement, and ResultSet must be closed to prevent resource leaks. A try-with-resources block automatically closes any resource declared within it, whether the block completes normally or throws an exception.
  5. Challenge

    Step 5: Updating and Deleting Data (CRUD)

    To complete the set of core database operations, you will now implement the 'Update' and 'Delete' functionality. The principles are very similar to what you've already done. You'll use a PreparedStatement to execute a DML (Data Manipulation Language) command and check the return value to see how many rows were affected.

  6. Challenge

    Step 6: Handling Transactions

    By default, JDBC connections operate in auto-commit mode. This means every single SQL statement (INSERT, UPDATE, DELETE) is immediately committed as its own transaction. This is simple, but often not what you want.

    Imagine you need to update the prices of ten products. If the fifth update fails due to a database error, the first four updates are already permanent. This leaves your data in an inconsistent state.

    Transactions solve this. By disabling auto-commit, you can group a series of statements. They all succeed together (commit), or they all fail together (rollback), ensuring your data remains consistent. This is often referred to as atomicity. ### Running the Application

    To run the main application and see the example CRUD operations in action:

    ./run.sh
    

    Or you can use Maven directly:

    mvn compile exec:java
    

    This will:

    1. Initialize the in-memory H2 database with the products table
    2. Create sample products (Laptop and Smartphone)
    3. Display all products
    4. Find a product by ID
    5. Update a product's price
    6. Delete a product
    7. Display the final state of all products
  7. Challenge

    Summary

    Congratulations on completing this Code Lab!

    To summarize here's what you've learned:

    Java Records

    You learned how to use Java Records as clean, immutable data carriers, replacing verbose JavaBean classes with concise, compiler-generated code.

    JDBC Fundamentals

    You mastered the fundamentals of JDBC, including:

    • Establishing database connections
    • Executing parameterized queries with PreparedStatement to prevent SQL injection
    • Processing ResultSet objects to map database rows to Java objects

    CRUD Operations

    You implemented a complete CRUD (Create, Read, Update, Delete) data access layer, learning how to:

    • Insert records with auto-generated keys
    • Query single and multiple records
    • Update existing data
    • Delete records

    Transaction Management

    Most importantly, you discovered how to manage transactions to ensure data integrity, grouping multiple database operations into atomic units that either all succeed or all fail together.

    Resource Management Best Practices

    By using try-with-resources throughout, you also learned best practices for resource management, ensuring that database connections, statements, and result sets are properly closed even when exceptions occur.

About the author

Pluralsight Code Labs offer an opportunity to get hands-on learning in real-time. Be it a Challenge, Sandbox, or Guided Lab, these provide the real world experience needed to succeed in your development role.

Real skill practice before real-world application

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight