Anda di halaman 1dari 19

JDBC

Introduction

JDBC is Java application programming interface (API) that allows the Java programmers to access database management system from Java code. It was developed by JavaSoft, a subsidiary of Sun Microsystems. The JDBC ( Java Database Connectivity) API defines interfaces and classes for writing database applications in Java by making database connections The JDBC API is a Java API that can access any kind of tabular data, especially data stored in a Relational Database. Using JDBC you can send SQL, PL/SQL statements to almost any relational database. JDBC is a Java API for executing SQL statements and supports basic SQL functionality. It provides RDBMS access by allowing you to embed SQL inside Java code.

JDBC helps you to write java applications that manage these three programming activities: 1. Connect to a data source, like a database 2. Send queries and update statements to the database 3. Retrieve and process the results received from the database in answer to your query. Java Database Connectivity is similar to Open Database Connectivity (ODBC) which is used for accessing and managing database, but the difference is that JDBC is designed specifically for Java programs, whereas ODBC is not depended upon any language. Accessing a database using JDBC involves a number of steps: 1. Get a Connection object connected to a database 2. Get a Statement object from an open Connectionobject 3. Get a ResultSet from a Statement's query execution 4. Process the rows from the ResultSet

JDBC Components
JDBC has four Components: 1. The JDBC API. 2. The JDBC Driver Manager. 3. The JDBC Test Suite. 4. The JDBC-ODBC Bridge.

1. The JDBC API.


The JDBC API provides the facility for accessing the relational database from the Java programming language. The API technology provides the industrial standard for independently connecting Java programming language and a wide range of databases. The user not only execute the SQL statements, retrieve results, and update the data but can also access it anywhere within a network because of it's "Write Once, Run Anywhere" (WORA) capabilities.

Due to JDBC API technology, user can also access other tabular data sources like spreadsheets or flat files even in the a heterogeneous environment. JDBC API is part of the Java platform, which includes the Java Standard Edition (Java SE ) and the Java Enterprise Edition (Java EE). JDBC 4.0 application programming interface is divided into two packages i-) java.sql ii-) javax.sql. Both packages are included in the Java SE and Java EE platforms.

2.

The JDBC Driver Manager


The JDBC Driver Manager is a very important class that defines objects which connect Java applications to a JDBC driver. Usually Driver Manager is the backbone of the JDBC architecture. It's very simple and small and is used to provide a means of managing the different types of JDBC database driver running on an application. The main responsibility of JDBC database driver is to load all the drivers found in the system properly as well as to select the most appropriate driver from opening a connection to a database. The Driver Manager also helps to select the most appropriate driver from the previously loaded drivers when a new open database is connected

3. The JDBC Test Suite. The function of JDBC driver test suite is to make ensure that the JDBC drivers will run user's program. The test suite of JDBC application program interface is very useful for testing a driver based on JDBC technology during testing period. It ensures the requirement of Java Platform Enterprise Edition (J2EE).

4. The JDBC-ODBC Bridge.


The Java Software bridge provides JDBC access via ODBC drivers. The JDBC-ODBC bridge, also known as JDBC type 1 driver is a database driver that utilize the ODBC driver to connect the database. This driver translates JDBC method calls into ODBC function calls. The Bridge implements JDBC for any database for which an ODBC driver is available. The Bridge is always implemented as the sun.jdbc.odbc Java package and it contains a native library used to access ODBC.

JDBC Architecture

Two-tier and Three-tier Processing Models


The JDBC API supports both two-tier and three-tier processing models for database access. Two Tier Model In the two-tier model, a Java applet or application talks directly to the data source. This requires a JDBC driver that can communicate with the particular data source being accessed. A user's commands are delivered to the database or other data source, and the results of those statements are sent back to the user. The data source may be located on another machine to which the user is connected via a network. This is referred to as a client/server configuration, with the user's machine as the client, and the machine housing the data source as the server. The network can be an intranet, which, for example, connects employees within a corporation, or it can be the Internet.

Three Tier Model

In the three-tier model, commands are sent to a "middle tier" of services, which then sends the commands to the data source. The data source processes the commands and sends the results back to the middle tier, which then sends them to the user. MIS directors find the three-tier model very attractive because the middle tier makes it possible to maintain control over access and the kinds of updates that can be made to corporate data. Another advantage is that it simplifies the deployment of applications. Finally, in many cases, the three-tier architecture can provide performance advantages.

