Chapter 2 JDBC

Embed Size (px)

Citation preview

  • 8/11/2019 Chapter 2 JDBC

    1/30

    JDBC

  • 8/11/2019 Chapter 2 JDBC

    2/30

    The JDBC (Java Database Connectivity) interface is a

    pure Java API used to execute SQL Statements.

    It provides a set of classes & interfaces that can be

    used by developers to write a database applications in

    Java.

  • 8/11/2019 Chapter 2 JDBC

    3/30

    Two Tier Access Models

    Application Space

    Java Application

    JDBC DRIVER

    SQL stmts Result Set

    DATABASE

    Your Java Application talks directly to the DB. This is accomplished through

    the Use of JDBC DRIVER, which sends commands directly to DB. The results

    are sent back from the DB directly to the application.

  • 8/11/2019 Chapter 2 JDBC

    4/30

    Three Tier Access Models

    Application Space

    Java Application

    JDBC DRIVER

    SQL stmts Result Set

    APPLICATION SERVER

    (MIDDLE TIER)

    Proprietary Protocol

    DATABASE

    Your JDBC DRIVER sends commands to a Middle Tier, which in turn sends

    commands to the DB. The results of these commands are sent back to

    middle tier, which sends them back to the application.

  • 8/11/2019 Chapter 2 JDBC

    5/30

    For Example

    Located@ Any PC

    HTTPRequests

    MicrosoftInternetExplorer

    HTML

    Located@ Your PC

    Apache Tomcat Java ServeletsApp Server

    JDBCTuples

    Requests

    Located Oracle@ DBLab DB Server

  • 8/11/2019 Chapter 2 JDBC

    6/30

    Basic Components of JDBC:

    1.JDBC API

    2.Driver Manager

    3. JDBC Test Suite

    4. JDBC-ODBC Bridge

  • 8/11/2019 Chapter 2 JDBC

    7/30

    Type 1 : JDBC-ODBC Bridge , plus ODBC Driver

    It provides JDBC access to DB through Open Database Connectivity Drivers (ODBC)

    Application Space

    Java Application

    JDBC - ODBC BRIDGE

    SQL COMMANDS Result Set

    ODBC DRIVER

    Proprietary Protocol

    DATABASE

  • 8/11/2019 Chapter 2 JDBC

    8/30

    The JDBC type 1 driver, also known as the JDBC-ODBC bridge is a

    database driver implementation that employs the ODBC driver to

    connect to the database

    The driver converts JDBC method calls into ODBC function calls. The

    bridge is usually used when there is no pure-Java driver available for a

    particular database.

    The driver is implemented in the sun.jdbc.odbc.JdbcOdbcDriver classand comes with the Java 2 SDK, Standard Edition.

  • 8/11/2019 Chapter 2 JDBC

    9/30

    Functions

    1.Translates query obtained by JDBC into corresponding ODBCquery, which is then handled by the ODBC driver.

    2. Sun provides a JDBC-ODBC Bridge driver.

    sun.jdbc.odbc.JdbcOdbcDriver. This driver is native code and not

    Java, and is closed source.

    3. Client -> JDBC Driver -> ODBC Driver -> Database

    4. There is some overhead associated with the translation work to

    go from JDBC to ODBC.

  • 8/11/2019 Chapter 2 JDBC

    10/30

    Advantages:1. Almost any database for which ODBC driver is installed, can be

    accessed.

    Disadvantages:1. Performance overhead since the calls have to go through the

    JDBC overhead bridge to the ODBC driver, then to the native

    database connectivity interface.

    2. The ODBC driver needs to be installed on the client machine.

    3. Considering the client-side software needed, this might not be

    suitable for applets.

  • 8/11/2019 Chapter 2 JDBC

    11/30

    Type 2 : Native API , Partly Java Driver

    It converts JDBC commands into DBMS specific calls.

    Here Client must have some binary code loaded on its machine.

    Application Space

    Java Application

    Type 2 JDBC DRIVER

    SQL COMMANDS Result Set

    Native Database Library

    Proprietary Protocol

    DATABASE

  • 8/11/2019 Chapter 2 JDBC

    12/30

    The JDBC type 2 driver, also known as the

    Native-API driver is a database driverimplementation that uses the client-side

    libraries of the database.

    The driver converts JDBC method calls intonative calls of the database API.

  • 8/11/2019 Chapter 2 JDBC

    13/30

    Functions:

    1. This type of driver converts JDBC calls into calls to the client

    API for that database.

    2. Client -> JDBC Driver -> Vendor Client DB Library ->

    Database

  • 8/11/2019 Chapter 2 JDBC

    14/30

    Advantage

    Better performance than Type 1 since no jdbc to odbc translation is

    needed.

    Disadvantages

    1. The vendor client library needs to be installed on the clientmachine.

    2. Cannot be used in internet due the client side software needed.

    3. Not all databases give the client side library

  • 8/11/2019 Chapter 2 JDBC

    15/30

    Type 3 : JDBC-Net , Pure Java Driver

    These drivers are used for 3-tier solutions. It translates JDBC calls into DB

    independent n/w protocol that is sent to a middleware server.

    This server translates this DB independent protocol into a DB Specific protocol,

    which is sent to a particular DB.

    Application Space

    Java Application

    Type 3 JDBC DRIVER

    SQL COMMAND Result Set

    MIDDLEWARE

    JDBC DRIVER

    Proprietary Protocol

    DATABASE

  • 8/11/2019 Chapter 2 JDBC

    16/30

    The JDBC type 3 driver, also known as the network-protocol

    driver is a database driver implementation which makes use

    of a middle-tier between the calling program and the

    database.

    The middle-tier (application server) converts JDBC calls

    directly or indirectly into the vendor-specific database

    protocol.

  • 8/11/2019 Chapter 2 JDBC

    17/30

    Functions:

    1.Follows a three tier communication approach.

    2. Can interface to multiple databases - Not vendor specific.

    3. The JDBC Client driver written in java communicates with a

    middleware-net-server using a database independent protocol, and thenthis net server translates this request into database commands for that

    database.

    4. Thus the client driver to middleware communication is database

    independent.

    5. Client -> JDBC Driver -> Middleware-Net Server -> Any Database

  • 8/11/2019 Chapter 2 JDBC

    18/30

    Advantages1. Since the communication between client and the middleware

    server is database independent, there is no need for the vendor db

    library on the client machine. Also the client to middleware need'nt

    be changed for a new database.

    2. The Middleware Server (Can be a full fledged J2EE Application

    server) can provide typical middleware services like caching

    (connections, query results, and so on), load balancing, logging,

    auditing etc..

    3. eg. for the above include jdbc driver features in Weblogic.

    4. Can be used in internet since there is no client side software

    needed.

    5. At client side a single driver can handle any database.(It worksprovided the middlware supports that database!!)

  • 8/11/2019 Chapter 2 JDBC

    19/30

    Disadvantages

    1.Requires database-specific coding to be done in the middle tier.

    2. An extra layer added may result in a time-bottleneck. But

    typically this is overcome by providing efficient middleware

    services described above.

  • 8/11/2019 Chapter 2 JDBC

    20/30

    Type 4 : Native Protocol , Pure Java Driver

    It Communicates directly with the DB.

    They do this by converting JDBC commands directly into the DB Engines Native

    Protocol.

    Application Space

    Java Application

    Type 4 JDBC DRIVER

    SQL COMMAND Result Set

    Using Proprietary Protocol Using Proprietary Protocol

    DATABASE

  • 8/11/2019 Chapter 2 JDBC

    21/30

    The JDBC type 4 driver, also known as the native-protocol driver

    is a database driver implementation that converts JDBC calls

    directly into the vendor-specific database protocol.

  • 8/11/2019 Chapter 2 JDBC

    22/30

    Functions

    1. Type 4 drivers are entirely written in Java that communicates directly

    with a vendor's database through socket connections. No

    translation or middleware layers, are required, improving performance.

    2. The driver converts JDBC calls into the vendor-specific database

    protocol so that client applications can communicate directly with thedatabase server.

    3. Completely implemented in Java to achieve platform independence.

    4. e.g include the widely used Oracle thin driver - oracle.jdbc.driver.OracleDriver which connect to jdbc:oracle:thin URL format.

    5. Client Machine -> Native protocol JDBC Driver -> Database server

  • 8/11/2019 Chapter 2 JDBC

    23/30

    Advantages

    These drivers don't translate the requests into db request toODBC or pass it to client api for the db, nor do they need a

    middleware layer for request indirection.

    Thus the performance is considerably improved.

    DisadvantageAt client side, a separate driver is needed for each database.

  • 8/11/2019 Chapter 2 JDBC

    24/30

    Java

    applicationJDBC-API

    JDBC-

    Driver manager

    Native JDBC-

    Protocol driver Net-driver

    DB-

    Middleware

    JDBC-ODBC Native

    bridge API-driver

    ODBC Client library

    Client library

    JDBC Drivers

  • 8/11/2019 Chapter 2 JDBC

    25/30

  • 8/11/2019 Chapter 2 JDBC

    26/30

    1. Establish a connection

    import java.sql.*;

    Load the specific driver

    Class.forName(sun.jdbc.odbc.JdbcOdbcDriver");

    Dynamically loads a driver class & registers it to aDriverManager

    Make the connection

    Connection con = DriverManager.getConnection("jdbc:odbc:dsn", username, passwd);

    Establishes connection to database by obtaininga Connectionobject

  • 8/11/2019 Chapter 2 JDBC

    27/30

    2.Create JDBC Statement

    Statement stmt = con.createStatement() ; Creates a Statement object for sending SQL statements to

    the database

  • 8/11/2019 Chapter 2 JDBC

    28/30

    3. Executes SQL Commands

    String tbl = "Create table user (Name VARCHAR(32),Age Integer)";

    stmt.executeUpdate(tbl);

    String intbl = "Insert into user values(abc,30)";

    stmt.executeUpdate(intbl);

  • 8/11/2019 Chapter 2 JDBC

    29/30

    4. Process Results

    String query = "select * from user";

    ResultSetrs = Stmt.executeQuery(query);

    while (rs.next()) {

    String name = rs.getString("NAME");

    int marks = rs.getInt(Age");

    }

  • 8/11/2019 Chapter 2 JDBC

    30/30

    5. Close Connections

    Stmt.close()

    Con.close()