JDBC Driver Manager The JDBC DriverManager class defines objects which can connect Java applications to a JDBC

driver. DriverManager has traditionally been the backbone of the JDBC architecture. It is quite small and simple. This is a very important class. Its main purpose is to provide a means of managing the different types of JDBC database driver. On running an application, it is the DriverManager's responsibility to load all the drivers found in the system property jdbc. drivers. For example, this is where the driver for the Oracle database may be defined. This is not to say that a new driver cannot be explicitly stated in a program at runtime which is not included in jdbc.drivers. When opening a connection to a database it is the DriverManager' s role to choose the most appropriate driver from the previously loaded drivers. 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 Driver This topic defines the Java(TM) Database Connectivity (JDBC) driver types. Driver types are used to categorize the technology used to connect to the database. A JDBC driver vendor uses these types to describe how their product operates. Some JDBC driver types are better suited for some applications than others. Types of JDBC drivers This topic defines the Java(TM) Database Connectivity (JDBC) driver types. Driver types are used to categorize the technology used to connect to the database. A JDBC driver vendor uses these types to describe how their product operates. Some JDBC driver types are better suited for some applications than others. There are four types of JDBC drivers known as: JDBC-ODBC bridge plus ODBC driver, also called Type 1. Native-API, partly Java driver, also called Type 2. JDBC-Net, pure Java driver, also called Type 3. Native-protocol, pure Java driver, also called Type 4. Type 1 Driver- the JDBC-ODBC bridge
y y y y

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 class and comes with the Java 2 SDK, Standard Edition. The driver is platform-dependent as it makes use of ODBC which in turn depends on native libraries of the operating system. Also, using this driver has got other dependencies such as ODBC must be installed on the computer having the driver and the database which is being connected to must support an ODBC driver. Hence the use of this driver is discouraged if the alternative of a pure-Java driver is available. Type 1 is the simplest of all but platform specific i.e only to Microsoft platform. A JDBC-ODBC bridge provides JDBC API access via one or more ODBC drivers. Note that some ODBC native code and in many cases native database client code must be loaded on each client machine that

Informix. Client -> JDBC Driver -> ODBC Driver -> Database 4. Type 2 drivers are generally faster than Type 1 drivers. Functions: 1. For platform interoperability. being a full-Java implementation.JdbcOdbcDriver. can be accessed. Advantages: Almost any database for which ODBC driver is installed. 2. Note that.uses this type of driver. The Java Native Interface (JNI) is used to call ODBC functions from the JDBC driver.the Native-API Driver The JDBC type 2 driver. then to the native database connectivity interface. The ODBC driver needs to be installed on the client machine. You cannot use a Type 2 driver in an applet since applets cannot load native code. 3. see JDBC-ODBC Bridge Driver. 3. this might not be suitable for applets. Type 1 drivers cannot be used in an applet since applets cannot load native code.jdbc. The type 2 driver is not written entirely in Java as it interfaces with non-Java code that makes the final database calls. this kind of driver is generally most appropriate when automatic installation and downloading of a Java technology application is not important. which is then handled by the ODBC driver. Type 2 drivers need native binary code installed and configured to work. The driver is compiled for use with the particular operating system. or other DBMS. This is an advantage because ODBC drivers exist for many Relational Database Management System (RDBMS) platforms. Performance overhead since the calls have to go through the JDBC overhead bridge to the ODBC driver. However the type 2 driver provides more functionality and performance than the type 1 driver as it does not have the overhead of the additional ODBC function calls. This can be a serious drawback for a production application. Sun provides a JDBC-ODBC Bridge driver. and is closed source. The driver converts JDBC method calls into native calls of the database API. A Type 2 driver also uses the JNI. Hence. Considering the client-side software needed. Type 2 Driver . Type 2 drivers use a native API to communicate with a database system. Type 1 drivers are "bridge" drivers.odbc. Java native methods are used to invoke the API functions that perform database operations. They use another technology such as Open Database Connectivity (ODBC) to communicate with a database. the Type 4 driver. 2. There is some overhead associated with the translation work to go from JDBC to ODBC. DB2. Sybase. A Type 2 JDBC . sun. Translates query obtained by JDBC into corresponding ODBC query. For information on the JDBC-ODBC bridge driver provided by Sun. A Type 1 driver needs to have the bridge driver installed and configured before JDBC can be used with it. also known as the Native-API driver is a database driver implementation that uses the client-side libraries of the database. this style of driver requires that some binary code be loaded on each client machine. Disadvantages: 1. like the bridge driver. This driver is native code and not Java. is preferred over this driver. A native-API partly Java technology-enabled driver converts JDBC calls into calls on the client API for Oracle.

