After knowing what is JDBC in JDBC tutorial, let us know drivers and JDBC Driver Types as they are required in each JDBC programming.
What is a driver and JDBC Driver Types?
Driver is a software which connects two dissimilar software components or software and hardware. Dissimilar (or heterogeneous) means, they differ in each and every respect. For example, load an operating system and connect a mouse. You will not see a mouse cursor on the monitor as mouse is not recognised by the OS. Now you require a driver as mouse and OS are heterogenous – mouse is hardware having weight and appearance and OS is a software without shape and weight. With the mouse, the manufacturer supplies a driver CD. Connect the mouse and just run the CD, now the mouse is recognized by the OS.
Similarly, Java and database (say, Oracle) are two dissimilar software components. Java is a programming language used to develop software and Oracle is a database used to store data. Even the companies who developed them are different (Sun Microsystems and Oracle corporation; by the time JDBC was introduced, Java is not acquired by Oracle corporation). Now you feel the necessity of a driver. The driver here is called as JDBC driver.
Coming to JDBC drivers, Sun Microsystems identified 4 types of drivers. Each of these types meets a different application need and also differ in their performance.
All the JDBC Driver Types are discussed here under with diagrams.
- Type 1: JDBC-ODBC Bridge, plus ODBC driver
- Type 2: Native-API, partly-Java driver
- Type 3: JDBC-net, pure Java driver
- Type 4: Native-protocol, pure Java driver
Type 1: JDBC-ODBC Bridge, plus ODBC Driver
This first type of bridge driver is supported by the JDBC through JdbcOdbcDriver class from sun.jdbc.odbc package. This driver bridges with another driver (ODBC driver) to connect to a database. The disadvantage with this driver is the database client code and ODBC driver must be installed on every client in an office. This is quiet easy where systems are confined to a limited area but difficult when the systems are spread apart. By performance it is the poorest of all four types as it requires many translations between the JDBC call and finally the SQL statement executed on the database.
By performance it is very very slow. This is mostly used in colleges while learning JDBC but not in realtime (in realtime, Type 4 driver is used). This driver is used with DSN.
Type 2: Native-API Driver, partly- Java driver
The native-API driver converts JDBC commands into DBMS-specific native calls. This is much like the restriction of Type 1 drivers. The client must have some binary code loaded on its machine. These drivers have an advantage over type 1 drivers, because they interface directly with the database.
Type 3: JDBC – Net, pure Java Driver
The JDBC-Net drivers are a three-tier solution. This type of driver translates JDBC calls into a database-independent network protocol that is sent to a middleware server. This server, then translates this DBMS-specific protocol, which is sent to a particular database. The results are routed back through the middleware server and sent back to the client. This type of solution makes it possible to implement a pure Java client. It also makes it possible to swap databases without affecting the client. This is by far the most flexible JDBC solution. The following figure shows this three-tier solution.
Type 4: Native-Protocol, pure Java Driver (known as thin driver)
By performance-wise it is the most preferred and is the one used in real time. Performance is due to the direct conversion of JDBC queries into the database-specific network protocol and additional transformation of middle layers is eliminated. With this driver, the client (in 3-tier, the client for a database server is an application server) can connect to the database directly and is the ideal choice in Intranets. This native-protocol driver is database dependent and is generally supplied by the database vendors itself (many times, free of cost). This driver is written entirely using Java language and is commonly known as "thin driver" as less code is executed and less translations exists (directly translates Java calls to SQL statements). The following diagrams communications of a Type 4 driver.
This driver is very fast as it executes less code to communicate. For this reason, it is known as thin driver. It can be used with connection pooling.
What is DSN and creating DSN is given in Creating Data Source Name (DSN) JDBC.
How to select the right JDBC Driver Type?
It depends on the requirements of your particular project. If you do not have the opportunity of inclination to install and configure software on each client, you can rule out Type 1 and Type 2 drivers. However, the cost of Type 3 and Type4 drivers is prohibitive, Type 1 and Type 2 drivers may become more attractive because they are usually available free of charge. The selection of Type 3 and Type4 drivers for a particular application depends on the benefits of flexibility and interoperability against performance. Type 3 drivers offer your application the ability to transparently access different types of databases, while type 4 drivers usually exhibit better performance most frequently used in realtime. Type 4 can be used with frameworks like Hibernate.