Recent Pos

Working With JDBC Driver API

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmail

jdbc api driver example
The following steps are involved in working with JDBC(Java Database Connectivity) API accessing the database server:

Step 1: Obtain Connection of DB.

Step 2: Get a JDBC Statement.

Step 3: Execute the Statement.

Step 4: Close the Connection.

Step 1: Obtaining the Connection to DB:

⇒ Any application to access the database server requires to open a session to the DB before any other request.

⇒ The Driver object is a JDBC driver object responsible for establishing connection to the database server.

⇒ The java.sql.Driver is an interface part of JDBC API designed to describe Driver object.

⇒ The Driver object supports the following method for opening a new session:

⇒ The above method is used to request JDBC driver for creating a new Session to the database server.

⇒ This takes two arguments:

1. The JDBC URL(Uniform Resource Locator) as String : The driver object connect method is used to establish a session to the server, and to do this it requires the location of the server. The first argument of this method takes the JDBC URL which will describe the location of the server to create the session.

Format of the JDBC URL:

Note: The JDBC URL is driver dependent, it changes from one driver to other.

2. java.util.Properties: The database server requires zero or more details such as username and password for creating a session. And to submit these details we use the second argument of connect() method.

Properties: The java.util.Properties is a Java Collections Map implementation. This allows to store key-value pairs. Each key-value pair is known as property.

⇒ The following method of properties object is used to store the key-value pair:

⇒ And the following is used to read the Property value:

Note: The keys are case sensitive.

⇒ In case if the properties object doesn’t finds the property with the given key it returns null.

⇒ One of the approaches to obtain the Connection is using the Driver object directly. In this approach we use the following two steps to obtain the Connection:

Step 1.1: Create the Driver object

Step 1.2: Invoke the connect() method of Driver object

Step 1.1 – Creating the Driver object:-

⇒ As we know that we use “new” keyword for creating a new object in Java.

⇒ Thus we can use new keyword here to create the Driver object.

Example:

⇒ The JdbcOdbcDriver is the name of the Driver class in the package “sun.jdbc.odbc”. This is a Driver implementation for JDBC Type-1 Driver.

⇒ Similarly we have Driver implementation class for every JDBC Driver, examples:

⇒ That means the driver implementation class names are different for each JDBC Driver.

⇒ We know that using the new keyword requires to be hard-coded the name of the class into the program, which makes the application depend on driver. That is if we want to change from one JDBC driver to other we need to do the modifications in program and recompile and redeployment.

⇒ To solve this problem we can use the Java Reflection API (java.lang.Class) to create the Driver object.

Prerequisites:

⇒ The java.lang.Class object represents the class/interface loaded into the JVM.

⇒ We can use the following static method of java.lang.Class to explicitly load the given class into the JVM:

Example:

⇒ This loads the Employee class in case if it is not earlier loaded into this JVM. Returns the class object representing the Employee class loaded.

⇒ We can use the following method of Class object to create a new instance of the Class representing by this class object.

Example:

⇒ At Step A (i.e. when calling forName() method it checks whether the class Employee is loaded into the JVM, if not loaded load it now creating the Class object, if already loaded get the Class object created for Employee earlier. Finally return the Class obejct representing the Employee class. In case of any problem throw exception.

⇒ At Step B (i.e. invoking newInstance() method) this will create an instance of the class representing by this class object. That is in the above example this will create an object of Employee class.

⇒ Once the Driver object is created we now can invoke the connect() method for obtaining the Connection.

⇒ We know that to call the connect() method we need a JDBC URL and DB connection Properties as input.

⇒ The JDBC URL for JDBC-ODBC bridge driver is as shown below:

⇒ When working with ODBC we can configure the datasource details with a unique name(i.e. Data Source Name DSN) supporting the ODBC DriverManager for accessing the data store.

⇒ To configure the ODBC DataStroe we can use the ODBC DataSource Administration tool which can be found in the “Control Panel”->”Administrative Tools” of Windows Machine.

Example for demonstrating creating the Connection using Driver Object (Approach 1):

//Step 1: Create a Driver object
//Approach A – Use a new keyoword
//Approach B – Use newInstance() method of Class object.

//Step 2: Invoke connect() method
//Create a Properties object setting the DB Connection properties

Example:

 
 
Program to create a Connection using Oracle Type-2 Driver (JDBC OCI Driver):

⇒ To run this program that is (use Oracle JDBC Type-2 Driver) we need to following configurations.

1. Set ojdbc14.jar or ojdbc5.jar or ojdbc6.jar into classpath.

⇒ The ojdbc5.jar and ojdbc6.jar comes with Oracle 11g, implements JDBC 4.0.

⇒ ojdbc14.jar implements JDBC 3.0

Using Oracle Type-4 Driver (Oracle Thin Driver):

⇒ The Driver class name for the Oracle Type-4 driver is also same as of Oracle Type-2 Driver.

⇒ Only the change we find is the JDBC URL. The JDBC URL for this driver is as shown below:

Example:

⇒ To work with this driver we don’t require to have the bin setting into path.

⇒ Lets consider the Oracle database is installed in the machine with an IP address 192.168.10.21 and the hostname is “lab21” then the JDBC URL will be as shown below:

OR

<-Previous: Understanding The JDBC Driver Types Architecture | | Next->DAO (Data Access Object) pattern implementation using JDBC

No Comments

Post a Comment