Two-tier Architecture for Data Access.

Three-tier Architecture for Data Access.

1. 2.

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. Following is the architectural diagram, which shows the location of the driver manager with respect to the JDBC drivers and the Java application:

What is JDBC Driver ?


The JDBC API defines the Java interfaces and classes that programmers use to connect to databases and send queries. A JDBC driver implements these interfaces and classes for a particular DBMS vendor. A Java program that uses the JDBC API loads the specified driver for a particular DBMS before it actually connects to a database. The JDBC DriverManager class then sends all JDBC API calls to the loaded driver. JDBC drivers implement the defined interfaces in the JDBC API for interacting with your database server. For example, JDBC drivers enable you to open database connections and to interact with it by sending SQL or database commands then receiving results with Java. The Java.sql package that ships with JDK contains various classes with their behaviours defined and their actual implementations are done in third-party drivers. Third party vendors implements the java.sql.Driver interface in their database driver.

JDBC Drivers Types:

JDBC drivers are divided into four types or levels. The different types of JDBC drivers are: 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) JDBC driver implementations vary because of the wide variety of operating systems and hardware platforms in which Java operates.

Type 1 JDBC-ODBC Bridge


The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the ODBC driver. ODBC is a generic API. Implemented in native code and requires some non-Java software on the client Not a mandatory component of the JDK, and is not automatically supported by Java run-time environments The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available.

1. 2.

3. 4.

Advantage The JDBC-ODBC Bridge allows access to almost any database, since the database's ODBC drivers are already available. Disadvantages Since the Bridge driver is not written fully in Java, Type 1 drivers are not portable. A performance issue is seen as a JDBC call goes through the bridge to the ODBC driver, then to the database, and this applies even in the reverse process. They are the slowest of all driver types. The client system requires the ODBC Installation to use the driver. Not good for the Web.

Type 2 Native-API Java driver


1. 2. 3. 4.

5.

The distinctive characteristic of type 2 jdbc drivers are that Type 2 drivers convert JDBC calls into database-specific calls i.e. this driver is specific to a particular database. Distinctive characteristic of type 2 jdbc drivers is that Oracle will have oracle Native API Converts JDBC commands into DBMS-specific native calls Advantage The distinctive characteristic of type 2 jdbc drivers are that they are typically offer better performance than the JDBC-ODBC Bridge as the layers of communication (tiers) are less than that of Type 1 and also it uses Native API which is Database specific. Disadvantage Native API must be installed in the Client System and hence type 2 drivers cannot be used for the Internet. Like Type 1 drivers, its not written in Java Language which forms a portability issue. If we change the Database we have to change the native api as it is specific to a databas Mostly obsolete now Usually not thread safe.

Type 3 JDBC-Net drivers

1.
2. 3. 4. 5.

6. 7.

Type 3 database requests are passed through the network to the middletier server. The middle-tier then translates the request to the database. The middle-tier server can in turn use Type1, Type 2 or Type 4 drivers. Advantage This driver is server-based, so there is no need for any vendor database library to be present on client machines. This driver is fully written in Java and hence Portable. It is suitable for the web. There are many opportunities to optimize portability, performance, and scalability. The net protocol can be designed to make the client JDBC driver very small and fast to load. The type 3 driver typically provides support for features such as caching (connections, query results, and so on), load balancing, and advanced system administration such as logging and auditing. This driver is very flexible allows access to multiple databases using one driver. They are the most efficient amongst all driver types. Disadvantage It requires another server application to install and maintain. Traversing the Recordset may take longer, since the data comes through the

Type 4 Native Protocol Drivers


1.

2.

3.

The Type 4 uses java networking libraries to communicate directly with the database server. Native Protocol drivers communicate directly with the DB They convert JDBC commands directly into the DBs native protocol No additional transformation or middleware layers, therefore has high performance Advantage The major benefit of using a type 4 JDBC drivers are that they are completely written in Java to achieve platform independence and eliminate deployment administration issues. It is most suitable for the web. Number of translation layers is very less i.e. type 4 JDBC drivers don't have to translate database requests to ODBC or a native connectivity interface or to pass the request on to another server, performance is typically quite good. You dont need to install special software on the client or server. Further, these drivers can be downloaded dynamically. Disadvantage With type 4 drivers, the user needs a different driver for each database.

Anda mungkin juga menyukai