Explain Java Database Connectivity
JDBC (Java Database Connectivity) is an API (Application Programming Interface) in Java that defines how a client may access a database. It provides methods for querying and updating data in a database. JDBC is oriented towards relational databases.
Table of Contents
Key Components of Explain Java Database Connectivity
- DriverManager: Manages a list of database drivers.
- Driver: An interface that handles the communications with the database server.
- Connection: Interface with all methods for contacting a database.
- Statement: Â Used for executing a static SQL statement and returning the results.
- PreparedStatement: Used for executing a precompiled SQL statement.
- ResultSet: Represents the result set of a database query.
Steps to Use Java Database Connectivity
- Load the JDBC Driver: Register the driver class with DriverManager.
- Establish a Connection: Use DriverManager.getConnection to create a Connection object.
- Create a Statement: Use the Connection object to create a Statement or PreparedStatement.
- Execute a Query: Execute SQL queries using the Statement object.
- Process the Results: Use the ResultSet object to process the results of the query.
- Close the Resources: Close the ResultSet, Statement, and Connection objects to free up resources.
Here is a conceptual picture of the JDBC process:
Let’s illustrate the Java Database Connectivity process with an example.
Create a Database Table:
Before running the Java code, create a table in your database.
sql
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
age INT,
salary DOUBLE
);
Java Program Using JDBC:
java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCExample {
public static void main(String[] args) {
String jdbcURL = "jdbc:mysql://localhost:3306/your_database";
String username = "root";
String password = "password";
Connection connection = null;
try {
// Load the Java Database Connectivity driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish a connection
connection = DriverManager.getConnection(jdbcURL, username, password);
System.out.println("Connected to the database!");
// Create a SQL query
String sql = "SELECT * FROM employees";
// Create a Statement
PreparedStatement statement = connection.prepareStatement(sql);
// Execute the query
ResultSet resultSet = statement.executeQuery();
// Process the results
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
double salary = resultSet.getDouble("salary");
System.out.println(id + ", " + name + ", " + age + ", " + salary);
}
// Close the resources
resultSet.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
System.out.println("Disconnected from the database.");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
Explanation
- Loading the Java Database Connectivity Driver:
- Class.forName(“com.mysql.cj.jdbc.Driver”); loads the MySQL JDBC driver.
- Establishing a Connection:
- DriverManager.getConnection(jdbcURL, username, password); creates a connection to the database.
- Creating a Statement:
- connection.prepareStatement(sql); creates a PreparedStatement for the SQL query.
- Executing the Query:
- statement.executeQuery(); executes the SQL query and returns the result set.
- Processing the Results:
- The ResultSet object is used to iterate over the results and print them.
- Closing the Resources:
- The ResultSet, PreparedStatem