You are on page 1of 22

JDBC Programming in Java

MSIT
1
JDBC
• JDBC stands for Java database connectivity.

• Java Database Connectivity (JDBC) provides Java


developers with a standard API that is used to access
databases, regardless of the driver and database
product.

MSIT 2
Architecture

MSIT 3
JDBC Architecture

Application JDBC Driver

• Java code calls JDBC library


• JDBC loads a driver
• Driver talks to a particular database
• Can have more than one driver -> more than one database
• Ideal: can change database engines without changing any
application code

MSIT 4
JDBC Goals
• 100% Pure Java
• Keep it simple
• High-performance
• Use multiple methods to express multiple
functionality
• JDBC connections support creating and executing
statements. These may be update statements such
as SQL's CREATE, INSERT, UPDATE and
DELETE,

MSIT 5
JDBC Drivers
• JDBC Drivers are mainly divides in to four types:

Java JDBC-ODBC
application Bridge
JDBC-API
JDBC-
Driver manager
Native-
API/partly
Native JDBC- JDBC-ODBC Native Java driver
Protocol driver Net-driver bridge API-driver
Net-protocol/all-
DB-
ODBC Client library Java driver
Middleware

Client library Native-


protocol/all-
Java driver

MSIT 6
JDBC Drivers (Fig.)

Type I ODBC
ODBC
“Bridge” Driver

Type II
JDBC CLI (.lib)
“Native”

Type III Middleware


“Middleware” Server

Type IV
“Pure”
MSIT 7
Type I Drivers
• 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 uses this type of driver.

• This kind of driver is generally most appropriate


when automatic installation and downloading of a
Java technology application is not important.

MSIT 8
Type II Drivers
• Native API drivers
• Requires installation/configuration on client machines
• Used to leverage existing CLI libraries
• Usually not thread-safe
• Mostly obsolete now
• e.g. Intersolv Oracle Driver, Web Logic drivers

MSIT 9
Type III Drivers

• Calls middleware server, usually on database host


• Very flexible -- allows access to multiple databases
using one driver
• These drivers use a networking protocol and
middleware to communicate with a server.
• The server then translates the protocol to DBMS
function calls specific to DBMS.
e.g. Symantec DBAnywhere

MSIT 10
Type IV Drivers
• 100% Pure Java.
• Type 4 drivers are all Java drivers.
• Use Java networking libraries to talk directly to
database engines
• Only disadvantage: need to download a new driver
for each database engine
• e.g. Oracle, mSQL

MSIT 11
JDBC Limitations
• No scrolling cursors
• No bookmarks

MSIT 12
JDBC Object Classes
• Driver Manager
– Loads, chooses drivers
• Driver
– connects to actual database
• Connection
– a series of SQL statements to and from the DB
• Statement
– a single SQL statement
• Result Set
– the records returned from a Statement

MSIT 13
JDBC Class Usage
DriverManager

Driver

Connection

Statement

ResultSet

MSIT 14
Basic steps to use a database in
Java
• Establish a connection
• Create JDBC Statements
• Execute SQL Statements
• GET Result Set
• Close connections

MSIT 15
Establish a connection
• import java.sql.*;
• Load the vendor specific driver
– Class.forName("oracle.jdbc.driver.OracleDriver");
• What do you think this statement does, and how?
• Dynamically loads a driver class, for Oracle database
• Make the connection
– Connection con =
DriverManager.getConnection( "jdbc:oracle:thin:@orac
le-prod:1521:OPROD", username, passwd);
• What do you think this statement does?
• Establishes connection to database by obtaining
a Connection object
MSIT 16
Create JDBC statement(s)
• Statement stmt = con.createStatement() ;

• Creates a Statement object for sending SQL


statements to the database

MSIT 17
Executing SQL Statements
• String createLehigh = "Create table Lehigh " +
"(SSN Integer not null, Name VARCHAR(32), " +
"Marks Integer)";
stmt.executeUpdate(createLehigh);
//What does this statement do?

• String insertLehigh = "Insert into Lehigh values“ +


"(123456789,abc,100)";
stmt.executeUpdate(insertLehigh);

MSIT 18
Get ResultSet
String queryLehigh = "select * from Lehigh";
ResultSet rs = Stmt.executeQuery(queryLehigh);
//What does this statement do?

while (rs.next()) {
int ssn = rs.getInt("SSN");
String name = rs.getString("NAME");
int marks = rs.getInt("MARKS");
}
MSIT 19
Close connection
• stmt.close();
• con.close();

MSIT 20
JDBC Class Diagram

MSIT 21
JDBC
• JDBC Data Access API – JDBC Technology Homepage
– http://java.sun.com/products/jdbc/index.html
• JDBC Database Access – The Java Tutorial
– http://java.sun.com/docs/books/tutorial/jdbc/index.html
• JDBC Documentation
– http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/index.html
• java.sql package
– http://java.sun.com/j2se/1.4.2/docs/api/java/sql/package-summary.html
• JDBC Technology Guide: Getting Started
– http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/getstart/GettingStartedTOC.fm.html
• JDBC API Tutorial and Reference (book)
– http://java.sun.com/docs/books/jdbc/

MSIT 22

You might also like