Section 13 · Stage 4: Build Applications
JDBC & Database Connectivity
Set up Maven projects and connect Java to a database with JDBC: queries, prepared statements, transactions and the DAO pattern.
What you will be able to do
- Create and run a Maven project and add libraries as dependencies
- Connect a Java program to MySQL and run queries with JDBC
- Prevent SQL injection with prepared statements
- Manage transactions and close resources correctly from Java
- Organise database code with the DAO pattern and a connection pool
- Build a console version of ShopLite on top of the SQL schema
What you will learn
13.1Maven Projects & Dependencies9 lessons
Use Maven to build projects and download libraries instead of copying JAR files by hand.
- What a build tool does and why Maven35 min
- Create a Maven project in your IDE and from the terminal35 min
- pom.xml: coordinates, packaging and properties35 min
- Adding dependencies and the local repository40 min
- The build lifecycle: compile, test, package40 min
- Running your app and building a JAR40 min
- Maven vs Gradle in one lesson30 min
- Practice: Maven projects45 min
- Mini project: the shoplite-console Maven project60 min
13.2Connecting to a Database7 lessons
Open a connection from Java to MySQL, understand what the JDBC driver does, and fix the errors you meet on the way.
13.3Running Queries with JDBC7 lessons
Send SQL from Java and turn the results into Java objects.
- Statement and executeQuery40 min
- Reading a ResultSet row by row40 min
- Column types to Java types, dates and NULL40 min
- executeUpdate for INSERT, UPDATE and DELETE40 min
- Mapping rows to your own classes and records40 min
- Practice: running queries with JDBC45 min
- Mini project: ShopLite catalog reports60 min
13.4Prepared Statements & SQL Injection7 lessons
Pass user input to SQL safely, see how injection attacks work, and insert many rows efficiently.
- SQL injection demonstrated on a login query30 min
- PreparedStatement and ? placeholders30 min
- Setting parameters of each type30 min
- Getting generated keys after INSERT28 min
- Batch inserts with addBatch30 min
- Practice: prepared statements45 min
- Mini project: customer sign-up, product search and bulk import55 min
13.5Resource Handling & SQL Errors6 lessons
Close connections and statements reliably and handle database errors properly.
- Why unclosed connections break applications40 min
- try-with-resources for Connection, Statement and ResultSet40 min
- SQLException, SQL state and error codes40 min
- Turning SQL errors into meaningful application exceptions40 min
- Practice: resources and SQL errors50 min
- Mini project: leak-free ShopLite with clear errors60 min
13.6Transactions in JDBC7 lessons
Control commit and rollback from Java so related changes stay consistent, even with many users at once.
- Auto-commit and turning it off40 min
- commit and rollback in a try/catch45 min
- Placing an order: stock update plus order insert45 min
- Setting the isolation level45 min
- Preventing overselling with SELECT ... FOR UPDATE50 min
- Practice: transactions in JDBC55 min
- Mini project: placing ShopLite orders safely90 min
13.7The DAO Pattern8 lessons
Keep SQL in one layer so the rest of the program works with plain Java objects.
- Why mixing SQL with business logic hurts35 min
- DAO interface and JDBC implementation40 min
- Entity classes vs DTOs40 min
- A service layer on top of DAOs45 min
- Paging results with LIMIT and OFFSET from Java40 min
- Swapping implementations behind an interface40 min
- Practice: the DAO pattern50 min
- Mini project: the ShopLite DAO and service layers60 min
13.8Connection Pooling & Configuration7 lessons
Reuse database connections with a pool and load settings from configuration, the way real applications do.
- Why opening a connection per query is slow40 min
- DataSource vs DriverManager40 min
- HikariCP setup and key settings45 min
- Loading configuration from a properties file45 min
- Running SQL scripts at startup45 min
- Practice: connection pooling and configuration50 min
- Mini project: pooled, configurable shoplite-console60 min
13.9JDBC Mini Project: ShopLite Console App5 lessons
Build a menu-driven ShopLite app with Maven, JDBC and DAOs on the database from the SQL section.
