Java SQL for Database Connectivity | Generated by AI

Home 2025.08

The java.sql package is a core part of Java’s JDBC API (Java Database Connectivity), enabling Java applications to interact with relational databases like MySQL, PostgreSQL, Oracle, SQL Server, and others. It provides the interfaces and classes required to connect to a database, execute SQL queries, and retrieve results.


📘 Overview of java.sql

The java.sql package includes classes and interfaces to:


🔧 Setup: What You Need

1. JDBC Driver

2. Database URL

Each JDBC driver has a connection URL format:

jdbc:mysql://localhost:3306/mydatabase
jdbc:postgresql://localhost:5432/mydatabase

🧩 Key Classes and Interfaces

🔌 1. DriverManager

Establishes a connection to a database.

Connection conn = DriverManager.getConnection(url, user, password);

🧵 2. Connection

Represents a session with a database.

Connection conn = DriverManager.getConnection(...);
conn.setAutoCommit(false);  // for manual transaction control

📤 3. Statement / PreparedStatement / CallableStatement

Statement

Used for executing static SQL queries.

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

PreparedStatement

Used for executing parameterized queries. Avoids SQL injection.

PreparedStatement ps = conn.prepareStatement("SELECT * FROM users WHERE id = ?");
ps.setInt(1, 101);
ResultSet rs = ps.executeQuery();

CallableStatement

Used for stored procedures.

CallableStatement cs = conn.prepareCall("{call getUser(?)}");
cs.setInt(1, 5);
ResultSet rs = cs.executeQuery();

📥 4. ResultSet

Holds the result of a query.

while (rs.next()) {
    int id = rs.getInt("id");
    String name = rs.getString("name");
}

📚 5. Metadata

DatabaseMetaData dbMeta = conn.getMetaData();
ResultSetMetaData rsMeta = rs.getMetaData();

🧪 Basic Example

import java.sql.*;

public class JdbcExample {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/testdb";
        String user = "root";
        String password = "password";

        try (Connection conn = DriverManager.getConnection(url, user, password)) {
            String sql = "SELECT * FROM users WHERE age > ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, 18);

            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getInt("id") + " - " + rs.getString("name"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

🔁 Transactions

conn.setAutoCommit(false);  // Disable auto-commit

try {
    PreparedStatement ps1 = conn.prepareStatement("UPDATE accounts SET balance = balance - ? WHERE id = ?");
    ps1.setDouble(1, 100.0);
    ps1.setInt(2, 1);
    ps1.executeUpdate();

    PreparedStatement ps2 = conn.prepareStatement("UPDATE accounts SET balance = balance + ? WHERE id = ?");
    ps2.setDouble(1, 100.0);
    ps2.setInt(2, 2);
    ps2.executeUpdate();

    conn.commit(); // Commit the transaction

} catch (SQLException ex) {
    conn.rollback(); // Roll back on error
}

Exception Handling

try {
    // DB code
} catch (SQLException ex) {
    System.err.println("Error Code: " + ex.getErrorCode());
    System.err.println("SQL State: " + ex.getSQLState());
    ex.printStackTrace();
}

🧰 Useful Features in java.sql

Feature Interface/Class Purpose
Metadata DatabaseMetaData DB version, supported features, tables, etc.
Result column info ResultSetMetaData Column count, type, label, etc.
BLOB/CLOB Handling Blob, Clob Store/retrieve large binary/text data
Batch Execution addBatch(), executeBatch() Execute many statements together
Scrollable ResultSet ResultSet.TYPE_SCROLL_INSENSITIVE Move cursor back and forth
Updatable ResultSet ResultSet.CONCUR_UPDATABLE Modify rows directly in ResultSet

📦 Maven Dependency Example (MySQL)

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.33</version>
</dependency>

🧠 Best Practices


🧮 Advanced Topics


🧾 Summary Table

Task Class/Interface
Connect to DB DriverManager, Connection
Execute SQL Statement, PreparedStatement
Stored Procedures CallableStatement
Read Results ResultSet
DB Info DatabaseMetaData
Column Info ResultSetMetaData
Transactions Connection.commit(), rollback()
Exception Handling SQLException

If you’d like, I can also give:

Let me know!


Back Donate