JDBC

Java Database Connectivity (JDBC) is a Java-based API that enables Java applications to interact with relational databases. It provides a standard interface for connecting to databases, executing SQL queries, and processing the results.

Key Features

  • Database Independence: JDBC provides a database-agnostic way to interact with various relational databases.

  • Transaction Management: Supports transaction management to ensure data consistency.

  • Error Handling: Robust error handling through SQLExceptions.

  • Security: Supports secure communication with databases.

  • Scalability: Suitable for small-scale applications to large enterprise-level systems.

JDBC Architecture

JDBC architecture consists of the following layers:

  • JDBC API: Interface specifications that define how Java applications interact with databases.

  • Driver Manager: Manages a list of database drivers. It selects an appropriate driver from the list.

  • Driver: Implements the details of how to connect to a specific database.

JDBC Drivers

JDBC drivers are platform-specific implementations that enable Java applications to communicate with databases. There are four types of JDBC drivers: Type-1, Type-2, Type-3, and Type-4.

  • Type-1 Driver: Also known as JDBC-ODBC bridge driver.

  • Type-2 Driver: Native-API driver.

  • Type-3 Driver: Network Protocol driver.

  • Type-4 Driver: Thin driver or Direct-to-Database driver.

Choose the appropriate driver based on your application's requirements.

Establishing Database Connection

To establish a connection, use the Connection interface. Example:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectToMySQL {
    public static void main(String[] args) {
        Connection connection = null;

        try {
            // JDBC URL, username, and password of MySQL server
            String url = "jdbc:mysql://localhost:3306/your_database_name";
            String user = "your_username";
            String password = "your_password";

            // Establishing the connection
            connection = DriverManager.getConnection(url, user, password);

            // Checking if the connection is successful
            if (connection != null) {
                System.out.println("Connected to the database!");
            } else {
                System.out.println("Failed to connect to the database.");
            }

        } catch (SQLException e) {
            // Handle any SQL errors
            e.printStackTrace();
        } finally {
            try {
                // Close the connection
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

7 Steps to Execute a Query

  1. Establishing a Connection: Connects to the MySQL database using the JDBC URL, username, and password.

  2. Creating a Statement: Creates a Statement object for executing SQL queries.

  3. Creating a Table: Creates a table named "users" if it doesn't exist.

  4. Inserting Data: Inserts a record into the "users" table.

  5. Retrieving Data: Executes a SELECT query and retrieves data from the "users" table.

  6. Displaying Results: Prints the retrieved data.

  7. Closing Resources: Closes the ResultSet, PreparedStatement, Statement, and Connection to release resources.

Last updated