rajesh jdbc

22
Java Database Connectivity

Upload: aditya-sharma

Post on 06-May-2015

284 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Rajesh   jdbc

Java Database Connectivity

Page 2: Rajesh   jdbc

Java Database Connectivity (JDBC)

JDBC – provides an interface to Relational Data Sources.

JDBC library provides the means for executing SQL statements to access and operate on a relational database.

JDBC library is implemented in the java.sql package

Set of classes and interfaces that provide a uniform API for access to broad range of databases.

Page 3: Rajesh   jdbc

Java Database Connectivity (JDBC) JDBC:

establishes a connection with a database

sends SQL statements . processes the results.

Page 4: Rajesh   jdbc

Talking to Databases

Java Application

JDBC

AccessDatabase

OracleDatabase

SybaseDatabase

Page 5: Rajesh   jdbc

JDBC Concepts JDBC’s design is very similar to the design of

ODBC.

Driver Manager Loads database drivers, and manages the

connection between the application and the driver.

Driver Translates API calls into operations for a specific

data source

Connection A session between an application and a

database.

Page 6: Rajesh   jdbc

JDBC Concepts (contd.)

Statement An SQL Statement to perform a

query or update operation.

Metadata Information about returned data,

the database and the driver.

ResultSet Logical set of columns and rows

returned by executing an SQL statement (resulting tuples).

Page 7: Rajesh   jdbc

JDBC Component Interaction

DriverManager

Connection Statement ResultSet

Driver

Database

Creates Creates Creates

SQL

Result(tuples)

EstablishLink to DB

Page 8: Rajesh   jdbc

Two-Tier Database Access Model

Java Application talks directly to the database.

Accomplished through the JDBC driver which sends commands directly to the database.

Results sent back directly to the application.

A JDBC driver sits on the client machine.

e.g. HTTP, email

Application Space

Java Application

JDBC Driver

Database

SQLCommand

ResultSet

Page 9: Rajesh   jdbc

Three-Tier Database Access Model

JDBC driver sends commands to a middle tier, which in turn sends commands to database.

middle tier can provide • a higher-level API, not just SQL • control over database access • performance advantages ex. load balancing and caching frequently accessed data

The client machine communicates with the middle tier using such protocols as Hypertext Transfer Protocol (HTTP) or Remote Method Invocation (RMI).

Whereas the middle tier and database communication are governed by a DBMS-proprietary protocol.

Application Space

Java Application

JDBC Driver

Database

SQLCommand

ResultSet

Application Server(middle-tier)

ProprietaryProtocol

Page 10: Rajesh   jdbc

Three-Tier Database Access Model

Application Space

Java Application

JDBC Driver

Database

SQLCommand

ResultSet

Application Server(middle-tier)

ProprietaryProtocol

Pros:flexible: can change

one part without affecting otherscan connect to

different databases without

changing codespecialization:

presentation / business logic /

data managementcan cache queriescan implement proxies

and firewalls

Cons:higher complexityhigher maintenancelower network

efficiencymore parts to configure

(and buy)

Page 11: Rajesh   jdbc

JDBC Drivers

JDBC API, a purely java-based API.

JDBC Driver Manager which communicates with vendor specific drivers that perform real communication with the database.

Java ApplicationJDBC Driver

ManagerJDBC/

ODBC Bridg

e

Vendor Supplied

JDBC Driver

ODBC Drive

r

Page 12: Rajesh   jdbc

Type 1: JDBC-ODBC Bridge, Plus ODBC Driver

JDBC-ODBC bridge plus ODBC driver basically converts JDBC calls to Microsoft’s open database connectivity(ODBC).

As with JDBC , ODBC programming interface that you can use to access most type of relational databases on all type of platform.

Application Space

Java Application

JDBC – ODBC Bridge

Database

SQLCommand

ResultSet

ODBC Driver

ProprietaryProtocol

Page 13: Rajesh   jdbc

Type 1: JDBC-ODBC Bridge, Plus ODBC Driver

Advantages:• Can be useful for databases

where other methods not available (e.g., MSAccess).

Disadvantages:• Client computer needs to be

configured with ODBC driver and ODBC instance which specifies database server and other information.

• JDBC performance relies on ODBC driver.

Page 14: Rajesh   jdbc

JDBC DataTypes

SQL Type Java TypeCHAR StringVARCHAR StringLONGVARCHAR StringNUMERIC

java.Math.BigDecimalDECIMAL

java.Math.BigDecimalBIT booleanTINYINT intSMALLINT intINTEGER intBIGINT longREAL floatFLOAT doubleDOUBLE doubleBINARY byte[]VARBINARY byte[]DATE

java.sql.DateTIME

java.sql.TimeTIMESTAMP

java.sql.Timestamp

Page 15: Rajesh   jdbc

Steps during execution

1. Importing Packages .2. Registering the JDBC Drivers .3. Opening a Connection to a Database

.4. Creating a Statement Object. 5. Executing a Query and Returning a

Result Set Object .6. Processing the Result Set .7. Closing the Result Set and

Statement Objects .8. Closing the Connection.

Page 16: Rajesh   jdbc

1. Importing Packages

// Program name: jdbc_smvdu.java

//Import packages

import java.sql.*; // JDBC packages import java.math.*; import java.io.*;import oracle.jdbc.driver.*;

Page 17: Rajesh   jdbc

2. Load JDBC Drivers

class jdbc_smvdu {

public static void main (String args []) throws SQLException {

// Load Odbc driver

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

Page 18: Rajesh   jdbc

3. Opening Connection to a Database

//Prompt user for username and password String user; String password;

user = readEntry

("username: "); password = readEntry

("password: ");

// Connect to the local database

Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@aardvark:1526:teach", user, password);

Page 19: Rajesh   jdbc

4. Creating a Statement

Statement stmnt= conn.createStatement();

String st= “Select first name, age from Employee where id= 20 ”;

Page 20: Rajesh   jdbc

5. Executing a Query, Returning a ResultSet Object.6. Processing the ResultSet.

ResultSet rset = stmnt.executeQuery (s);

// Print query results

while (rset.next ()) System.out.println (rset.getString (1)+" "+ rset.getString(2));

Page 21: Rajesh   jdbc

7. Close the ResultSet and Statement Objects.8. Closing the Connection.

// close the result set, statement, and the connection

rset.close( ); stmnt.close( ); conn.close( );

}

Page 22: Rajesh   jdbc