Notes – JDBC in Advanced Java
JDBC stands for Java Database Connectivity. It is a Java API that allows Java applications to interact with databases.
JDBC provides a standard way to connect, query, update, and manage data from relational databases like MySQL, Oracle, PostgreSQL, etc.
Why Use JDBC?
- To connect Java applications with any relational database
- To perform database operations like:
- Insert, Update, Delete, and Retrieve data
- To write platform-independent database code
JDBC Architecture
| Layer | Description |
|---|---|
| Application Layer | Java program written by the developer |
| JDBC API | Interfaces and classes like Connection, Statement, ResultSet |
| JDBC Driver | Translates JDBC calls into database-specific calls |
| Database | The actual relational database like MySQL, Oracle, etc. |
Key JDBC Classes & Interfaces
| Component | Purpose |
|---|---|
DriverManager | Manages JDBC drivers and establishes connection |
Connection | Represents a session with the database |
Statement | Executes static SQL queries |
PreparedStatement | Executes precompiled SQL with input parameters |
ResultSet | Stores the result of SQL queries (like SELECT statements) |
Basic JDBC Workflow
- Load the Driver
Class.forName("com.mysql.cj.jdbc.Driver"); - Establish Connection
Connection con = DriverManager.getConnection(dbURL, username, password); - Create Statement
Statement stmt = con.createStatement(); - Execute Query
ResultSet rs = stmt.executeQuery("SELECT * FROM users"); - Process Result
while(rs.next()) { ... } - Close Connection
con.close();
Real-World Use Cases
- User registration forms storing data in a database
- Login authentication systems
- Admin dashboards fetching live data
- Inventory management apps
