JDBC IN JAVA
JDBC (Java Database Connectivity) is a key API in Advanced Java that allows Java applications to interact with databases. It provides a standard way to connect to a database, execute SQL queries, and retrieve results. Below is a comprehensive overview:
Key Components of JDBC
-
DriverManager:
Manages a list of database drivers and establishes a connection between the application and the database.
-
Connection:
Represents the session between the Java application and the database.
-
Statement:
Executes static SQL queries against the database.
-
PreparedStatement:
Used to execute parameterized SQL queries, improving security and performance.
-
CallableStatement:
Used for executing stored procedures in the database.
-
ResultSet:
Represents the results of a query, allowing navigation through rows of data.
Steps to Use JDBC
- Load the Driver:
Class.forName("com.mysql.cj.jdbc.Driver"); // Load MySQL Driver
- Establish a Connection:
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/yourDatabaseName", "username", "password");
- Create a Statement:
Statement stmt = con.createStatement();
- Execute a Query:
ResultSet rs = stmt.executeQuery("SELECT * FROM yourTableName");
while (rs.next()) {
System.out.println(rs.getString("columnName"));
}
- Close the Connection:
rs.close();
stmt.close();
con.close();
Advantages of JDBC
- Platform Independence: Works on any platform with a supported Java environment.
- Supports Multiple Databases: Compatible with various databases like MySQL, Oracle, PostgreSQL, etc.
- Dynamic SQL Execution: Provides flexibility to execute both static and dynamic SQL.
- Database Metadata: Allows retrieval of database and table structure.
Types of JDBC Drivers
- Type 1: JDBC-ODBC Bridge Driver (Deprecated)
- Type 2: Native-API Driver
- Type 3: Network Protocol Driver
- Type 4: Thin Driver (Pure Java driver, widely used)
Common JDBC Operations
- Insert Data:
String query = "INSERT INTO employees (id, name) VALUES (?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setInt(1, 101);
pstmt.setString(2, "John Doe");
pstmt.executeUpdate();
- Update Data:
String query = "UPDATE employees SET name = ? WHERE id = ?";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, "Jane Doe");
pstmt.setInt(2, 101);
pstmt.executeUpdate();
- Delete Data:
String query = "DELETE FROM employees WHERE id = ?";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setInt(1, 101);
pstmt.executeUpdate();
Example Program
import java.sql.*;
public class JdbcExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/yourDatabase";
String user = "root";
String password = "password";
try (Connection con = DriverManager.getConnection(url, user, password);
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM yourTable")) {
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Top comments (0)