When building real-world applications, we often need to connect them with databases to store and retrieve data. In Java, this is achieved using JDBC – Java Database Connectivity.
JDBC is a standard Java API that allows applications to connect and interact with relational databases such as MySQL, PostgreSQL, Oracle, or SQL Server.
What is JDBC?
JDBC (Java Database Connectivity) is:
- A standard Java API for connecting Java applications with relational databases.
- A set of classes and interfaces provided by the Java platform.
- Useful for performing CRUD (Create, Read, Update, Delete) operations in databases.
Steps in JDBC
To connect a Java program with a database using JDBC, we follow 7 main steps:
1. Import the Package
import java.sql.*;
2. Load and Register the Driver
Each database needs a specific driver. Example for MySQL:
Class.forName("com.mysql.cj.jdbc.Driver");
3. Establish a Connection
We connect our Java program to the database using:
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydb", "username", "password");
4. Create a Statement
There are three ways to create statements:
- Statement – used for general SQL queries
- PreparedStatement – used for parameterized queries (more secure)
- CallableStatement – used to call stored procedures
Statement stmt = con.createStatement();
5. Execute the Statement / SQL Query
There are three execution methods:
1.executeQuery() → Used for SELECT queries (DQL).
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
2.executeUpdate() → Used for INSERT, UPDATE, DELETE queries (DML/DDL).
- Returns an int (number of rows affected).
int rows = stmt.executeUpdate("INSERT INTO students VALUES(1, 'Tamil')");
3.execute() → Can run any SQL (returns true/false).
6. Process the Results
When using executeQuery(), results are stored in a ResultSet.
while (rs.next()) {
System.out.println(
rs.getInt("rno") + " " +
rs.getString("name") + " " +
rs.getString("location")
);
}
7. Close the Connection
It’s important to close connections after use.
con.close();
Example
Here is a simple JDBC program that connects to a MySQL database, retrieves records from a table, and displays them:
package jdbcdemo;
import java.sql.*; // 1. import the package
public class JdbcDemo {
public static void main(String[] args) throws Exception {
// 2. load and register the driver
// Class.forName("com.mysql.cj.jdbc.Driver");
// 3. establish a connection
String url = "jdbc:mysql://localhost:3306/jdbcforyoutube";
String username = "root";
String password = "Kts@1812";
Connection con = DriverManager.getConnection(url, username, password);
// 4. create the statement
Statement st = con.createStatement();
// 5. execute the statement
String sql = "select * from student";
ResultSet rs = st.executeQuery(sql);
// 6. process the results
while (rs.next()) {
System.out.println(
rs.getInt("rno") + " " +
rs.getString("name") + " " +
rs.getString("location")
);
}
// 7. close the connection
con.close();
}
}
Summary of JDBC Steps
- Import the package
- Load and register the driver
- Establish a connection
- Create a statement
- Execute the query
- Process the results
- Close the connection