Docsity

Java Database Connectivity with JDBC: Understanding Database Connectivity and JDBC API, Slides of Java Programming

Partial preview of the text

Download Java Database Connectivity with JDBC: Understanding Database Connectivity and JDBC API and more Slides Java Programming in PDF only on Docsity!

ava - programming

An Introduction

JDBC

J

docsity.com

Today’s Lecture

o Java Database Connectivity

 Concepts  JDBC API  Data Base Connectivity using Java  Important Features of JDBC API

docsity.com

JDBC

o Role of Driver

 JDBC API talks with vendor specific driver  Driver converts JDBC API calls to vendor specific Database API calls

o Merits and Demerits  Applications using JDBC API have no access to vendor specific Database API but to driver only  The performance is reduced  The same application can be used with another implementation of the same database for any other vendor for which a driver is available

docsity.com

JDBC Drivers

o Database drivers are classified into four types based on the nature of the driver provided  Bridges.  Convert from JDBC to another standard for example ODBC. That standard then converts into the vendor specific API.

 Part Java part Native  Driver contains some Java code and some Native code

 Intermediate Server  JDBC talks directly to a separate server which then converts from JDBC to native API (great for intranets)

 Pure Java  JDBC driver converts directly to native API and sends through vendor specific network protocol (great for intranets)

docsity.com

Java application (^) Data source

JDBC API JDBC-ODBC Bridge^ ODBC API ODBC Layer

application process

ODBC process

JDBC-ODBC Bridge

docsity.com

Part Java, Part Native Driver

  • JDBC driver consists of java code and partly native driver code which uses

vendor-specific API for accessing databases

  • Requires the JDBC driver with the correct native code for the database to

be loaded on the client

  • More efficient than JDBC-ODBC bridge due to fewer layers of

communication and translation

CCTM: Course material developed by James King ([email protected])

Intermediate Database Access Server

  • Database is accessed via an intermediate server often running on a different

machine or network from the client => good for intranet use.

  • Many database vendors are now supplying intermediate servers which directly

understand JDBC

  • JDBC driver on client talks to JDBC intermediate access server via a standard

protocol => no need for native driver on client

  • The intermediate server handles the request using a native driver

CCTM: Course material developed by James King ([email protected])

network

Java application Data source

JDBC API JDBC Driver

application process

JDBC Driver Server

Native Driver

Intermediate data access server: Gateway for multiple database server

Intermediate Database Access Server

docsity.com

The Driver manager

  • It is possible that a Java application may need to talk to multiple

databases, possibly from different vendors (especially if it is a

distributed system)

  • The JDBC driver manager provides the ability to communicate with

multiple databases and keep track of which driver is needed for which

database.

  • Even if you only talk to one database you need to connect to it via the

driver manager

JDBC Driver Manager

ODBC Oracle Driver XYZ Driver

Access Oracle xyzBase CCTM: Course material developed by James King ([email protected])

Java JDBC Programming Model

  • DriverManager allows you to connect to one or more to databases
  • Connection represents the connection to a single database
  • Statement contains an SQL statement to be sent down the connection to be executed

by the database

  • ResultSet contains the results of an SQL query such as SELECT

docsity.com

Java JDBC Programming Steps

  1. Load Driver.

The forName method of Class loads the specified JDBC driver and initialises the DriverManager.

  1. Connect to database. getConnection in DriverManager returns a Connection

Object. The Connection Object is used to communicate with the database

specified in getConnection.

  1. Execute SQL. createStatement in Connection returns an Statement Object

which can be used to send SQL statements to the connected database.

CCTM: Course material developed by James King ([email protected])

4 Use executeQuery or executeUpdate methods in Statement to search or modify

the database. (Searches produce a ResultSet object)

5 Process the ResultSet or handle any errors

6 Close Statement object and close the Connection when you have no more

statements to send.

docsity.com

Java JDBC Programming Steps

Statement executeQuery ResultSet

SQL

“Select blah blah”

SQL

“Delete blah blah”

Statement executeUpdate

Once you have a statement object you can send SQL queries and updates to the database. Queries return a ResultSet object and updates return the number of records affected.

int

SQL is placed into the statement object

Statement.executeQuery returns any found database records

SQL is placed into the statement object

Statement.executeUpda te returns the number of records affected

CCTM: Course material developed by James King ([email protected])

JDBC Drivers

o A JDBC driver is always needed to connect to a database

o Loading a driver requires the class name of the driver. For

 JDBC-ODBC: sun.jdbc.odbc.JdbcOdbcDriver  Oracle driver: oracle.jdbc.driver.OracleDriver  mySQL: org.gjt.mm.mysql.Driver

o The class for the name of the driver is loaded using the static

method forName in the class Class. (Class is in the package
java.lang and so imported automatically)

 Class.forName("org.gjt.mm.mysql.Driver");  This loads and initializes the driver.  It is possible to load several drivers.

 The class DriverManager manages the loaded driver(s)

docsity.com