Recent Pos

DAO (Data Access Object) pattern implementation using JDBC

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmail

dao implementation using jdbc
⇒ It is one of the core design patterns of OOP (Object Oriented Programming) model. The JDBC DriverManager is an implementation of DAO design pattern.

⇒ In many projects we find a requirement of accessing multiple data stores and even need for migrating from one database to other.

Problem:

⇒ We want to separate the persistence logic from business logic.

Solution:

⇒ Implement a special object with all the persistence logic separating from business logic. This special object is referred as DAO.

Benefits of using this pattern:

⇒ Easy development

⇒ Proper responsibility division

⇒ Cost management

⇒ Easy migration

⇒ Better testing support

⇒ Loose coupling with data stores

dao implementation using jdbc

Approach 2: For obtaining the Connection Using the DriverManager:

⇒ In the earlier approach we found using the Driver object to create the Connection.

Note: A single instance of Driver object is enough to create multiple connections.

⇒ The java.sql.DriverManager is a factory class that can manage the Driver object for creating the Connections.

⇒ Using the DriverManager can benefit us managing the Driver object throughout the system creating the Connection at multiple points.

⇒ The following two steps are involved in using DriverManager for creating the Connection:

Step 1.1:- Register the Driver object

Step 1.2:- Invoke getConnection() method

1.1 – Registering the Driver object:

The following static method of DriverManager class is used to register the Driver object:

⇒ This method will add the given Driver object to the List managed by the DriverManager.

Internal code of DriverManager:

Note: JDBC specification has a rule to the 3rd party vendors, the Driver implementation class should have a static block which needs to create an object of itself and register to DriverManager.

Example:

-That means loading the Driver class into the JVM will result into registering the respective Driver class obejct to the DriverManager.

Note: (New in JDBC 4.0)
⇒ From JDBC 4.0 we dont have to explicitly register the DriverObject, the DriverManager can get the Driver objects from the classpath and register automatically.

1.2 – Getting the Connection:

⇒ After the Driver object is registered to the DriverManager we can use any of the following static methods to get the Connection.

⇒ The first two getConnection() methods will internally create the Properties and call the third getConnection() method.

⇒ The getConnection() method of DriverManager will internally use the registered Driver object to create the Connection. The internal code of this method in DriverManager will be as shown below:

⇒ Program to test creating the Connection using DriverManager:
 

<-Previous: Working With JDBC Driver API | | Next->The JDBC Statement PreparedStatement and CallableStatement Object

No Comments

Post a Comment