🔌 JDBC Connectivity in Java (Java Database Connectivity)
JDBC is an API in Java that allows you to connect and interact with a relational database (like PostgreSQL, MySQL, Oracle, etc.).
📌 What is JDBC?
JDBC (Java Database Connectivity) is a standard Java API that enables Java applications to execute SQL statements, retrieve results, and manage database connections.
It is part of the Java Standard Edition (J2SE).
🔧 JDBC Architecture
Application
↓
JDBC API (java.sql)
↓
JDBC Driver Manager
↓
JDBC Driver (provided by DB vendor)
↓
Database (e.g., PostgreSQL, MySQL)
✅ Steps to Connect Java with Database using JDBC
1. Import the package
import java.sql.*;
2. Load and Register the Driver
Class.forName("org.postgresql.Driver"); // For PostgreSQL
3. Create a Connection
Connection con = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/mydb", "username", "password");
4. Create a Statement
Statement stmt = con.createStatement();
5. Execute the Query
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
6. Process the Results
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
7. Close the Connection
con.close();
✨ Complete Example (JDBC + PostgreSQL)
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) {
try {
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/mydb", "postgres", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
🧠JDBC Interfaces Overview
Interface | Purpose |
---|---|
Driver |
Loads the JDBC driver |
Connection |
Establishes the connection to the database |
Statement |
Executes SQL statements |
PreparedStatement |
Precompiled statement for better performance |
ResultSet |
Holds data retrieved from a query |
DriverManager |
Manages a list of database drivers |
🚀 Uses of JDBC
- Database Connectivity: Connect Java apps to databases.
- CRUD Operations: Perform Create, Read, Update, Delete operations.
- Data Processing: Fetch and process large datasets.
- Enterprise Applications: Backend for web and enterprise systems.
- Automation Tools: Create tools that generate reports or manipulate data.
- Desktop Applications: Enable local apps to access local/remote databases.
- Batch Processing: Insert/update multiple records efficiently.
🆚 JDBC vs ORM (e.g., Hibernate)
Feature | JDBC | Hibernate (ORM) |
---|---|---|
Control | Full SQL control | SQL abstracted with HQL/objects |
Learning Curve | Easier to start | Steeper learning curve |
Flexibility | High | High, but with more abstraction |
Productivity | Manual mapping | Automatic mapping (Entity classes) |
Performance | Fast for small tasks | Better for large applications |
Would you like a JDBC program that inserts or updates data, or a JDBC + GUI integration example?
Top comments (0)