JDBC Driver
JDBC Driver
JDBC Driver
JDBC Drivers
JDBC-ODBC bridge driver
Native-API driver
Network Protocol driver
Thin driver
JDBC Driver is a software component that enables java application to interact with the database.
There are 4 types of JDBC drivers:
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC
bridge driver converts JDBC method calls into the ODBC function calls. This is now discouraged
because of thin driver.
Advantages:
Easy to use.
Can be easily connected to any database.
Disadvantages:
2) Native-API driver
The Native API driver uses the client-side libraries of the database. The driver converts JDBC
method calls into native calls of the database API. It is not written entirely in java.
A
dvantage:
Disadvantage:
The Native driver needs to be installed on the each client machine.
The Vendor client library needs to be installed on client machine.
The Network Protocol driver uses middleware (application server) that converts
JDBC calls directly or indirectly into the vendor-specific database protocol. It is
fully written in java.
Advantage:
Disadvantages:
4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is
why it is known as thin driver. It is fully written in Java language.
Advantages:
Disadvantage:
There are 5 steps to connect any java application with the database using JDBC.
These steps are as follows:
The forName() method of Class class is used to register the driver class. This method is used to
dynamically load the driver class.
Class.forName("oracle.jdbc.driver.OracleDriver");
The getConnection() method of DriverManager class is used to establish connection with the
database.
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");
The createStatement() method of Connection interface is used to create statement. The object of
statement is responsible to execute queries with the database.
Statement stmt=con.createStatement();
The executeQuery() method of Statement interface is used to execute queries to the database. This
method returns the object of ResultSet that can be used to get all the records of a table.
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
By closing connection object statement and ResultSet will be closed automatically. The close()
method of Connection interface is used to close the connection.
con.close();
Create a Table
we are connecting to an Oracle database and getting data from emp table. Here,
system and oracle are the username and password of the Oracle database.
import java.sql.*;
class OracleCon
{
public static void main(String args[])
{
Try
{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Two-Tier Client/Server Model and Three-Tier Client/Server Model:
The Client-Application contains the codes for interfacing with the user and also
for saving data in the database server. The Client-Application sends the request
to the server and it processes the request and sends back with data. This means
client Application handles both the Presentation layer (application interface)
and the Application layer (logical operations). The client-Application layer can
be build using languages such as C, C++, Java, Python, PHP, Rails, .NET.
On the other hand, the database server handles the data management layer. Data
management layer consists of data storage(database or file system) and methods
for storing and retrieving data from the data storage. Commonly used databases
include MySQL, MongoDB, PostgreSQL, SQLite. Hosting is possible on-
premises or in the cloud.
Advantages:
The Application tier: it is the middle layer. This layer contains business rules
and logical operations for performing tasks like processing commands,
validation, logical evaluations, decision making, database interactions, and web
applications structuring. It can be implemented using frameworks such as
Django, Rails, Spring, Laravel, .NET. Javascript too can be used when executed
on Node.js. Deployment is possible on distributed or dedicated in-house servers
such as Nginx, Apache, Puma, Microsoft’s Internet Information Server.
The Data tier: it is the back-end layer. It is similar to the database server on
two-tier architecture.
Advantages:
Disadvantage:
Complexity is increased.