Introduction-to-JDBC
JDBC is an API (Application Programming Interface) that allows Java applications to connect with databases. It enables Java programs to execute SQL queries, update records, and retrieve data from relational databases like MySQL, PostgreSQL, etc.
- JDBC was developed by Sun Microsystems.
- It provides a standard way to connect Java application with multiple database.
Simple Structure of JDBC
Steps to Start a JDBC Project
1. Download and install MySQL software.
2. Download MySQL connection Jar files.3. Open Eclipse or any IDE to create a Java project.
4. Create Java build path: Right click ---> properties ---> libraries ---> add external jar file(MySQL-connector-Java) ---> apply and close.
5. Create a new package and class.
JDBC Steps for Connecting Java with MySQL
To connect Java applications with MySQL database we need to follow five steps
A driver translate Java code into database-specific commands.
2. Establish a connection between the Java application and MySQL database.
3. Prepare SQL statement.
4. Execute statement.
5. Close connection.
A driver is used to translate Java to database specific language. It is dependent on a database, for each database, there is a separate Driver.
Driver Manager is used to establish connections between Java applications to databases, and it is independent.
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) {
try {
// Step 1: Load the MySQL JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Loaded Successfully!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
2. Establish a connection between the Java application and MySQL database.
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) {
try {
// Load Driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Step 2: Establish Connection
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydatabase", "root", "password");
System.out.println("Connected to Database!");
con.close(); // Close connection
} catch (Exception e) {
e.printStackTrace();
}
}
}
- Change mydatabase with your own database name.
- Change root and password with your MySQL credentials.
Statement stmt = con.createStatement();
String query = "INSERT INTO students (id, name, age) VALUES (1, 'John', 22)";
stmt.executeUpdate(query);
System.out.println("Data Inserted Successfully!");
4. Execute statement.
// SELECT Query
String sql = "SELECT * FROM students";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") +
", Name: " + rs.getString("name") +
", Age: " + rs.getInt("age"));
}
5. Close connection.
con.close();
System.out.println("Connection Closed.");
What is Driver and Driver Manager ?
Driver
A driver is used to translate Java to database specific language. It is dependent on a database, for each database, there is a separate Driver.
Driver Manager
Driver Manager is used to establish connections between Java applications to databases, and it is independent.
Example: Complete JBDC of working program to connect Java with MySQL, insert a record, and fetch data.
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) {
try {
// Step 1: Load Driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Step 2: Establish Connection
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydatabase", "root", "password");
// Step 3: Prepare SQL Statement
Statement stmt = con.createStatement();
String insertQuery = "INSERT INTO students (id, name, age) VALUES (1, 'Alice', 23)";
stmt.executeUpdate(insertQuery);
System.out.println("Data Inserted Successfully!");
// Step 4: Execute SELECT Query
String selectQuery = "SELECT * FROM students";
ResultSet rs = stmt.executeQuery(selectQuery);
// Display Results
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") +
", Name: " + rs.getString("name") +
", Age: " + rs.getInt("age"));
}
// Step 5: Close Connection
con.close();
System.out.println("Connection Closed.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Two Minute Drill
- JDBC (Java Database Connectivity) enables Java applications to connect with databases.
- It follows 5 key steps: Load Driver --> Connect --> Prepare Query --> Execute --> Close.
- Use DriverManager.getConnection() to connect to a database.
- Use Statement or PreparedStatement for executing SQL queries.