JDBC ( Java Database Connectivity)

Preview:

DESCRIPTION

Prepared by : Raithatha Yash Roll No : 6540. JDBC ( Java Database Connectivity). What is JDBC ?. A java API that provides cross DBMS connectivity to a wide varity of SQL Databases It contains various java classes & interfaces which can be implemented by vendors of different DBMS - PowerPoint PPT Presentation

Citation preview

Prepared by :Raithatha YashRoll No : 6540

What is JDBC ?

A java API that provides cross DBMS connectivity to a wide varity of SQL Databases

It contains various java classes & interfaces which can be implemented by vendors of different DBMS

It is included in J2SE & J2EE both

Yash Raithatha ,6540

History

Microsoft ODBC ODBC uses CLI specifications from

SQL Access group ODBC in C so no Platform

independency Sun decided to make such an API

through JAVA code

Yash Raithatha ,6540

JDBC Driver

The JDBC Driver provides vendor-specific implementations of the abstract classes & interfaces provided by the JDBC API.

Used to connect to the database. To connect different DBMS we need

to have the corresponding JDBC driver which is generally provided by the vendor of DBMS

Yash Raithatha ,6540

Yash Raithatha ,6540

JDBC Architecture

Java code calls JDBC library JDBC loads a driver Driver talks to a particular databaseCan have more than one driver -> more

than one databaseBest : can change database engines

without changing any application code

Yash Raithatha ,6540

Application JDBC Driver

JDBC Drivers

Type I: “Bridge” Type II: “Native” Type III: “Middleware” Type IV: “Pure”

Yash Raithatha ,6540

Type I Drivers i.e Bridge Converts JDBC calls into ODBC

function calls which in turn communicates with the ODBC driver

ODBC driver manipulates the database

Yash Raithatha ,6540

JDBC ODBCODBC Driver Data

Source

Advantages & disadvantages

Advantagesif JDBC driver for any DBMS is not

availableeasy to install

DisadvantagesODBC driver needs to be installed on

client Application will become platform

dependent as ODBC driver is bound with machine

Performance overhead

Yash Raithatha ,6540

Type II Drivers i.e Native

Converts JDBC calls into native Database API calls using CLI library

Not written entirely in java Usually not thread-safe Mostly obsolete now e.g. Intersolv Oracle Driver, WebLogic

drivers

Yash Raithatha ,6540

JDBCNative DB API Data

source

Advantages & disadvantages

AdvantagesBetter Performance than type 1

driverDisadvantages

No Platform independency Vendor client library needs to be

installed on the client

Yash Raithatha ,6540

Type III Drivers i.e. Middleware Convert JDBC calls into server

protocol which in turn converts them to DBMS specific protocol

Written entirely in java Best :Only need to download one

driver to support multiple databasee.g. Symantec DBAnywhere

Yash Raithatha ,6540

MiddleWare

Yash Raithatha ,6540

JDBC

Middleware

Server

DS1

DS 2

DS 3

Advantages & disadvantages

AdvantagesNo need for vendor db library on

client machinemiddle ware services like caching ,

loading ,auditing , etcDisadvantage

database specific coding must be done in middle ware server

Yash Raithatha ,6540

Type IV Drivers

100% Pure Java -- the Holy Grail 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

Yash Raithatha ,6540

JDBC Data Source

JDBC Drivers (Fig.)Yash Raithatha ,6540

JDBC

Type I“Bridge”

Type II“Native”

Type III“Middleware”

Type IV“Pure”

ODBCODBCDriver

CLI (.lib)

MiddlewareServer

JDBC URLs

jdbc:subprotocol:source each driver has its own subprotocol each subprotocol has its own syntax

for the sourcejdbc:odbc:DataSource

e.g. jdbc:odbc:Northwind

jdbc:msql://host[:port]/database e.g. jdbc:msql://foo.nowhere.com:4333/accounting

Yash Raithatha ,6540

java.sql

JDBC is implemented via classes & interfaces in the java.sql package

Yash Raithatha ,6540

JDBC Object Classes

DriverManager Loads, chooses drivers

Driver connects to actual database

Connection a series of SQL statements to and from the

DBStatement

a single SQL statementResultSet

the records returned from a Statement

Yash Raithatha ,6540

DriverManager

DriverManager tries all the drivers Uses the first one that works When a driver class is first loaded, it

registers itself with the DriverManager

Therefore, to register a driver, just load it!

Yash Raithatha ,6540

Driver

Driver d = new foo.bar.MyDriver();

Connection c = d.connect(...); Not recommended, use

DriverManager instead Useful if you know you want a

particular driver

Yash Raithatha ,6540

Connection

A Connection represents a session with a specific database.

Within the context of a Connection, SQL statements are executed and results are returned.

Can have multiple connections to a database Also provides “metadata” -- information

about the database, tables, and fields Also methods to deal with transactions

Yash Raithatha ,6540

Statement

A Statement object is used for executing a static SQL statement and obtaining the results produced by it.

Yash Raithatha ,6540

ResultSet

A ResultSet provides access to a table of data generated by executing a Statement.

Only one ResultSet per Statement can be open at once.

A ResultSet maintains a cursor pointing to its current row of data.

The 'next' method moves the cursor to the next row.

Yash Raithatha ,6540

Example

String DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";

String url = "jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb);DBQ=c:/My Project/mydata.mdb";

Connection conn;

Yash Raithatha ,6540

try{ Class.forName(DBDriver); conn =

DriverManager.getConnection(url);} catch(Exception e)

{ JOptionPane.showMessageDialog(null, "Error in establishing Connection");

} // catch ends

Yash Raithatha ,6540

try{ String sql = “ Your SQL Query “; PreparedStatement ps =

conn.prepareStatement(sql); ResultSet rs = ps.executeQuery( );Or int rowsAffeted=ps.executeUpdate(

); }Catch(Exception e){}

Yash Raithatha ,6540

Yash Raithatha ,6540

Recommended