17
Unit – I Part-II JDBC (Java Database Connectivity)

Chapter1:JDBC

Embed Size (px)

Citation preview

Page 1: Chapter1:JDBC

Unit – IPart-II

JDBC(Java Database Connectivity)

Page 2: Chapter1:JDBC

Contents

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

Page 3: Chapter1:JDBC

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.

Page 4: Chapter1:JDBC

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

Page 5: Chapter1:JDBC

JDBC Drivers

JDBC

Type I“Bridge”

Type II“Native”

Type III“Middleware”

Type IV“Pure”

ODBC ODBCDriver

CLI (.lib)

MiddlewareServer

Page 6: Chapter1:JDBC

Type I Drivers

Copyright © 1997 Alex Chaffee

• Use bridging technology• Requires installation/configuration on

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

Page 7: Chapter1:JDBC

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

Page 8: Chapter1:JDBC

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

Page 9: Chapter1:JDBC

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

Page 10: Chapter1:JDBC

Steps for Creating JDBC Application

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

Page 11: Chapter1:JDBC

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

Page 12: Chapter1:JDBC

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.

Page 13: Chapter1:JDBC

3)Executing SQL Statements

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

st.executeUpdate(insertStud );

Page 14: Chapter1:JDBC

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

Page 15: Chapter1:JDBC

5) Close connection

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

Page 16: Chapter1:JDBC

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…

Page 17: Chapter1:JDBC

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

}}