Java
Module 26: Java JDBC & Database ConnectivityBeginner28 min read

JDBC Architecture, PreparedStatement, CRUD & Transactions

Build secure database-driven Java applications with Connection, PreparedStatement (preventing SQL injection), ResultSet, and ACID transactions.

Key Takeaways & Core Concepts
  • JDBC Driver (Type 4 Pure Java): Translates JDBC calls into the database's native wire protocol (e.g., mysql-connector-j).
  • `DriverManager` vs `DataSource`: DriverManager.getConnection(...) opens a raw TCP connection per call (slow). Production servers always use a `DataSource` connection pool (like HikariCP) to reuse open connections.
  • `PreparedStatement` vs `Statement`: Never concatenate user input into SQL strings with Statement—that causes SQL Injection! PreparedStatement precompiles parameterized SQL (? placeholders) and binds user values safely as literal data.
  • `ResultSet`: Forward-only cursor over query rows (while (rs.next())).

Introduction

JDBC (Java Database Connectivity) is Java's standard database-access API (java.sql). Even when you use higher-level ORMs like Hibernate / Spring Data JPA or jOOQ, they all run on top of JDBC under the hood.

Secure JDBC PreparedStatement & Transaction Pattern
1public class Main {
2 public static void main(String[] args) {
3 String insertSql = "INSERT INTO students (name, email) VALUES (?, ?)";
4 String transferDebit = "UPDATE accounts SET balance = balance - ? WHERE id = ?";
5 System.out.println("Safe Parameterized SQL: " + insertSql);
6 System.out.println("Transactional Debit SQL: " + transferDebit);
7 }
8}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
8 lines
Note: Binding parameters with `ps.setString(1, name)` treats user input strictly as literal data, neutralizing SQL injection.

JDBC Architecture & Core Interfaces

  • JDBC Driver (Type 4 Pure Java): Translates JDBC calls into the database's native wire protocol (e.g., mysql-connector-j).
  • `DriverManager` vs `DataSource`: DriverManager.getConnection(...) opens a raw TCP connection per call (slow). Production servers always use a `DataSource` connection pool (like HikariCP) to reuse open connections.
  • `PreparedStatement` vs `Statement`: Never concatenate user input into SQL strings with Statement—that causes SQL Injection! PreparedStatement precompiles parameterized SQL (? placeholders) and binds user values safely as literal data.
  • `ResultSet`: Forward-only cursor over query rows (while (rs.next())).

ACID Transactions & Batch Processing

By default, JDBC runs in autoCommit = true mode (each SQL statement commits immediately). For multi-step operations like transferring funds:

  1. 1
    Call conn.setAutoCommit(false);.
  2. 2
    Execute debit & credit updates.
  3. 3
    Call conn.commit();, or conn.rollback(); inside catch (SQLException e).

For bulk inserts, use ps.addBatch() and ps.executeBatch() to minimize network round-trips.

Practice & AssessmentJava Programming Tutorial

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 0 correct(0 attempted)