Introduction to JDBC in Java: A Beginner’s Guide with Examples
#Introduction JDBC in Java: A Beginner’s Guide with Examples
Introduction
In modern software development, most applications interact with databases. Java provides a powerful API known as JDBC (Java Database Connectivity) to connect, query, and manipulate relational databases.
JDBC acts as a bridge between Java applications and databases, making it possible to write database-independent code.
In this guide, we’ll explore JDBC architecture, drivers, steps to connect, and a working example.
JDBC (Java Database Connectivity) is a standard Java API that allows Java programs to interact with relational databases such as MySQL, Oracle, PostgreSQL, SQL Server, etc.
JDBC is part of Java SE.
It provides a set of classes and interfaces in the package:
java.sql
It follows a standard protocol so developers can switch databases without changing much code.
The JDBC architecture consists of two main layers:
JDBC API Layer – Interfaces and classes in Java (java.sql
package).
JDBC Driver Layer – Database-specific driver implementation that communicates with the database.
JDBC supports four types of drivers:
JDBC-ODBC Bridge Driver (Type 1) – Legacy, rarely used.
Native API Driver (Type 2) – Uses database-specific native libraries.
Network Protocol Driver (Type 3) – Middleware driver, communicates via server.
Thin Driver (Type 4) – Pure Java driver, most commonly used (e.g., MySQL Connector/J).
✅ Best choice today: Type 4 Driver.
Load the Driver Class
Establish the Connection
Create a Statement
Execute Queries (SQL)
Process Results
Close the Connection
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCDemo {
public static void main(String[] args) {
try {
// Step 1: Load and register driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Step 2: Establish connection
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testdb", "root", "password");
// Step 3: Create statement
Statement stmt = con.createStatement();
// Step 4: Execute query
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
// Step 5: Process results
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
// Step 6: Close connection
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
✅ Output: Prints records from the users
table in testdb
.
Platform-independent database connectivity.
Supports multiple relational databases.
Provides both statement and prepared statement execution.
Built-in transaction management.
Building Java desktop applications that interact with databases.
Backend web applications connecting to MySQL/Oracle/PostgreSQL.
Data migration between databases.
Running SQL queries inside Java applications.
Always close connections after use.
Use PreparedStatement instead of Statement to prevent SQL Injection.
Handle exceptions properly.
Use connection pooling in large applications.
JDBC in Java is the foundation of database programming. It provides a reliable and efficient way to connect Java applications with relational databases. With JDBC, developers can build powerful, database-driven applications that are scalable, secure, and portable.