Chapter1:JDBC

Preview:

Citation preview

Unit – IPart-II

JDBC(Java Database Connectivity)

Contents

• Introduction to JDBC• JDBC Architecture• JDBC Drivers• Steps to Create JDBC Application• Operations– Insert– Update– Delete– Select

JDBC ( Java Database Connectivity) • JDBC is Java application programming

interface (API) that allows the Java programmers to access database management system from Java code

• defines interfaces and classes for writing database applications in Java by making database connections.

• executing SQL statements and supports basic SQL functionality.

• It provides RDBMS access by allowing you to embed SQL inside Java code.

JDBC Architecture

Application JDBC Driver

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

database• Ideal: can change database engines without changing

any application code

JDBC Drivers

JDBC

Type I“Bridge”

Type II“Native”

Type III“Middleware”

Type IV“Pure”

ODBC ODBCDriver

CLI (.lib)

MiddlewareServer

Type I Drivers

Copyright © 1997 Alex Chaffee

• Use bridging technology• Requires installation/configuration on

client machines• Not good for Web• e.g. ODBC Bridge

Type II Drivers

• Native API drivers• Requires installation/configuration on

client machines• Used to leverage existing libraries• Usually not thread-safe• Mostly out-dated now• e.g. Intersolv Oracle Driver, WebLogic

drivers

Type III Drivers

• Calls middleware server, usually on database host

• Very flexible -- allows access to multiple databases using one driver

• Only need to download one driver• But it’s another server application to install

and maintain• e.g. Symantec DBAnywhere

Copyright © 1997 Alex Chaffee

Type IV Drivers

• 100% Pure Java• 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

Steps for Creating JDBC Application

1.Establish a Connection2.Create JDBC Statements3.Execute SQL Statements4.GET ResultSet 5.Close connections

11

1. Establish a connection

• import java.sql.*;• Load the vendor specific driver

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");// Dynamically loads a driver class for databases

• Make the connection Connection con = DriverManager.getConnection(“jdbc:odbc:DSNName”,

"","");// Establishes connection to database by obtaining

a Connection object

2. Create JDBC statement(s)Three kinds of Statements:

Statement: Execute simple sql queries without parameters.Statement st= con.createStatement() ; Creates an SQL Statement object.

Prepared Statement: Execute precompiled SQL queries with or without parameters.PreparedStatement objects are precompiled SQL statements.

Ex:PreparedStatement ps=con.prepareStatement("insert into Student

values(?,?,?,?)")

Callable Statement: Execute a call to a database stored procedure.CallableStatement cs=con.prepareCall(String sql)returns a new CallableStatement object. CallableStatement objects are SQL stored procedure call statements.

3)Executing SQL Statements

String insertStud = "Insert into Student values (1,’Vikrant’,’Pune’)";

st.executeUpdate(insertStud );

4) Get ResultSet

String query = "select * from Student";

ResultSet rs = st.executeQuery(query);

while ( rs.next() ){

System.out.println(rs.getInt(1) + " " + rs.getString(2)+ “ ” + rs.getString(3));}

5) Close connection

st.close();con.close();

Sample JDBC Program// for insertion of recordsimport java.sql.*;public class Employee {

static Connection con=null;static PreparedStatement pst=null;static String username="postgres";static String pwd="root";static String connURL="jdbc:postgresql://localhost:5433/postgres";public static void main(String[] args) {try{//loading the driversClass.forName("org.postgresql.Driver");

//Establish the connectioncon=DriverManager.getConnection(connURL,username,pwd);pst=con.prepareStatement("insert into employee values(?,?)");

Cont…

Sample JDBC Program

pst.setInt(1, 1009);pst.setString(2, "G.D Agrawal");//Executing the SQL statementint x=pst.executeUpdate();System.out.println( x +" Record inserted..");//Close the Connectionscon.close();

}catch(Exception ex) {

System.out.println(ex);}

}}

Recommended