Java Program to Connect Database using JDBC API


JDBC stands for Java Database Connectivity. It is a Java API that manages connecting to a database, issuing queries and commands, and handling result sets obtained from the database. JDBC was one of the earliest libraries developed for the Java language.

Java Program to Connect Database using JDBC API
JDBC was initially conceived as a client-side API, enabling a Java client to interact with a data source. With JDBC 2.0, which included an optional package supporting server-side JDBC connections. Every new JDBC release since then has featured updates to both the client side.

Steps to Connect Database using JDBC API:

i. Install or locate the database you want to access.
ii. Include the JDBC library.
iii. Ensure the JDBC driver you need is on your classpath.
iv. Use the JDBC library to obtain a connection to the database.
v. Use the connection to issue SQL commands.
vi. Close the connection when you are finished.

JDBC Connection in Java example:

import java.sql.*;
public class JDBC_Connection {
static final String DB_URL = "jdbc:mysql://localhost/WEBEDUCLICK";
static final String USER = "guest";
static final String PASS = "PassW0rd";
static final String QUERY = "SELECT id, first, last, age FROM Employees";
public static void main(String[] args) {
try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(QUERY);) {
while ( {
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", Age: " + rs.getInt("age"));
System.out.print(", First: " + rs.getString("first"));
System.out.println(", Last: " + rs.getString("last"));
} catch (SQLException e) {