this is the most flexible JDBC API alternative. 5. communicates with a middleware-net-server using a database independent protocol. It depends on the number of databases the middleware has been configured to support. auditing etc. query results. Follows a three tier communication approach. Disadvantages 1. Not all databases give the client side library. 4. The specific protocol used depends on the vendor. that the Web imposes. eg. Type 3 JDBC drivers are the most flexible JDBC solution because they do not require any native binary code on the client. Also the client to middleware need'nt be changed for a new database. making use of the middleware provides additional advantages of security and firewall access. logging. The Middleware Server (Can be a full fledged J2EE Application server) can provide typical middleware services like caching (connections. Functions: 1. Can interface to multiple databases . 2. The type 3 driver is platform-independent as the platformrelated differences are taken care by the middleware. 3. . The server then translates the protocol to DBMS function calls specific to DBMS. The same driver can be used for multiple databases. Client -> JDBC Driver -> Vendor Client DB Library -> Database Advantage Better performance than Type 1 since no jdbc to odbc translation is needed. 2. Cannot be used in internet due the client side software needed. The JDBC Client driver written in java. Since the communication between client and the middleware server is database independent. Client -> JDBC Driver -> Middleware-Net Server -> Any Database Advantages 1. 2. In order for these products to also support Internet access they must handle the additional requirements for security. there is no need for the vendor db library on the client machine. and then this net server translates this request into database commands for that database. Also. However.. The middle-tier (application server) converts JDBC calls directly or indirectly into the vendor-specific database protocol. This differs from the type 4 driver in that the protocol conversion logic resides not at the client. 2.Not vendor specific.the Network-Protocol Driver The JDBC type 3 driver. load balancing. 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. access through firewalls. It is likely that all vendors of this solution will provide products suitable for Intranet use. 3. Several vendors are adding JDBC technology-based drivers to their existing database middleware products. etc. but in the middle-tier. This net server middleware is able to connect all of its Java technology-based clients to many different databases. Type 3 driver . 3. the type 3 driver is written entirely in Java. A net-protocol fully Java technology-enabled driver translates JDBC API calls into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server..driver may require some Database Management System (DBMS) networking software to be installed. for the above include jdbc driver features in Weblogic. This type of driver converts JDBC calls into calls to the client API for that database. The vendor client library needs to be installed on the client machine. Thus the client driver to middleware communication is database independent. A Type 3 driver does not need any client installation. In general. and so on). The Developer Kit for Java JDBC driver is a Type 2 JDBC driver. Functions: 1. like type 4 drivers. These drivers use a networking protocol and middleware to communicate with a server.

Several database vendors have these in progress. This means that there is no client installation or configuration. 2. Disadvantage At client side. Completely implemented in Java to achieve platform independence. No translation or middleware layers. DBMS vendors are generally the only companies providing a Type 4 JDBC driver. also known as the native-protocol driver is a database driver implementation that converts JDBC calls directly into the vendor-specific database protocol. OracleDriver which connect to jdbc:oracle:thin URL format.driver. Client Machine -> Native protocol JDBC Driver -> Database server Advantages These drivers don't translate the requests into db request to ODBC or pass it to client api for the db. Type 4 . improving performance. Functions 1. a separate driver is needed for each database. 5. a Type 4 driver may not be suitable for some applications if the underlying protocol does not handle issues such as security and network connectivity well. However. 3. The driver converts JDBC calls into the vendor-specific database protocol so that client applications can communicate directly with the database server. A Type 4 driver uses Java to implement a DBMS vendor networking protocol. It is installed inside the Java Virtual Machine of the client. But typically this is overcome by providing efficient middleware services described above. separate drivers. The IBM Toolbox for Java JDBC driver is a Type 4 JDBC driver. The type 4 driver is written completely in Java and is hence platform independent. Type 4 drivers are all Java drivers. Unlike the type 1 and 2 drivers. As the database protocol is vendor-specific.(It works provided the middlware supports that database!!) Disadvantages 1. indicating that the API is a pure Java networking protocol driver. Since the protocols are usually proprietary. 2. Can be used in internet since there is no client side software needed. need to be used to connect to the database. Requires database-specific coding to be done in the middle tier. e. nor do they need a middleware layer for request indirection. 4. At client side a single driver can handle any database. A native-protocol fully Java technology-enabled driver converts JDBC technology calls into the network protocol used by DBMSs directly. This allows a direct call from the client machine to the DBMS server and is a practical solution for Intranet access. usually vendor-supplied.g include the widely used Oracle thin driver . Since many of these protocols are proprietary the database vendors themselves will be the primary source for this style of driver. It provides better performance over the type 1 and 2 drivers as it does not have the overhead of conversion of calls into ODBC or database API calls. are required. 5.oracle. . An extra layer added may result in a time-bottleneck. it does not need associated software to work.the Native-Protocol Driver The JDBC type 4 driver. Thus the performance is considerably improved.jdbc. Type 4 drivers are entirely written in Java that communicate directly with a vendor's database through socket connections.4.

