Let us do get a cover overview of the JDBC and the ODBC prior in order to better understand what exactly is type 4 driver. A JDBC driver enables Java application to interact with a database from where we can fetch or store data. JDBC drivers are analogous to ODBC drivers. The JDBC classes are contained in the Java Package java.sql and javax.sql.JDBC helps to
- Connect to a data source, like a database.
- Send queries and update statements to the database
- Retrieve and process the results received from the database in answer to your query
The Java.sql package that ships with JDK, contains various classes with their behavior defined, and their actual implementations are done in third-party drivers. Third-party vendors implement the java.sql.Driver interface in their database driver.
ODBC is based on the device driver model, where the driver encapsulates the logic needed to convert a standard set of commands and functions into the specific calls required by the underlying system.
JDBC driver types are used to categorize the technology used to connect to the database.
- Type -1 Bridge driver
- Type -2 Native API
- Type -3 Network Protocol
- Type -4 Native Protocol
Type-4 driver is also called native protocol driver. This driver interacts directly with the database. It does not require any native database library, that is why it is also known as Thin Driver.
- Does not require any native library and Middleware server, so no client-side or server-side installation.
- It is fully written in Java language, hence they are portable drivers.
Type-4 JDBC driver also known as ‘thin driver’ or Direct to Database Pure Java Driver. It is portable, the fastest among all JDBC drivers and database dependent.
The thin driver converts JDBC calls directly into the vendor-specific database protocol. It is fully written in Java language. Also, it is a platform-independent driver but it is database dependent as it uses a native protocol(Protocol can establish a connection between particular server only).
Thin driver installs inside Java Virtual Machine of the Client.
- No special software on the client or server needed. No translation or middleware layers are used, improving performance.
- Platform independent (Completely coded in Java)
- No need to install native libraries.
- It uses a database server-specific protocol which makes it secure.
- Helpful in debugging as JVM can manage all aspects of the application-to-database connection.
- It uses database-specific protocol and it is DBMS vendor dependent
- Scalable
- Advanced system administration
- Superior performance
- Advance Java feature set
- Offers significantly better performance than the JDBC/ODBC Bridge and Type 2 Drivers
If you are accessing one type of database, such as Oracle, Sybase, or IBM, the preferred driver type is type-4.
Unicode supported
Multi-lingual applications can be developed on any operating system platform with JDBC using the Type 4 JDBC drivers to access both Unicode and non-Unicode enabled databases. Internally, Java applications use UTF-16 Unicode encoding for string data. When fetching data, the Type 4 JDBC drivers automatically perform the conversion from the character encoding used by the database to UTF-16. Similarly, when inserting or updating data in the database, the drivers automatically convert UTF-16 encoding to the character encoding used by the database.
Lets us do discuss the error handling in type 4 JDBC drivers as type 4 JDBC drivers are reported throwing errors to the calling application by throwing SQLExceptions. This is because each SQLException contains:
- Native error code
- Description of cause of the error
- A string containing the XOPEN SQLstate