How to Establish a Database Connection in Java Using JDBC (DriverManager Explained)

Establishing a Database Connection in JDBC (Java)

To communicate with a database server from a Java application, the programmer must establish a database connection using JDBC (Java Database Connectivity).

Role of DriverManager and Connection

  • The DriverManager class is present in the java.sql package.
  • It provides static factory methods to create and return an object of the Connection interface.
  • The Connection interface represents an active connection between a Java program and a database server.
👉 The implementation of the Connection interface is provided by the database vendor (such as MySQL, Oracle, PostgreSQL) inside a JDBC driver JAR file (e.g., mysql-connector.jar).

JDBC API vs JDBC Driver

             Component.                                   Description
  • java.sql package.                 Part of JDBC API (available in rt.jar)
  • DriverManager.                     Factory class used to obtain database connections
  • Connection interface.          Represents database connection
  • JDBC Driver JAR.                 Vendor-specific implementation of JDBC interfaces
✅ Note: Since JDBC drivers are third-party software, they must be added to the Java Build Path.

DriverManager as Factory Design Pattern

The DriverManager class follows the Factory Design Pattern because:
  • It hides the object creation logic.
  • It returns different Connection objects based on the database URL and driver.

Methods to Establish Database Connection

The DriverManager class provides three overloaded static methods to establish a database connection:

1️⃣ getConnection(String url)

Connection con = DriverManager.getConnection(url);
  • Uses only the database URL.
  • Credentials may be embedded in the URL.
  • Rarely used in real-world applications.

2️⃣ getConnection(String url, String user, String password)

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

Most commonly used method

  • URL specifies database location.
  • Username and password are passed separately.
  • Preferred for clarity and security.

3️⃣ getConnection(String url, Properties props)

Properties props = new Properties();
props.put("user", "root");
props.put("password", "password");

Connection con = DriverManager.getConnection(url, props);
  • Used when credentials are stored in a properties file.
  • Best practice for enterprise applications.
  • Supports loose coupling and easy configuration changes.

Real-Life Example

🔹 When a banking application needs to fetch account details:

  • Java application sends a request
  • JDBC establishes a connection using DriverManager
  • Database responds with account data
  • Connection is closed after operation

Key Points to Remember

  • JDBC connection is mandatory for database operations.
  • DriverManager creates Connection objects.
  • JDBC drivers are vendor-specific.
  • DriverManager implements Factory Design Pattern.
  • Connection methods are overloaded for flexibility.

Comments

Popular posts from this blog

History of Java Programming Language | Features, Origin & Uses

Inheritance in Java: Types, Examples, and Explanation

Java Programming Features Every Beginner Should Know