๐ Introduction
In real-world applications, data is not stored in variables โ it is stored in databases ๐๏ธ
๐ But how does a Java program talk to a database like MySQL?
๐ The answer is JDBC (Java Database Connectivity) ๐ก
๐ง What is JDBC?
๐ JDBC is an API (Application Programming Interface) that allows Java programs to:
- Connect to database ๐
- Execute SQL queries ๐งพ
- Retrieve and update data ๐
๐ฏ Why JDBC?
- Connect Java with databases ๐
- Perform CRUD operations (Create, Read, Update, Delete)
- Used in real-world applications like banking, e-commerce ๐ฆ
๐งฑ 1. JDBC Architecture (Database Connectivity Model)
๐ง How JDBC Works?
๐ JDBC acts as a bridge between Java application and database
๐ Flow of JDBC
Java Application โ JDBC API โ JDBC Driver โ Database
๐ Components:
- Java Application ๐ป
- JDBC API ๐ฆ
- JDBC Driver โ๏ธ
- Database ๐๏ธ
๐ Explanation
- Java app sends request
- JDBC API forwards request
- Driver converts request into DB-specific format
- Database executes query
๐งฑ 2. Types of JDBC Drivers
๐น Type 1: JDBC-ODBC Bridge Driver
๐ Converts JDBC calls โ ODBC calls
- โ Slow
- โ Deprecated
๐น Type 2: Native API Driver
๐ Uses database-specific libraries
- Faster than Type 1
- Platform dependent
๐น Type 3: Network Protocol Driver
๐ Uses middleware server
- Platform independent
- Complex architecture
๐น Type 4: Thin Driver (Most Important โ )
๐ Pure Java driver (direct DB connection)
- โ๏ธ Fast
- โ๏ธ Platform independent
- โ๏ธ Most commonly used
๐ฏ Example
๐ MySQL Connector/J is Type 4 driver
๐งฑ 3. Roles of JDBC Drivers
๐ง What does a driver do?
๐ It acts as a translator ๐ง
๐ Responsibilities:
- Convert Java calls โ SQL commands
- Establish connection with database
- Send queries
- Return results
๐งฑ 4. Steps for JDBC Connectivity
๐ฅ 5 Basic Steps
1๏ธโฃ Load Driver
Class.forName("com.mysql.cj.jdbc.Driver");
2๏ธโฃ Establish Connection
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testdb", "root", "password");
3๏ธโฃ Create Statement
Statement stmt = con.createStatement();
4๏ธโฃ Execute Query
ResultSet rs = stmt.executeQuery("SELECT * FROM student");
5๏ธโฃ Close Connection
con.close();
๐งฑ 5. Database Connectivity Statements
๐น 1. Statement
๐ Used for simple SQL queries
โ Example
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM student");
๐น 2. PreparedStatement
๐ Precompiled SQL (better performance & security)
โ Example
PreparedStatement ps = con.prepareStatement(
"INSERT INTO student VALUES (?, ?)");
ps.setInt(1, 101);
ps.setString(2, "Ajay");
ps.executeUpdate();
๐ Advantage
- Prevents SQL Injection ๐
- Faster execution โก
๐น 3. CallableStatement
๐ Used for stored procedures
โ Example
CallableStatement cs = con.prepareCall("{call myProcedure()}");
cs.execute();
๐งฑ 6. Communicating with Database
๐ Types of Queries
๐น 1. executeQuery()
๐ Used for SELECT
ResultSet rs = stmt.executeQuery("SELECT * FROM student");
๐น 2. executeUpdate()
๐ Used for INSERT, UPDATE, DELETE
stmt.executeUpdate("INSERT INTO student VALUES (1, 'John')");
๐น 3. execute()
๐ Used for general SQL
๐ง Reading Data from ResultSet
โ Example
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
๐ Explanation
rs.next()โ moves to next rowgetInt(),getString()โ fetch data
๐งช Complete JDBC Program (Runnable)
import java.sql.*;
public class JDBCDemo {
public static void main(String[] args) {
try {
// Load driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Create connection
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testdb", "root", "password");
// Create statement
Statement stmt = con.createStatement();
// Execute query
ResultSet rs = stmt.executeQuery("SELECT * FROM student");
// Process result
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
// Close connection
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
โ ๏ธ Common Mistakes
โ Forgetting JDBC driver
โ Wrong URL format
โ Not closing connection
โ Using Statement instead of PreparedStatement
๐คฏ Common Confusions
๐ค Difference between executeQuery & executeUpdate
| Method | Use |
|---|---|
| executeQuery | SELECT |
| executeUpdate | INSERT/UPDATE |
๐ค Why PreparedStatement?
๐ Security + performance
๐ผ Interview Questions
โ What is JDBC?
๐ API to connect Java with database
โ Types of drivers?
๐ Type 1, 2, 3, 4
โ What is ResultSet?
๐ Stores query results
โ What is PreparedStatement?
๐ Precompiled SQL statement
๐ฏ Best Practices
โ๏ธ Use PreparedStatement
โ๏ธ Close connections
โ๏ธ Handle exceptions
โ๏ธ Use proper DB credentials
๐ Conclusion
JDBC is the foundation of database interaction in Java ๐
You learned:
- โ๏ธ JDBC architecture
- โ๏ธ Driver types
- โ๏ธ Statements
- โ๏ธ Database communication
๐ก Final Thought
๐ Without JDBC, Java applications cannot interact with databases โ
๐ making it one of the most important real-world concepts ๐ฏ
๐ฅ One-Line Summary
๐ โJDBC enables Java programs to communicate with databases using drivers and SQL statements.โ
๐ป Happy Coding! ๐
Next step: Build a real database project ๐ฅ