Right side. The graphic below illustrates JDBC connectivity using ODBC drivers and existing database client libraries.The JDBC API is the industry standard for database-independent connectivity between the Java programming language and a wide range of databases. allowing a direct call from the client machine to the DBMS server and providing a practical solution for intranet access. JDBC API Overview The JDBC API makes it possible to do three things: y y y Establish a connection with a database or access any tabular data source Send SQL statements Process the results JDBC Architecture The JDBC API contains two major sets of interfaces: the first is the JDBC API for application writers. and the second is the lower-level JDBC driver API for driver writers. The JDBC APIprovides a call-level API for SQL-based database access. Type 1: JDBC-ODBC Bridge plus ODBC Driver This combination provides JDBC access via ODBC drivers. ODBC binary code -. database client code -. JDBC technology drivers fit into one of four categories. Applications and applets can access databases via the JDBC API using pure Java JDBC technology-based drivers. Run Anywhere" capabilities for applications that require access to enterprise data. Sun provides a JDBC - .must be loaded on each client machine that uses a JDBC -ODBC Bridge. which is then transla ted to a DBMS protocol by a middleware server. JDBC technology allows you to use the Java programming language to exploit "Write Once. Type 3: Pure Java Driver for Database Middleware This style of driver translates JDBC calls into the middleware vendor's protocol. Type 4: Direct-to-Database Pure Java Driver This style of driver converts JDBC calls into the network protocol used directly by DBMSs. Left side.and in many cases. The middleware provides connectivity to many different databases. as shown in this figure: Left side.

Sybase. Database Connection Identified by URL JDBC technology exploits the advantages of Internet -standard URLs to identify database connections. businesses are not locked in any proprietary architecture. Type 2: A native API partly Java technology-enabled driver This type of driver converts JDBC calls into calls on the client API for Oracle. that makes code even more portable and easier to maintain. JDBC hides the complexity of many d ata access tasks. Industry Momentum Leading database. which is appropriate for experimental use and for situations in which no other driver is available. middleware and tool vendors have been building support for JDBC technology into many new products. open interface for Java applications to access databases. Right side. please see the article published in Computerworld . this style of driverrequires that some binary code be loaded on each client machine. The JDBC API is simple to learn. doing most of the "heavy lifting"for the programmer behind the scenes. DB2. . Informix. or other DBMS. like the bridge driver. it is automatically downloaded as part of the applet that makes the JDBC calls. See the Industry Support page for a list of companies that are shipping pro ducts with support for JDBC technology. For comparison of driver types.ODBC Bridge driver. Zero configuration for clients supports the network computing paradigm and centralizes software maintenance. and inexpensive to maintain. No Installation A pure JDBC technology -based driver does not require special installation. The JDBC API includes an even better way to identify and connect to a data source. With a driver written in the Java programming language. no configuration is require d on the client side. easy to deploy. Note that. and can continue to use their installed databases and access information easily -. This ensures that customers can build portable Java applications while choosing from a wide range of competitive products for the solution best suited to their needs. u sing a DataSource object. Key Features Full Access to Metadata The JDBC API provides metadata access that enables the development of sophisticated applications that need to understand the underlying facilities and capabilities of a specific database connection. Partnering for Progress Sun worked with an array of companies in the industry to create and rapidly establish the JDBC API as the industry-standard. Simplified Enterprise Development The combination of the Java API and the JDBC API makes application development easy and economical. Advantages of JDBC Technology Leverage Existing Enterprise Data With JDBC technology. all the information needed to make a connection is completely defined by the JDBC URL or by a DataSource object registered with a JavaNaming and Directory Interface (JNDI) naming service.even if it is stored on different database management systems. Zero Configuration for Network Computers With the JDBC API.

An example of a J2EE based architecture that includes a JDBC implementation: Requirements y y Software: The Java 2 Platform (either the Java 2 SDK. The biggest advantage of programming in Java is its platform independence. providing server -side functionality for industrial strength scalability. essential for enterprise database computing. Standard Edition (J2SE) and the Java 2 Platform. Enterprise Edition). an SQL database. Enterprise Edition (J2EE). What is JDBC? Java Database Connectivity or JDBC for short is set of Java API's that enables the developers to create platform and database independent applications in java. Hardware: Same as for the Java 2 Platform. and a JDBC technology -baseddriver for that database. Standard Edition. This functionality is provided transparently to the programmer. Included in the Java Platform As a core part of the Java 2 Platform. DataSource objects can provide connection pooling and distributed transactions. The JDBC API is included in both the Java 2 Platform.In addition to this important advantage. An application written to access the MS Access database on Win 95/Win NT platform can work on Linux against Oracle database. provided none of the database calls it makes are . the JDBC API is available anywhere that the platform is. Introduction to the JDBC Introduction This article introduce you with JDBC and shows you how to our search engine with database. This means that your applications can truly write database applications once and access data anywhere. only by changing the name of driver. or theJava 2 SDK.

