0% found this document useful (0 votes)
37 views

JDBC

The document discusses Java and databases. It describes the client-server architecture, where clients request services from servers. It then covers JDBC (Java Database Connectivity), the API for connecting Java applications to databases. JDBC uses drivers to communicate with different database types. There are four main driver types: Type I uses ODBC, Type II uses native APIs, Type III is 100% Java and uses a middleware server, and Type IV is also 100% Java and connects directly to databases. The document outlines the steps to use JDBC including identifying and registering drivers, getting database connections, creating statements, and retrieving result sets.

Uploaded by

api-26757811
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

JDBC

The document discusses Java and databases. It describes the client-server architecture, where clients request services from servers. It then covers JDBC (Java Database Connectivity), the API for connecting Java applications to databases. JDBC uses drivers to communicate with different database types. There are four main driver types: Type I uses ODBC, Type II uses native APIs, Type III is 100% Java and uses a middleware server, and Type IV is also 100% Java and connects directly to databases. The document outlines the steps to use JDBC including identifying and registering drivers, getting database connections, creating statements, and retrieving result sets.

Uploaded by

api-26757811
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Java and Database

Contents

• Client-Server
• JDBC
• JDBC Drivers
• JDBC API
Client-Server

The Architecture
Introduction

• A Client is defined as a requester of services


and a Server is defined as the provider of
services.
• The term client/server was first used in the
1980s in reference to personal computers
(PCs) on a network.
• The client/server architecture is a versatile
and modular infrastructure.
Cont…

• It is intended to improve usability, flexibility,


interoperability, and scalability of an
application.
Advantages

• This approach introduced a DB server to


replace the file server.
• Using a relational database management
system (DBMS), user queries could be
answered directly.
• This architecture reduced network traffic by
providing a query response rather than total
file transfer.
Cont…

• It improves multi-user updating through a GUI


front end to a shared database.
• In this architectures standard query language
(SQL) statements are typically used to
communicate between the client and server.
Architectures

• This architecture of Client-Server is referred


to as Application Tier architecture.
• Few well known are
• Two-Tier
• Three-Tier
• N-Tier
Two-Tier Architecture

Client Tier Data Tier

GUI Client Database


Storage
Three-Tier Architecture

Client Tier Middle Tier Data Tier

Web Client
Business Database
Logic Storage
GUI Client
N-Tier Architecture

Client Tier Middle Tier Data Tier


Business
Logic
Web Client
Transaction Database
Manager Storage
GUI Client

Message
Server
JDBC

The API
Introduction

• Java Database Connectivity API


• API=Application Programmer’s Interface
• Java specific PI to Databases
• Compare to ODBC
Cont…

• JDBC is overall API for connecting to


databases
• Low-level
• JDBC Driver developers use JDBC to make JDBC
drivers
• High-level
• Java developers use JDBC to call JDBC driver to
access databases
Why JDBC???

• DBMS independent interface


• generic SQL database access framework
• uniform interface to different data sources
JDBC – Just another API

• It’s a series of inter-connected classes & interfaces


• It’s just Java
• JDBC drivers actually do all of the real work
• Contain routines for connecting, querying, etc for
its database
• Explicitly or Implicitly create instance of driver
JDBC Drivers

Types
Types of JDBC Drivers

• There are four basic types of drivers available


for JDBC
• Each one has a specific use
• Each one has specific limitation
Type I

• This driver type is the JDBC-ODBC bridge


• It is limited to running locally
• Must have ODBC installed on computer
• Must have ODBC driver for specific database
installed on computer
• Generally can’t run inside an applet because
of Native Method calls
JDBC-ODBC Bridge Driver

Client Machine

Java Application

JDBC-ODBC Bridge
Server Machine

ODBC Driver Database


Server

Vendor DB Library DB
Type II

• Native Database library driver


