☕ What is JDBC?
JDBC (Java Database Connectivity) is an API that allows Java applications to connect to
and interact with databases.
It’s part of the [Link] package.
⚙️ JDBC Architecture
Java Application
↓
JDBC API ([Link])
↓
JDBC Driver
↓
Database (MySQL, Oracle, etc.)
🔗 5 Steps to Connect Java with Database
Ste Method Description
p
1️⃣ Load Driver Registers the database driver class
2️⃣ Create Connects Java to the database
Connection
3️⃣ Create Statement Defines SQL queries
4️⃣ Execute Query Executes SQL (SELECT, INSERT, etc.)
5️⃣ Close Connection Frees resources
💻 Example: JDBC with MySQL
🧰 Prerequisites:
● Install MySQL
● Add MySQL Connector JAR to your classpath (e.g.,
[Link])
🪄 Code Example
import [Link].*;
public class JdbcExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "root";
String password = "1234";
try {
// 1. Load Driver
[Link]("[Link]");
// 2. Establish Connection
Connection con = [Link](url, user,
password);
// 3. Create Statement
Statement stmt = [Link]();
// 4. Execute Query
ResultSet rs = [Link]("SELECT * FROM
students");
// 5. Process Results
while ([Link]()) {
[Link]([Link]("id") + " " +
[Link]("name"));
}
// 6. Close Connection
[Link]();
[Link]();
[Link]();
} catch (Exception e) {
[Link]();
}
}
}
📦 JDBC Classes and Interfaces
Interface/Class Description
DriverManager Manages database drivers and
connections
Connection Represents a connection to the database
Statement Used for executing simple SQL
statements
PreparedStatemen Used for parameterized queries (safer)
t
ResultSet Represents the result of a SQL query
SQLException Handles database-related exceptions
🧮 Example: Insert Using PreparedStatement
import [Link].*;
public class InsertExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "root";
String password = "1234";
try (Connection con = [Link](url, user,
password)) {
String query = "INSERT INTO students (id, name, marks)
VALUES (?, ?, ?)";
PreparedStatement ps = [Link](query);
[Link](1, 101);
[Link](2, "John");
[Link](3, 85);
int rows = [Link]();
[Link](rows + " record(s) inserted.");
} catch (SQLException e) {
[Link]();
}
}
}
✅ Advantages of PreparedStatement:
● Prevents SQL Injection
● Faster execution (precompiled)
● Easier to reuse with parameters
🔍 Executing SQL Commands
SQL Type JDBC Method Example
SELECT executeQuery Returns ResultSet
()
INSERT, UPDATE, DELETE executeUpdat Returns number of affected
e() rows
DDL (CREATE, DROP) execute() Returns boolean or update
count
🧾 Processing ResultSet
while ([Link]()) {
int id = [Link]("id");
String name = [Link]("name");
double marks = [Link]("marks");
[Link](id + " " + name + " " + marks);
}
🧹 Closing Resources
Always close JDBC objects to free resources:
[Link]();
[Link]();
[Link]();
✅ Or use try-with-resources (auto-closes everything):
try (Connection con = ...; PreparedStatement ps = ...; ResultSet rs =
...) {
// code
}
🧭 Common JDBC URL Patterns
Database URL Example
MySQL jdbc:mysql://localhost:3306/dbname
Oracle jdbc:oracle:thin:@localhost:1521:xe
PostgreSQ jdbc:postgresql://localhost:5432/dbname
L
SQL Server jdbc:sqlserver://localhost:1433;databas
eName=mydb
⚡ JDBC CRUD Summary
Operation SQL JDBC Method
Create INSERT executeUpdat
e()
Read SELECT executeQuery
()
Update UPDATE executeUpdat
e()
Delete DELETE executeUpdat
e()
🧮 Example Output
101 John
102 Alice
103 Raj
Would you like me to create a “Basic JDBC Cheatsheet PDF” (with connection setup, CRUD
examples, and PreparedStatement syntax)? It’s great for quick study or interviews.