JDBC: Java
Database
Connectivity
Overview on JDBC ,Design of JDBC ,Configuration of JDBC
by Sincy K Sabu M1C
JDBC
Java Database Connectivity
Connects Java Program with a Database
It is an API (Application Programming Interface)
JDBC: What is it?
JDBC (Java Database Connectivity) is a Java API that allows Java
programs to interact with databases.
It provides a standard set of classes and interfaces that make it easy to
connect to, query, and manipulate databases.
JDBC Design
JDBC Architecture
JDBC API
Java Application
JDBC Driver
Database
With JDBC, you can execute SQL commands and retrieve results. Use a scrollable and updatable
ResultSet for greater flexibility.
JDBC APO 1
collection of prewritten packages,
classes, and interfaces
2 JDBC Application
program written in Java.
JDBC Driver 3
software component that enables
java application to interact with the
database. 4 Database
organized collection of structured
information
JDBC Configuration
STEP 1: Load JDBC Drivers
Use Class.forName() to load the JDBC driver. EXAMPLE
SYNTAX // Loading MySQL JDBC driver
Class.forName("com.mysql.jdbc.Driver"); try {
// Loading the MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
JDBC Configuration
STEP 2: Establish Connection
Use // Establishing a connection to a MySQL database
DriverManager.getConnection() to
String url = "jdbc:mysql://localhost:3306/mydatabase";
establish a connection to the
database. String username = "username";
String password = "password";
SYNTAX
try {
Connection conn =
DriverManager.getConnection(url, username, Connection conn = DriverManager.getConnection(url, username, password);
password);
} catch (SQLException e) {
e.printStackTrace();
}
JDBC Configuration
STEP 3: Create Statement STEP 4: Executing Statements
Create a Statement, PreparedStatement, or CallableStatement based on the query requirements.
Use the created statement to execute SQL queries against the database.
SYNTAX
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM my_table");
// OR
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO my_table (column1, column2) VALUES (?, ?)");
JDBC Configuration
STEP 3: Create Statement STEP 4: Executing Statements
Create a Statement, PreparedStatement, or CallableStatement based on the query requirements.
Use the created statement to execute SQL queries against the database.
Example (Executing a Select Query):
try {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM employees");
while (resultSet.next()) {
// Process the data retrieved
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
// ... (retrieve other columns as needed)
System.out.println("ID: " + id + ", Name: " + name);
}
} catch (SQLException e) { e.printStackTrace();
}
JDBC Configuration
STEP 5: Processing Results STEP 6: Close Connection
Retrieve and process the results obtained from the executed queries using the ResultSet.
Close the ResultSet, Statement, and Connection objects after the operations are completed to
release database resources.
SYNTAX // Closing resources
// Processing the ResultSet resultSet.close();
while (resultSet.next()) {
statement.close();
// Access and process data
connection.close();
}
EXAMPLE CODE
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCConfiguration { public static void main(String[] args) {
// JDBC URL for MySQL database
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "username";
String password = "password";
try {
// Loading the MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Establishing a connection to the database
Connection connection = DriverManager.getConnection(url, username, password);
if (connection != null) {
System.out.println("Connected to the database!");
// Perform database operations here
// Closing the connection
connection.close();
System.out.println("Connection closed.");
}
} catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC driver not found.");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Connection failed!");
e.printStackTrace();
}
}
}
THANK YOU