about jdbc drivers

Upload: priya-chopra

Post on 06-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 About JDBC Drivers

    1/2

    About JDBC Drivers

    Java applications cannot directly communicate with a database to submit and retrieve the

    results of queries.this is because dbms/rdbms can understand only SQL statements andnot java language statements .hence you need a mechanism to transalate java statements

    to SQL statements.

    JDBC API uses a driver to address these issues .JDBC API takes care of converting java

    commands to generic SQL statements .

    There are several categories of jdbc drivers provided by different database vendors .They

    are

    1.JDBC-ODBC bridge driver:there are several DBMS/RDBMS, such as MS acess and

    SQL server that contain the ODBC driver embedded into them.since the ODBC API iswritten in the Clanguage and makes use of ponters and other constructs that java does not

    support ,a java program cannot directly communicate with an ODBC driver.The JDBC-ODBC bridge driver transalates the JDBC API to the ODBC API

    2.Native API partly java driver: some DBMS/RDBMS,such as DB2and Informix,contaila JDBC driver supplied by the database vendor.JDBC drivers consist of classes that the

    JDBC API can invoke directly.

    3.NATIVE protocal pure java driver/JDBC-net pure java driver: these drivers are used to

    connect a client application or applet to a database over a TCP/IP connection

    4.JDBC DRIVer MANAGER

    the functions are to maintain a list of drivers created for different databases and connect ajava apllication to the appropriate driver specified in a java program

    5.JDBC-ODBC Bridgeas apart of JDbc ,sun microsysyems provides a driver to acess ODBC data sources from

    JDBC.This driver s called JDBC-ODBC bridge.

    Can we Load more then one Drivers at once?

    Q1. Can we load more the one Driver thru Class.forName() method?

    Q2. What is the difference if we are using Class.forName()method andDriverManager.registerDriver().Which one is better?

    You can load multiple drivers using the Class.forName() method.

    The drivers could be for the same database or for different databases.

  • 8/3/2019 About JDBC Drivers

    2/2

    When you invoke Class.forName(), you, I mean the driver, are/is actually doing two

    things:

    1) create an instance of the driver which is being loaded

    2) register this instance of the driver with the DriverManager using theDriverManager.registerDriver() method.

    In order to use DriverManager.registerDriver you need a reference to a Driver instance.

    One way to get a reference to a driver instance is using the getDrivers method of the

    DriverManager class. But all the Driver instances you get by invoking this method arealready registered with the Drivermanager, so this is of no use. The 'other way' is to

    create an instance of the Driver like this:

    Driver d = new oracle.jdbc.driver.OracleDriver();

    DriverManager.registerDriver(d);

    Now, you tell me which one is better? ;)

    P.S: Its always better to use Class.forName(drivername).newInstance() than using just

    Class.forName(drivername). (Yeah there is a duplicate instance). Every driver has code

    in a static initializer block which registers itself with the DriverManager. A few JVMimplementations do not execute the static initializer block untill an instance of the class is

    created.