What is Dynamic Binding (Late Binding)?
Dynamic Binding also known as Late Binding is a concept in Java where the actual method implementation is decided at runtime instead of compile time. Java decides which method should execute when the program is running.
Example Imagine you are using a mobile phone and connecting it to different Bluetooth devices such as headphones, speakers or smartwatches. Your phone does not need to know the internal working of every device. It uses a common way to connect and each device handles the connection according to its own implementation.
Why Do We Use Dynamic Binding in JDBC?
When we connect a Java application to a database like PostgreSQL or MySQL we use JDBC (Java Database Connectivity). JDBC provides common interfaces that help Java applications communicate with different databases.
We are developing a college management application. Initially we use PostgreSQL as our database. Later the company decides to change the database to MySQL. Instead of writing completely different connection code we can use the common JDBC interfaces and change the database driver and connection details.
This is one of the reasons JDBC is flexible.
How Does It Work?
First we need to understand that Connection is an interface in Java JDBC. An interface defines methods but it does not provide the complete database specific implementation. The PostgreSQL JDBC driver provides its own implementation of the Connection interface.
When we call DriverManager.getConnection() JDBC identifies a suitable driver based on the database URL and asks that driver to establish the connection. The driver returns a connection object.
Connecting Java with PostgreSQL
Step 1: Import JDBC packages
java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
These imports allow us to use JDBC classes and interfaces. Connection represents the database connection DriverManager helps obtain the connection and SQLException handles database related errors.
Step 2: Create the database connection
java
public class DatabaseConnection {
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost:5432/college";
String username = "postgres";
String password = "password";
try {
Connection con = DriverManager.getConnection(url, username,password);
System.out.println("Database Connected Successfully");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
Explanation of the Code
We create a database URL that contains the database type server address, port number and database name. Here jdbc:postgresql tells JDBC that we want to connect to PostgreSQL. localhost means the database is running on our own computer 5432 is the default PostgreSQL port and college is the database name.
Next we provide the username and password required to access the database.
After that we use DriverManager.getConnection() to establish the connection. This method identifies the suitable JDBC driver and asks it to connect to the database.
The returned object is stored in the con variable whose type is Connection. Even though we use the common Connection interface the actual connection implementation is provided by the PostgreSQL JDBC driver.
If the connection is successful the program prints a success message. If something goes wrong the catch block handles the database error.
Top comments (0)