• Uses Native Database library on computer to
access database
• Generally can’t run inside an applet because of
Native Method calls
• Must have database library installed on client
• example: DB-lib for Sybase, Oracle, MS-SQL
server
Native-API/Partly Java Driver

Client Machine

Java Application

Native-API/ Partly Server Machine


Java Driver

Database
Server

Vendor DB Library DB
Type III

• 100% Java Driver, NO native methods


• Does NOT require pre-installation on client
• Can be downloaded and configured ‘on-the-fly’
just like any Java class file
• Uses a proprietary protocol for talking with a
middleware server
• Middleware server converts from proprietary
calls to DBMS specific calls
Net-Protocol/All-Java Driver

Client Machine

Java Application

Server Machine
Database
Server
Net-Protocol/ All- Middleware/
Java Driver Application
Server DB
Type IV

• 100% Java Driver, NO native methods


• Does NOT require pre-installation on client
• Can be downloaded and configured ‘on-the-
fly’ just like any Java class file
• Unlike Type III driver, talks DIRECTLY with
DBMS server
• Converts JDBC calls directly to database
specific calls
Native-Protocol/Pure Java Driver

Client Machine

Java Application

Server Machine

Database
Server

Native-Protocol/
Pure Java Driver DB
Which driver to use???

• Type I driver is handy for prototyping


• Type III driver adds security, caching, and
connection control
• Type III and Type IV drivers need no pre-
installation
• Preferred by 9 out of 10 Java developers:
Type IV
JDBC Portability

• Type IV and Type III drivers are truly portable


• Other two aren’t portable across OS platforms
JDBC API

Programming
Introduction

• JDBC API package is java.sql


• There are few steps to be remembered
• Identifying appropriate driver
• Registering driver to the driver manager
• Requesting connection to the manager
• Creating statement
• Getting the result back
Identifying driver

• There are four JDBC driver types


• We’ll be either using I or IV
• For Type-I the driver name is
• sun.jdbc.odbc.JdbcOdbcDriver
• For Type-IV Oracle driver is
• oracle.jdbc.driver.OracleDriver
• For Type-IV DB2 driver is
• com.ibm.db2.jdbc.DB2Driver
Where to find the driver

• JDBC-ODBC Bridge driver comes along the


JDK
• Oracle driver can be found in oracle installed
directory hierarchy
• ora9x/jdbc/lib/classes12.jar
• IBM DB2 driver can be found in db2 installed
directory hierarchy
• SQLLIB/java/db2java.jar
Registering driver

• There are couple of ways to register a JDBC


driver to the driver manager
• Class.forName()
• It takes the classpath as a String
• DriverManager.registerDriver()
• It needs the handle of the driver instance
Requesting connection

• Now here onwards we’ll be using interfaces


from the java.sql package
• DriverManager class method getConnection()
is used to request the connection
• Connection is an interface the handle of which
reference to the connection given by the
manager
Cont…

• For getting connection we have to provide a


URL which differ from driver to driver
• Jdbc-Odbc bridge driver
• jdbc:odbc:dsn_name
• Oracle Type-IV driver
• jdbc:oracle:thin:@host:port:dbname
• IBM DB2 Type-IV driver
• jdbc:db2://host:port//dbname
Creating statement

• Here it doesn’t only means writing a SQL


query it also has more in it
• Three types of statements can be created
using JDBC API
• Statements can be only after getting the
connection object
• Connection interface provides methods to
create statements of all types
Cont…

• Statement (I)
• For performing sql queries and Batch of
statements
• PreparedStatement (I)
• For creating Pre-Compiled sql queries
• CallableStatement (I)
• For calling Stored Procedures and functions
Getting Result

• For getting and working with the result of the


SQL query we have an interface in out API
• ResultSet (I)
• It stores the result of the executed statement.
• It is keeping the record in a logical table format i.e.
in rows and cols
Thank You

Zubair Shaikh
[email protected]
Presentation Version 1.1

You might also like