The connection can be established with the help of getConnection() method available in DriverManager class.sun.sql. import java.vendor specific. Where DSN-Data Source Name When the statement is executed the connection is set to the Connection object cn. This type of driver communicates to a middleware component which in turn connects to database and provide database connectivity. A list of currently available JDBC drivers can be found at http://java.sql package . What are JDBC Drivers? JDBC Drivers are set of classes that enables the Java application to communicate with databases. Here a thin code of Java wrap around the native code and converts JDBC commands to DBMS-specific native calls.drivers. These are: 1. Here no translation or middleware layer. The actual implementations are done in third-party drivers.*.jdbc. Type 4: Native-Protocol Pure Java Driver Type 4 drivers are entirely written in Java that communicate directly with vendor's database through socket connection.forName(³sun.Driver interface in their database driver. Step 3: Establishing the connection Establishing connection means making a connection to access RDBMS through JDBC driver. Process the result 7.sql that ships with JDK contains various classes for using relational databases.com/products/jdbc/jdbc. The first type of JDBC driver is JDBC-ODBC Bridge which provide JDBC access to any ODBC complaint databases through ODBC drivers. Type 1: JDBC-ODBC Bridge Driver 2. The statement used to load and register the JDBC-ODBC bridge driver is Class.html JDBC Drivers Types Sun has defined four JDBC driver types. Sun's JDBC-ODBC bridge is example of type 1 driver. Java. Close the connection Step1: Import java.sql package The important classes and interfaces of JDBC are available in the java. Type 2: Native -API Partly . The established connection is then set to new connection object. Import the package 2. are required which improves performance tremendously. Connection cn=DriverManager. Step 4: Creating the statement A Statement object is used for executing a static SQL statement and obtaining the results produced by it.getConnetion (³jdbc:odbc:DSN´. JDBC Fundamentals The java program can access the database using JDBC. In an application the user can register more than one driver.getConnection (³jdbc:odbc:mydata´). 3. Create a Statement object 5.sql. But these classes do not provide any implementation. The methods used for creating the statements are y Statement createStatement() Returns a new Statement object. Type 3: JDBC-Net Pure Java Driver Type 3 drivers are a three-tier solutions. the seven steps that are needed to access database are 1. For example to connect the ODBC data source named mydata via JDBC ±ODBC bridge the statement is Connection cn= DriverManager. The createStatement() method is used to create statements using the established connection. ´administrator´.Used for general queries . Execute a query 6.odbc. ³password´).JdbcOdbcDriver´). Establish the Connection 4. 4.Java Driver Type 2 drivers are developed using native code libraries. Step 2: Loading and registering the driver In order to access database it must have the particular driver for that database. Load and register the driver 3. So inorder to access the databases it must be imported in our application. only the behaviors are defined. which were originally designed for accessing the database through C/C++.forName(³path with driver name´). In Java we have to inform the Drive Manager about the required driver this is done with the help of the method forName(String str) available in the class named Class Class. Third party vendors implements the java.

y y y y y y y y y y PreparedStatement prepareStatement(String sql) Returns a new PreparedStatement object.The table rows are retrieved in sequence. A ResultSet provides access to a table of data generated by executing a Statement. public boolean last() Moves the cursor to the last row in this ResultSet object.For a statement called multiple times with different values (precompiled to reduce parsing time) CallableStatement prepareCall(String sql) Returns a new CallableStatement object for stored procedures Step 5: Executing the Query After making connection to the database we are ready to execute the SQL statements. Step 6: Retrieving the result. Returns the number of rows changed. The various methods available in Statement object to execute the query are ResultSet executeQuery(String) Execute a SQL statement that returns a single ResultSet. Fields indexed starting at 1 (not 0) Type getType(String columnName) Returns the data at the specified column In the above methods the Type should be replaced by the valid datatypes such as Boolean. A ResultSet maintains a cursor pointing to its current row of data. The 'next' method moves the cursor to the next row. Int.close().Only one ResultSet per Statement can be open at once. . After executing the SQL statements the requested data is stored in the ResultSet object. getInt(column index) etc.close(). public boolean next() Moves the cursor down one row from its current position. int executeUpdate(String) Execute a SQL INSERT. Long. The methods used to retrieve the values from the current row Type getType(int columnIndex) Returns the given field as the given type. UPDATE or DELETE statement. Float etc as getString(Column name). cn. Step 7: Closing the connection and statement When the client request is completed we have to close the created objects of Connection and Statement using close() method. String. st. boolean execute(String) Execute a SQL statement that may return multiple results. Methods: public boolean first() Moves the cursor to the first row in this ResultSet object.