JDBC Introduction


JDBC is a Java API that allows Java applications to establish database connections and execute SQL queries to perform data manipulation.


Key Features

  • Platform-independent database interaction
  • Part of java.sql and javax.sql packages
  • It enables SQL operations for creating, reading, updating and deleting database records.
  • JDBC supports connections to any relational database through the use of JDBC drivers.

jdbc


JDBC Architecture

1. JDBC API

Provides interfaces and classes for database communication.


2. JDBC Driver

Translates Java calls to native DB calls. Types:

  • Type 1: JDBC-ODBC bridge (obsolete)
  • Type 2: Native API driver
  • Type 3: Network Protocol driver
  • Type 4: Thin driver (pure Java, most common)
jdbc-flow

Steps to Use JDBC

Step 1: Import JDBC packages

import java.sql.*; 

Step 2: Load JDBC driver

Class.forName("com.mysql.cj.jdbc.Driver"); 

notepad

Use the correct driver class for your DB. Example: oracle.jdbc.driver.OracleDriver for Oracle.


Step 3: Establish the Connection

Connection con = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/mydatabase", "username", "password");

Step 4: Create a Statement

Statement stmt = con.createStatement();

Step 5: Execute SQL queries

ResultSet rs = stmt.executeQuery("SELECT * FROM users");

while (rs.next()) {
    System.out.println(rs.getInt("id") + " " + rs.getString("name"));
}

Step 6: Close the Connection

con.close();

Example

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/testdb", "root", "password");

            // Step 3: Create statement
            Statement stmt = con.createStatement();

            // Step 4: Execute query
            ResultSet rs = stmt.executeQuery("SELECT * FROM employees");

            // Step 5: Process result
            while (rs.next()) {
                System.out.println(rs.getInt(1) + " " + rs.getString(2));
            }

            // Step 6: Close connection
            con.close();

        } catch (Exception e) {
            System.out.println(e);
        }
    }
} 

JDBC Interfaces

Interface Description
Connection Connects to the DB
Statement Executes static SQL statements
PreparedStatement Precompiled SQL with parameters
ResultSet Holds data returned from a query
DriverManager Manages JDBC drivers



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.