JDBC is an API, which is used to interact with relational database using java program.
It has interfaces and classes. It can be used to connect to any kind of database.
There are 4 types of drivers available.
1. JDBC-ODBC Bridge Driver(Type-1 driver)
2. Native API Partly Java Driver(Type-2 driver)
3. Net protocol pure Java Driver(Type-3 driver)
4. Native protocol Pure Java Driver(Type-4 driver)
1. JDBC-ODBC Bridge Driver (Type-1 driver): Works as a bridge between
JDBC API and ODBC API. It uses Microsoft's Open database connectivity (ODBC),
which is an API written in C language.
Java Code (JDBC API)'JDBC-ODBC bridge driver'ODBC API/Layer'Database.
Jdbc-Odbc bridge translates JDBC calls to Odbc calls and sends to ODBC
using Odbc libraries.
Example using this driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//To use this driver first we need to load the driver class //
Connection con = DriverManager.getConnection("jdbc:odbc:dsnname",username,pwd);//
// dsnname is the name of the dsn. This is used to establish the connection.//
// username and password are optional.//
Statement stmt = con.createStatement();
//Statement object is created using Connection object.
ResultSet rs = stmt.executeQuery("Select * from EMP");
//Here statement objects method "executeQuery()" takes String parameter,
//which is an sql query. Here after we can process the ResultSet.
con.close().
// This is used to close the connection.//
Using this driver has advantages and disadvantages.
Using Type -1 driver any database can be accessed.
Type - 1 driver is not portable as it is not written in java;
client must be installed with ODBC driver to use this driver.
This driver is the slowest one among the other drivers as there
are few translations like the JDBC call need to go through the bridge
and then ODBC driver and then to the database. Same in reverse also,
so this lead to performance issue.