Sie sind auf Seite 1von 13

Unit-viii

COMP201, Summer 2007


COMP201, Summer 2007

Database programming using JDBC


A database vendor provides a set of API for accessing the data managed by a the database server.
Oracle Sybase Informix etc

Client applications written in native languages such as c/c++ can use these APIs to get direct access to the data. The JDBC API provides an alternative to using these vendor specific APIs.

COMP201, Summer 2007


COMP201, Summer 2007

This eliminates the need for the java developer to access vendor-specific APIs. A JDBC driver is a middleware layer that translates the JDBC API calls to the vendor specific APIs.

COMP201, Summer 2007


COMP201, Summer 2007

There are two important JDBC driver implementations. The first approach is a JDBC-ODBC bridge. Second approach is a pure java implementation. A driver that uses the JDBC-ODBC bridge approach is known as a Type I driver. Jdk includes a JDBC-ODBC bridge to connect to databases.

COMP201, Summer 2007


COMP201, Summer 2007

Pure java drivers are known as Type IV driver.


The JDBC specification defines two other driver types , Type II and Type III.

COMP201, Summer 2007


COMP201, Summer 2007

The Architecture of JDBC


Java Applications/ Applets

JDBC API

Type iv

Oracle JDBC Driver

JDBC-ODBC Bridge Driver

Type i

Oracle ODBC Driver

Microsoft ODBC Driver

Local or remote ORACLE DB

Microsoft Access Database

COMP201, Summer 2007


COMP201, Summer 2007

The JDBC Interfaces


Driver

Loading drivers
Connection

Connection

Establishing connections
Statement

Statement

Statement

Statement

Creating and executing statements


Processing ResultSet

ResultSet

ResultSet

ResultSet

ResultSet

COMP201, Summer 2007


COMP201, Summer 2007

Accessing the database


1. Load the driver :
To load a driver , we will specify the class name of the database driver in the Class.forName method. By doing so , it automatically creates a driver instance and registers itself with
the JDBC driver manager

Database Access MySQL Oracle

Driver Class sun.jdbc.odbc.JdbcOdbcDriver com.mysql.jdbc.Driver oracle.jdbc.driver.OracleDriver

Source Already in JDK Website Website

Ex:Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
COMP201, Summer 2007
COMP201, Summer 2007

2. Define the Connection URL


A JDBC URL identifies an individual database in a driver-specific manner. Different drivers may need different information in the URL to specify the host database. JDBC URLs usually begin with jdbc:subprotocol:subname. For example, the Oracle JDBC-Thin driver uses a URL of the form of jdbc:oracle:thin:@host:port:dbName; the JDBC-ODBC bridge uses jdbc:odbc:datasourcename;

Examples: String oracleURL=jdbc:oracle:thin:@+host+:+port+:+dbName; String sybaseURL=jdbc:sybase:Tds:@+host+:+port+:+dbName; String msAccessURL=jdbc:odbc:+dbName;

COMP201, Summer 2007


COMP201, Summer 2007

3. Establishing the connection


1. To make the actual network connection, pass the URL, database username and database password to the getConnection method of the Driver manager class. Ex:Connection con= DriverManager.getConnection(oracleURL, username, password );

COMP201, Summer 2007


COMP201, Summer 2007

10

4. Create a statement object Statement object is used to send queries to the database.
Ex:Statement stmt=connection.createStatement();

COMP201, Summer 2007


COMP201, Summer 2007

11

5. Execute a query Once we have a statement object , we can use it to send SQL queries by using the execute query method. Ex:String query=select *from emp; ResultSet rs=stmt.executeQuery(query);

COMP201, Summer 2007


COMP201, Summer 2007

12

5. Process the Results The simplest way to process the results is to use the next method of ResultSet to move through the table a row at a time.

COMP201, Summer 2007


COMP201, Summer 2007

13

Das könnte Ihnen auch gefallen