JDBC ( Java Database Connectivity)

28
Prepared by : Raithatha Yash Roll No : 6540

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

Transcript of JDBC ( Java Database Connectivity)

Page 1: JDBC ( Java Database Connectivity)

Prepared by :Raithatha YashRoll No : 6540

Page 2: 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

It is included in J2SE & J2EE both

Yash Raithatha ,6540

Page 3: JDBC ( Java Database Connectivity)

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

Page 4: JDBC ( Java Database Connectivity)

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

Page 5: JDBC ( Java Database Connectivity)

Yash Raithatha ,6540

Page 6: JDBC ( Java Database Connectivity)

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

Page 7: JDBC ( Java Database Connectivity)

JDBC Drivers

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

Yash Raithatha ,6540

Page 8: JDBC ( Java Database Connectivity)

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

Page 9: JDBC ( Java Database Connectivity)

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

Page 10: JDBC ( Java Database Connectivity)

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

Page 11: JDBC ( Java Database Connectivity)

Advantages & disadvantages

AdvantagesBetter Performance than type 1

driverDisadvantages

No Platform independency Vendor client library needs to be

installed on the client

Yash Raithatha ,6540

Page 12: JDBC ( Java Database Connectivity)

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

Page 13: JDBC ( Java Database Connectivity)

MiddleWare

Yash Raithatha ,6540

JDBC

Middleware

Server

DS1

DS 2

DS 3

Page 14: JDBC ( Java Database Connectivity)

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

Page 15: JDBC ( Java Database Connectivity)

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

Page 16: JDBC ( Java Database Connectivity)

JDBC Drivers (Fig.)Yash Raithatha ,6540

JDBC

Type I“Bridge”

Type II“Native”

Type III“Middleware”

Type IV“Pure”

ODBCODBCDriver

CLI (.lib)

MiddlewareServer

Page 17: JDBC ( Java Database Connectivity)

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

Page 18: JDBC ( Java Database Connectivity)

java.sql

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

Yash Raithatha ,6540

Page 19: JDBC ( Java Database Connectivity)

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

Page 20: JDBC ( Java Database Connectivity)

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

Page 21: JDBC ( Java Database Connectivity)

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

Page 22: JDBC ( Java Database Connectivity)

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

Page 23: JDBC ( Java Database Connectivity)

Statement

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

Yash Raithatha ,6540

Page 24: JDBC ( Java Database Connectivity)

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

Page 25: JDBC ( Java Database Connectivity)

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

Page 26: JDBC ( Java Database Connectivity)

try{ Class.forName(DBDriver); conn =

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

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

} // catch ends

Yash Raithatha ,6540

Page 27: JDBC ( Java Database Connectivity)

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

Page 28: JDBC ( Java Database Connectivity)

Yash Raithatha ,6540