Uploaded by ANMOL Sharma

Unit-3

advertisement
UNIT – 3
JAVA DATABASE CONNECTIVITY (JDBC)
Unit-3 Java DataBase Connectivity
• 3.1 Two-Tier Database Design, Three Tier Database Design
• 3.2 The JDBC API: The API components, database operations
like creating tables, CRUD(Create, Read, Update, Delete)
operations using SQL
• 3.3 JDBC- advantages and disadvantages
• 3.4 JDBC drivers
• 3.5 JDBC-ODBC bridge
• 3.6 Develop java program using JDBC
Introduction
• Database:
• Collection of data
• DBMS:
• Database management system
• Storing and organizing data
• SQL:
• Relational database
• Structured query language
• JDBC:
• Java database connectivity
• JDBC Driver
Introduction: JDBC
JDBC (Java DataBase
Connectivity) is used to
connect java application
with database.
It provides classes and
interfaces to connect or
communicate java application
with database.
JDBC is an API used to
communicate
Java
application to database in
database
independent
and platform independent
manner.
DataBase
Example
Oracle
SQl Server
MySQL
MS Access
Introduction: JDBC
• JDBC (Java DataBase Connection) is the standard
method of accessing databases from java application.
• JDBC is a Java API for executing SQL statements.
(standard for Java Database Connectivity.)
• JDBC is a platform independent interface between
relational database and java application.
• The combination of Java and JDBC makes it possible for
a programmer to “write it once and run it anywhere”.
• JDBC consists of a set of classes and interfaces written in
Java for connectivity.
3.1 Two-tier Database Design, Three-tier Database
Design
JDBC Architecture
•The JDBC API supports both two-tier and three-tier processing
models for database access but in general, JDBC Architecture
consists of two layers:
•JDBC API: This provides the application-to-JDBC Manager
connection.
•JDBC Driver API: This supports the JDBC Manager-to-Driver
Connection.
•The JDBC API uses a driver manager and database-specific drivers
to provide transparent connectivity to heterogeneous databases.
•The JDBC driver manager ensures that the correct driver is used to
access each data source.
•The driver manager is capable of supporting multiple concurrent
drivers connected to multiple heterogeneous databases.
3.1 Two-tier Database Design, Three-tier Database
Design
JDBC Architecture
3.1 Two-tier Database Design, Three-tier Database
Design
•Basically, Database application consist of three different
modules.
•User Interface (UI) : This module consist of Graphical User
Interface with which user interacts.
• Business Logic : This module contains code that performs
validation, calculations and various operations on database such
as insert, delete, update or select.
•Database: This modules contains database into which data are
stored and from which data are retrieved.
3.1 Two-tier Database Design, Three-tier Database
Design
Two tier database design
•The two-tier is based on ClientServer architecture.
•The direct communication takes
place between client and server.
•There is no mediator between
client and server.
•In this type of Database design,
User Interface and Business Logic
resides in client side and Database
resides on server side.
3.1 Two-tier Database Design, Three-tier Database
Design
Two tier database design
•In client – server architecture, client sends request to server and
server sends response back to client.
•The client computer directly access the data without seeing the
database. This means that the data on the server is much more
secure.
•In Two – Tier database architecture, the workload is shared
among client and server. The server is processing only database
and client works on the User Interface and Business Logic.
3.1 Two-tier Database Design, Three-tier Database
Design
Two tier database design
Advantages:
•It is easy to implement.
•It is easy to maintain.
•It performs faster means in this architecture communication is faster.
•High portability.
•Systems are accessible by multiple users from any part of the world.
•Modification is bit easy as compare to 3 - tier.
•Due to their less complexity, easy to build and thereby less expensive.
Disadvantages:
•Performance is reduced when clients increase.
•More suitable to departmental applications with small scale groupware and
simple for Web-based.
3.1 Two-tier Database Design, Three-tier Database
Design
Three tier database design
•In this type of Database design, User
Interface resides on client side, Business
Logic resides on separate application
server and Database resides on
Database server.
•In Three – Tier database design, client
communicates with database server
through application server.
•In Three – Tier database design, any
one of the three tiers can be upgraded
or replaced independently.
3.1 Two-tier Database Design, Three-tier Database
Design
Three tier database design
•Three-tier architecture typically
data access tier, and Data tier.
have Presentation tier,
Business
tier or
•Three layers in the three tier architecture are as follows:
1. Client layer (Presentation Layer)
2. Business layer (Application Layer / Middle Tier)
3. Data layer
•Client Layer :
•It is also called as Presentation Layer which contains UI part of our
application.
•This layer is used for the design purpose where data is presented to the
user or input is taken from the user.
•For this, in web communication HTML is used.
3.1 Two-tier Database Design, Three-tier Database
Design
Three tier database design
•Business Layer :
1)In this layer all business logic written like validation of data, calculations,
data insertion etc.
2)It acts as an interface between Client Layer and Data Layer.
3)It is also called the mediator, helps to make communication faster between
client and data layer.
•Data Layer :
1)In this layer actual database is comes in the picture.
2)It contains methods to connect with database and to perform insert,
update, delete, get data from database based on our input data.
3.1 Two-tier Database Design, Three-tier Database
Design
Three tier database design
Advantages:
•Improved Security – Client is not direct access to database.
•Scalability – Each layer is run on different systems.
•Performance is higher than 2-tier.
•High degree of flexibility as an additional tier for integration logic.
•It is fast in communication than 2-tier.
•Improve Data Integrity.
•Easy to maintain and modification is bit easy, won’t affect other modules.
•Graphical User Interface can be changed faster as it is separate from
business logic and database.
•Workload can be easily distributed among developers because each tier is
independent.
3.1 Two-tier Database Design, Three-tier Database
Design
Three tier database design
Disadvantages:
•Increase Complexity/Effort:
•It is more complex and difficult to build three – tier
architecture.
•The physical separation of the tiers may affect the performance
between the three tiers.
3.2 The JDBC API
●
Common JDBC Components
•JDBC defines how a java program can communicate with a database.
•JDBC architecture consists of different Layers and Drivers which are capable of
working with any database.
3.2 The JDBC API
●
Common JDBC Components
•JDBC have following components:
•JDBC API - (java.sql,javax.sql)
•Provides programmatic access to a relational database (like Oracle,
SQL Server, MySQL, etc.) from Java.
•Using JDBC API, frontend java applications can execute query and
fetch data from connected databases.
•JDBC API can also connect with multiple applications with same
database or same application with multiple Databases which may be
residing in different computers (distributed environment).
3.2 The JDBC API
●
Common JDBC Components
•Driver Manager:
•This class manages a list of database drivers.
•JDBC Driver Manager Interface defines 'objects' which are used to
connect java application with different JDBC Drivers.
•JDBC Driver Manager loads the most appropriate driver among all
the Drivers for creating a connection.
•JDBC Test Suite – JDBC Test Suite is used to check compatibility of a
JDBC driver with J2EE platform.
•It also check whether a driver follow all the standard and
requirements of J2EE environment.
3.2 The JDBC API
●
Common JDBC Components
JDBC-ODBC Bridge:
•As the name suggests this enables JDBC access via ODBC drivers.
•The JDBC Driver contact to the ODBC Driver for connection to the
database.
•The ODBC Driver is already installed or come as default driver in
windows.
•In Windows using this "data source name (DSN)" you can connect
JDBC to ODBC.
3.2 The JDBC API
Important interfaces and classes of java.sql package
•java.sql.DriverManager: handles the loading and unloading of the
appropriate database drivers required to make the connection.
•java.sql.Connection: exposes the database to the developer,
representing the connection as an accessible Java component.
•java.sql.Statement: provides a container for executing SQL
statements using a connection.
•java.sql.ResultSet: exposes the data that returns from the database
server to the Java application.
3.2 The JDBC API
•DriverManager: This class manages a list of database drivers. Find
proper database driver .
•Driver: This interface handles the communications with the
database server.
•Connection: This interface with all methods for contacting a
database. The connection object represents communication context.
•Statement: You use objects created from this interface to submit
the SQL statements to the database.
•ResultSet: These objects hold data retrieved from a database after
you execute an SQL query using Statement objects.
•SQLException: This class handles any errors that occur in a database
application.
3.4 JDBC Drivers
• JDBC Driver is a software component that enables java application
to interact with the database.
• JDBC drivers have a specific purpose and properties of its own.
• There are four types of JDBC drivers available:
• Type 1: JDBC-ODBC Bridge driver (Bridge)
• Type 2: Native-API/partly Java driver (Native)
• Type 3: All Java/Net-protocol driver (Middleware)
• Type 4: All Java/Native-protocol driver (Pure)
3.4 JDBC Drivers
3.4 JDBC Drivers
Type-1 : JDBC-To-ODBC Bridge Driver (Bridge)
3.4 JDBC Drivers
Type-1 : JDBC-To-ODBC Bridge Driver
3.4 JDBC Drivers
Type-1 : JDBC-To-ODBC Bridge Driver
3.4 JDBC Drivers
Type-1 : JDBC-To-ODBC Bridge Driver
•In a Type 1 driver, a JDBC bridge is used to access ODBC drivers installed on each
client machine.
•Using ODBC, requires configuring on your system a Data Source Name (DSN) that
represents the target database.
•The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the
ODBC driver.
•When Java first came out, this was a useful driver because most databases only
supported ODBC access but The JDBC-ODBC Bridge driver is recommended only
for experimental use or when no other alternative is available.
•Bridge solutions are not good solutions for applications that donot allow you to
install software on the client.
3.4 JDBC Drivers
Type-1 : JDBC-To-ODBC Bridge Driver
Advantages:
• It can be used with almost all databases because all the database
having their own ODBC driver.
• It is inexpensive.
• Easy to use.
Disadvantages:
•It is slower compared to other JDBC driver, because it requires
double translation for each request and response.
•It requires an ODBC driver to be installed on client computer.
• It is not well suited for web and applet.
•The Bridge driver is not written fully in Java, Type 1 drivers are not
portable.
•It is the slowest of all driver types.
3.4 JDBC Drivers
Type-2 : Native-API/partly Java driver
3.4 JDBC Drivers
Type-2 : Native-API/partly Java driver
3.4 JDBC Drivers
Type-2 : Native-API/partly Java driver
3.4 JDBC Drivers
Type-2 : Native-API/partly Java driver
•The type 2 drivers are native API drivers.
•Type 2 drivers convert JDBC calls into database-specific calls.
•This driver is specific to a particular database.
•This solution requires software on the client system.
•The vendor-specific driver must be installed on each client machine.
•These drivers are typically provided by the database vendors.
•If we change the Database, we have to change the native API because
it is database specific.
•Speed increase, because it eliminates ODBC's overhead.
•It is not written entirely in java.
3.4 JDBC Drivers
Type-2 : Native-API/partly Java driver
Advantage:
•Faster as compared to Type-1 Driver
•Contains additional features.
•Performance increases with a Type 2 driver as compare to Type 1
driver, because it eliminates ODBC's overhead.
3.4 JDBC Drivers
Type-2 : Native-API/partly Java driver
Disadvantage:
•It requires native library or the Call Level libraries to be installed on
client computer.
•All databases do not provide call level libraries.
•Increased cost of Application.
•It is not written in java, so it may cause portability issue. It is
platform dependent.
•If we change the Database we have to change the native API as it is
specific to a database.
3.4 JDBC Drivers
Type-3 : JDBC-Net Pure Java Drivers
3.4 JDBC Drivers
Type-3 : JDBC-Net Pure Java Drivers
3.4 JDBC Drivers
Type-3 : JDBC-Net Pure Java Drivers
3.4 JDBC Drivers
Type-3 : JDBC-Net Pure Java Drivers
•In a Type 3 driver, a three-tier architecture is used to access
databases.
•It uses middleware (application server) that converts JDBC calls
directly or indirectly into the vendor- specific database protocol.
•This driver translate the jdbc calls into a middleware server
specific calls.
•With the help of the middleware server, the translated jdbc calls
further translated into database server specific calls.
•In JDBC Net – All Java Driver, the native CLI libraries are placed on
a separate server.
3.4 JDBC Drivers
Type-3 : JDBC-Net Pure Java Drivers
•The JDBC driver on client uses a network protocol to provide
communication between the application and the JDBC driver on
server.
•Thus all communication between an application and database
server is 100% java but the communication between database
server and middleware server is based on native CLI.
•This type of driver also known as net-protocol fully java
technology-enabled driver.
3.4 JDBC Drivers
Type-3 : JDBC-Net Pure Java Drivers
•Type-3 driver is recommended to be used with applets. Its autodownloadable.
•Can interface to multiple databases
•This kind of driver is extremely flexible, since it requires no code
installed on the client.
•You can think of the application server as a JDBC "proxy,"
meaning that it makes calls for the client application.
3.4 JDBC Drivers
Type-3 : JDBC-Net Pure Java Drivers
Advantages
•Since the communication between client and the middleware
server is database independent, there is no need for the database
vendor library on the client side.
•This driver is fully written in java, so it is portable.
•It is very flexible allows access to multiple databases using one
driver.
•They are the most efficient amongst all driver types.
3.4 JDBC Drivers
Type-3 : JDBC-Net Pure Java Drivers
Advantages
•The middleware server can provide typical middleware services like
caching, load balancing, logging and auditing.
•Driver can be managed centrally.
•We can switch from one database to other without changing the
client-side driver class, by just changing configurations of
middleware server.
3.4 JDBC Drivers
Type-3 : JDBC-Net Pure Java Drivers
Disadvantages
•It requires another server application to install and maintain.
•It requires separate middleware server to manage driver centrally.
Database-specific coding needs to be done in the middle tier.
• Network support is required on client machine.
•Maintenance of Network Protocol driver becomes costly because it
requires database-specific coding to be done in the middle tier.
•Compared to Type 2 drivers, Type 3 drivers are slow due to
increased number of network calls.
3.4 JDBC Drivers
Type-4 : Native Protocol Pure Java Driver
3.4 JDBC Drivers
Type-4 : Native Protocol Pure Java Driver
3.4 JDBC Drivers
Type-4 : Native Protocol Pure Java Driver
3.4 JDBC Drivers
Type-4 : Native Protocol Pure Java Driver
•The Native Protocol – All Java Driver is also known as type – 4 driver
or Direct to Database Pure Java Driver.
•It converts JDBC calls directly into a vendor specific database
protocol.
•As the driver is vendor specific , the JDBC client requires separate
drivers for different database. Need to download a new driver for each
database engine.
•It does not require translation of the JDBC call into native CLI call or
ODBC call. It directly communicates with database.
3.4 JDBC Drivers
Type-4 : Native Protocol Pure Java Driver
•It is written in JAVA, so it is known as pure java or 100% java driver
and thus it is platform independent.
•This kind of driver is extremely flexible; you don't need to install
special software on the client or server.
•This type of driver is lightweight and generally known as thin driver.
3.4 JDBC Drivers
Type-4 : Native Protocol Pure Java Driver
Advantages:
•They are completely written in java. So they are platform
independent.
•It can be easily downloaded from web.
•No native libraries are required to be installed in client machine.
•No translation or middleware layers are used so Performance is
faster than other JDBC drivers.
•Secure to use since, it uses database server specific protocol.
3.4 JDBC Drivers
Type-4 : Native Protocol Pure Java Driver
Advantages:
•The client application connects directly to the database server.
•The JVM manages all the aspects of the application-to-database
connection.
•Does not require any Middleware server.
3.4 JDBC Drivers
Type-4 : Native Protocol Pure Java Driver
Disadvantages:
• Slow due to increase number of network call.
• It requires separate drivers for different database, because it is
vendor specific.
3.2 The JDBC API
• Database Operations ( CRUD – Create, Read, Update,
Delete operation )
JDBC Process:
Step 1: Loading JDBC Driver
Step 2: Creating connection to DBMS
Step 3: Creating Statement
Step 4: Executing Statement
Step 5: Closing the connection with DBMS
3.2 The JDBC API
• Database Operations ( CRUD – Create, Read, Update,
Delete operation )
1) Register the driver class
• The forName() method of Class class is used to register the driver
class.
• This method is used to dynamically load the driver class.
• Syntax of forName() method
public static void forName(String className)throws ClassNotFo
undException
• Example to register the OracleDriver class
Class.forName("oracle.jdbc.driver.OracleDriver");
Driver names for popular RDBMS are:
RDBMS
JDBC Driver Name
MySQL
com.mysql.jdbc.Driver
ORACLE
oracle.jdbc.driver.OracleDriver
MS-ACCESS
sun.jdbc.odbc.JdbcOdbcDriver
2) Create the connection object
•
After you have loaded driver, you can establish connection using the
DriverManager class (java.sql.DriverManager).
•
The getConnection() method of DriverManager class is used to establish
connection with the database.
•
Syntax of getConnection() method
•
public static Connection getConnection(String url)throws SQLException
•
public static Connection getConnection(String url,String name,String passwo
rd) throws SQLException
•
Example to establish connection with the Oracle database
Connection c=DriverManager.getConnection("jdbc:oracle:thin:@localhost:
1521:xe","system","password");
Database URLs for popular RDBMS are:
RDBMS
Database URL Format
MySQL
jdbc:mysql://hostname/databasename
ORACLE
jdbc:oracle:thin:@hostname:port:database
name
jdbc:odbc:databasename
MSACCESS
3) Create the Statement object
• Once connection is obtained, we can interact with database.
• The createStatement() method of Connection interface is used to
create statement.
• The object of statement is responsible to execute queries with the
database.
• Syntax of createStatement() method
public Statement createStatement()throws SQLException
• Example to create the statement object
Statement stmt=con.createStatement();
4) Execute the query
• 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. (SELECT Method)
• Syntax of executeQuery() method
public ResultSet executeQuery(String sql)throws SQLException
• Example to execute query
ResultSet rs=stmt.executeQuery("select * from student");
4) Execute the query
• int executeUpdate(String sql)
• It executes the given SQL statement, which may be an
INSERT, UPDATE, or DELETE statement.
• It returns no of rows affected by the execution of the
SQL statement.
• Example:
• st.executeUpdate("insert into info values(3,‘abc')");
5) Close the connection object
• By closing connection object statement and ResultSet
will be closed automatically.
• The close() method of Connection interface is used to
close the connection.
• Syntax of close() method:
• public void close()throws SQLException
• Example to close connection
• con.close();
• rs.close();
• st.close();
3.6 Develop JAVA program using JDBC
Connecting to Access Database using Type-1 Driver
•
•
•
•
•
•
•
Create DSN Name
Go to control panel
Go to Administrative tools
Select Data Source(ODBC)
Add new DSN name, select add
Select Access driver from the list, click on finish
Give a DSN name, click ok
CREATE table using JDBC with MS Access
import java.sql.*;
class TestAccessDB
{
public static void main(String []args)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Test", "", "");
Statement s=con.createStatement();
//creating statement
s.executeUpdate(“create table info(id int, name varchar(50))");
con.close(); //closing connection
}catch(Exception e)
{
e.printStackTrace();
}}}
INSERT data using JDBC with MS Access
import java.sql.*;
class TestAccessDB
{
public static void main(String []args)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Test", "", "");
Statement s=con.createStatement();
s.executeuUpdate(“insert into info values(1,’xyz’)");
con.close(); //closing connection
}catch(Exception e)
{
e.printStackTrace();
}}}
SELECT data using JDBC with MS Access
import java.sql.*;
class TestAccessDB
{
public static void main(String []args)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Test", "", "");
Statement s=con.createStatement(); //creating statement
ResultSet rs=s.executeQuery("select * from student"); //executing statement
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
con.close(); //closing connection
}catch(Exception e)
{
e.printStackTrace();
}}}
UPDATE data using JDBC with MS Access
import java.sql.*;
class TestAccessDB
{
public static void main(String []args)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Test", "", "");
Statement s=con.createStatement();
s.executeuUpdate(“update info set name=‘pqr’ where id=1");
con.close(); //closing connection
}catch(Exception e)
{
e.printStackTrace();
}}}
DELETE data using JDBC with MS Access
import java.sql.*;
class TestAccessDB
{
public static void main(String []args)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Test", "", "");
Statement s=con.createStatement();
s.executeuUpdate(“delete * from info where id=1");
con.close(); //closing connection
}catch(Exception e)
{
e.printStackTrace();
}}}
Connecting to MySQL Database using
Thin Driver
import java.sql.*;
public class MysqlJdbcOdbcDemo {
public MysqlJdbcOdbcDemo() {
}
public static void main (String[] args) {
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/ajpdb","root","");
Statement st = con.createStatement();
st.executeUpdate("insert into info values(3,’abc')");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Connecting to Oracle Database using Thin
Driver
import java.sql.*;
public class OracleJdbcDemo {
public OracleJdbcDemo() {
}
public static void main (String[] args) {
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"system","system");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select * from info");
while(rs.next())
{
System.out.println(rs.getString(2));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Prepared Statement
• The PreparedStatement interface extends the Statement
interface.
• This statement gives you the flexibility of supplying
arguments dynamically.
• Example:
• String query=“insert into info values(?,?)”;
• All of the Statement object's methods for interacting with
the database executeQuery(), and executeUpdate() also
works with the PreparedStatement object.
Prepared Statement
import java.sql.*;
class TestAccessDB
{
public static void main(String []args)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Test", "", "");
PreparedStatement pst=con.prepareStatement("insert into info values(?,?)");
pst.setInt(1,101);
pst.setString(2,“ajp");
pst.executeUpdate();
pst.setInt(1,102);
pst.setString(2,“nma");
pst.executeUpdate();
Prepared Statement
con.close(); //closing connection
}catch(Exception e)
{
e.printStackTrace();
}
}
}
3.3 JDBC : Advantages and Disadvantages
Advantages:
• Can read any database if proper drivers are installed.
• No content conversion required.
• Query and Stored procedure supported.
• Can be used for both Synchronous and Asynchronous processing.
• Zero Configurations for Network Computers.
• Database Connection Identified by URL.
• Business can continue to use their installed database and access
information, even if it is stored on different database management
system.
3.3 JDBC : Advantages and Disadvantages
Advantages:
• The combination of Java API and JDBC API makes application
development easy and cost effective.
• A pure JDBC technology based driver does not require special
installation.
• JDBC API includes a way to identify and connect to a data source
using Data Source object. This makes code even more portable and
easier to maintain.
3.3 JDBC : Advantages and Disadvantages
Disadvantages:
• Correct drivers need to be deployed for each type of database.
• Cannot update or insert multiple tables with sequence.
• JDBC needs database specific queries. It means if you are using
Oracle then you have to use Oracle queries.
• Handling the JDBC connections and properly closing the
connection is also a big issue. Properly closing the connection is
must.
• Each time connection closing is compulsory.
• Exception handling is one of the main drawback in JDBC.
3.3 JDBC : Advantages and Disadvantages
Disadvantages:
• JDBC is not good if used in large projects. There is a big
programming overhead.
• JDBC can't maintain the database independent SQL statements.
I.e in future if there is needs to change the database because of
maintenance problem then we need to change all SQL statements
according to the new database.
• We can't retrieve each record as object. JDBC retrieves it as scalar
values only. We know objects are more faster then scalar values.
3.5 JDBC – ODBC Bridge
• The JDBC – ODBC bridge driver is also known as Type – I driver.
• The JDBC – ODBC driver translates the JDBC calls into ODBC
call and then sends the ODBC call to ODBC driver, so it is known
as JDBC – ODBC bridge driver.
•
The JDBC-ODBC Bridge is a JDBC driver which implements
JDBC operations by translating them into ODBC operations.
•
The bridge is installed automatically with the JDK as package
sun.jdbc.odbc. No special configuration is required for the
bridge.
• This driver does not perform faster but it can be used with almost
all databases.
3.5 JDBC – ODBC Bridge
• The JDBC – ODBC driver can be used with multiple databases because
most of the databases support ODBC.
• This type of driver is preferred only when there is no pure java driver is
available for particular database.

The bridge driver does not work well with applets.

The bridge driver uses the odbc subprotocol. URLs for this subprotocol
are of the form:

jdbc:odbc:<data-source-name>[<attribute-name>=<attributevalue>]*
Example:

jdbc:odbc:Sybase

jdbc:odbc:mydb;UID=me;PWD=secret
3.5 JDBC – ODBC Bridge
• The JDBC – ODBC Bridge Driver Works as follow:
• First the JAVA application makes a request to JDBC Driver.
• The JDBC – ODBC bridge driver translates the JDBC call into ODBC
call.
• The translated ODBC call is sent to ODBC Driver.
• The ODBC driver again translate the request and sends it to the
database.
• Upon receiving the request from the ODBC driver, the database process
the request and present the data in ODBC format to ODBC driver.
• The JDBC – ODBC Bridge driver than translate the ODBC format into
JDBC format and present it to the client.
3.5 JDBC – ODBC Bridge
Advantages:
1. It can be used with almost all databases because all the
database having their own ODBC driver.
2. It is inexpensive.
Disadvantages:
1. It is slower compared to other JDBC driver, because it requires
double translation for each request and response.
2. It requires an ODBC driver to be installed on client computer.
3. It is not well suited for web and applet.
*******
Download