Java Database Connectivity (JDBC)

Chapter 14: Java Database Connectivity (JDBC) - Java Programming Guide

Chapter 14: Java Database Connectivity (JDBC)

Java Database Connectivity (JDBC) is a Java API that enables Java programs to interact with relational databases. It provides methods to query and update data in a database using Java.

🔍 What is JDBC?

JDBC stands for Java Database Connectivity. It is a set of Java APIs that allow Java applications to connect and execute queries with databases like MySQL, Oracle, PostgreSQL, etc.

🛠 Features of JDBC

  • Platform-independent database access
  • Standard Java API
  • Supports both procedural and object-oriented access
  • Connects with multiple types of databases

🧱 JDBC Architecture

The JDBC architecture consists of two layers:

  • JDBC API – Provides the application-to-JDBC Manager connection
  • JDBC Driver API – Provides the JDBC Manager-to-Driver connection

🔗 JDBC Components

  1. DriverManager – Manages a list of database drivers.
  2. Connection – Interface for a connection to a specific database.
  3. Statement – Used to execute SQL queries.
  4. ResultSet – Represents the result returned from a query.
  5. SQLException – Handles database errors.

🪜 Steps to Connect Java with a Database using JDBC

  1. Load the JDBC Driver
  2. Establish the connection
  3. Create a statement
  4. Execute the query
  5. Process the results
  6. Close the connection

💡 Example: Connect Java to MySQL Database


import java.sql.*;

public class JdbcExample {
    public static void main(String[] args) {
        try {
            // Load Driver
            Class.forName("com.mysql.cj.jdbc.Driver");

            // Establish Connection
            Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/testdb", "root", "password");

            // Create Statement
            Statement stmt = con.createStatement();

            // Execute Query
            ResultSet rs = stmt.executeQuery("SELECT * FROM students");

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

            // Close Connection
            con.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

✔️ Best Practices

  • Always close connections in a finally block
  • Use PreparedStatement to avoid SQL injection
  • Use connection pooling for efficiency
  • Handle exceptions properly

📌 JDBC Driver Types

  1. Type-1: JDBC-ODBC bridge driver
  2. Type-2: Native-API driver
  3. Type-3: Network Protocol driver
  4. Type-4: Thin driver (pure Java driver)

❓ FAQs with Answers

Q1: What is JDBC in Java?

A: JDBC stands for Java Database Connectivity. It is a Java API used to connect and interact with databases from Java programs.

Q2: How many types of JDBC drivers are there?

A: There are 4 types: Type-1 (JDBC-ODBC Bridge), Type-2 (Native-API), Type-3 (Network Protocol), and Type-4 (Thin Driver).

Q3: What is the difference between Statement and PreparedStatement?

A: Statement is used for static SQL queries, while PreparedStatement is used for dynamic and parameterized queries and provides protection from SQL injection.

Q4: Which JDBC driver is best for MySQL?

A: The Type-4 Thin driver (`com.mysql.cj.jdbc.Driver`) is most commonly used for connecting Java applications to MySQL.

Q5: How to handle exceptions in JDBC?

A: Use try-catch blocks to handle SQLExceptions, and always ensure the connection is closed in a `finally` block to prevent memory leaks.


🏁 Conclusion

JDBC is a powerful API that enables Java developers to connect and manipulate relational databases. Understanding JDBC is essential for developing enterprise-level Java applications.

Labels: Java Programming, JDBC, Java with Database, JDBC Tutorial, Java Chapter 14

📌 Related Links: Complete Java Course

Comments

Popular posts from this blog

Best AI Tools for Students and Professionals in 2025 | Boost Learning & Productivity

Introduction to Java

Top AI Tools for Content Creators: Bloggers, YouTubers & Writers (2025)