Вы находитесь на странице: 1из 16

JDBC

JDBC Overview
Stands for Java Database Connectivity A standard interface for accessing data sources normally databases (works with Excel) A base for building higher level tools and APIs

JDBC Overview
Defined by
interfaces and classes in the java.sql package

Implemented by
data source specific JDBC drivers
accept JDBC calls and perform operations using the API of the specific database

the JDBC-ODBC bridge


a special JDBC driver included with JDK
3

JDBC Overview
Databases are specified with URL syntax

What is ODBC?

Why Doesnt Java Use ODBC Instead of JDBC?

written by Microsoft provides access to most popular relational databases

Problems with using ODBC directly


Relies on C code which violates Java security
applets cant use it

Translating ODBC into pure Java would be difficult due to its heavy use of pointers ODBC is harder to learn than JDBC
complex, rarely used operations coexist with common ones must learn a lot in order to use basic functionality with JDBC, uncommon operations are supported by separate interfaces from those that provide basic functionality
5

JDBC Overview
Design of JDBC
based on ODBC and the
X/OPEN SQL Call Level Interface

makes it easy for ODBC developers to learn

JDBC-ODBC Bridge
allows access to ODBC databases from Java applications

Ways to Utilize JDBC


Two Tier Three Tier

JDBC Architecture

When Java code requests a data source connection the DriverManager chooses the appropriate registered driver
determined from subprotocol in URL specification
ex. jdbc:odbc:MySource

Architecture With JDBC/ODBC Bridge

10

The Architecture of JDBC


Java Applications/ Applets

JDBC API

Oracle JDBC Driver

JDBC-ODBC Bridge Driver

Oracle ODBC Driver

Microsoft ODBC Driver

Local or remote ORACLE DB

Microsoft Access Database

11

The JDBC Interfaces


Driver

Loading drivers
Connection

Connection

Establishing connections
Statement

Statement

Statement

Statement

Creating and executing statements


Processing ResultSet

ResultSet

ResultSet

ResultSet

ResultSet

12

Developing JDBC Programs


Loading drivers
Establishing connections Creating and executing statements Processing ResultSet Statement to load a driver: Class.forName("JDBCDriverClass");

A driver is a class. For example:


Database Driver Class Source Access sun.jdbc.odbc.JdbcOdbcDriver Already in JDK MySQL com.mysql.jdbc.Driver Website Oracle oracle.jdbc.driver.OracleDriver Website The JDBC-ODBC driver for Access is bundled in JDK. MySQL driver class is in mysqljdbc.jar Oracle driver class is in classes12.jar To use the MySQL and Oracle drivers, you have to add mysqljdbc.jar and classes12.jar in the classpath using the following DOS command on Windows: classpath=%classpath%;c:\book\mysqljdbc.jar;c:\book\classes12.jar
13

Developing JDBC Programs


Loading drivers Connection connection = DriverManager.getConnection(databaseURL); Database Access MySQL Oracle URL Pattern jdbc:odbc:dataSource jdbc:mysql://hostname/dbname jdbc:oracle:thin:@hostname:port#:oracleDBSID See Supplement IV.D for creating an ODBC data source

Establishing connections
Creating and executing statements

Processing ResultSet

Examples: For Access:

Connection connection = DriverManager.getConnection ("jdbc:odbc:ExampleMDBDataSource");

For MySQL:
Connection connection = DriverManager.getConnection ("jdbc:mysql://localhost/test");

For Oracle:
Connection connection = DriverManager.getConnection ("jdbc:oracle:thin:@liang.armstrong.edu:1521:orcl", "scott", "tiger");
14

Developing JDBC Programs


Loading drivers Establishing connections

Creating statement:
Statement statement = connection.createStatement();

Executing statement (for update, delete, insert):


statement.executeUpdate ("create table Temp (col1 char(5), col2 char(5))");

Creating and executing statements


Processing ResultSet

Executing statement (for select):


// Select the columns from the Student table ResultSet resultSet = statement.executeQuery ("select firstName, mi, lastName from Student where lastName " + " = 'Smith'");

15

Developing JDBC Programs


Loading drivers Establishing connections Creating and executing statements Executing statement (for select):
// Select the columns from the Student table ResultSet resultSet = stmt.executeQuery ("select firstName, mi, lastName from Student where lastName " + " = 'Smith'");

Processing ResultSet (for select):


// Iterate through the result and print the student names while (resultSet.next()) System.out.println(resultSet.getString(1) + " " + resultSet.getString(2) + ". " + resultSet.getString(3));

Processing ResultSet

16

Оценить