rajesh jdbc

Post on 06-May-2015

275 Views

Category:

Technology

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Java Database Connectivity

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.

Java Database Connectivity (JDBC) JDBC:

establishes a connection with a database

sends SQL statements . processes the results.

Talking to Databases

Java Application

JDBC

AccessDatabase

OracleDatabase

SybaseDatabase

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.

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).

JDBC Component Interaction

DriverManager

Connection Statement ResultSet

Driver

Database

Creates Creates Creates

SQL

Result(tuples)

EstablishLink to DB

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

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

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)

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

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

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.

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

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.

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.*;

2. Load JDBC Drivers

class jdbc_smvdu {

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

// Load Odbc driver

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

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);

4. Creating a Statement

Statement stmnt= conn.createStatement();

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

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));

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( );

}

top related