Download - Chapter1:JDBC
![Page 1: Chapter1:JDBC](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/1.jpg)
Unit – IPart-II
JDBC(Java Database Connectivity)
![Page 2: Chapter1:JDBC](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/2.jpg)
Contents
• Introduction to JDBC• JDBC Architecture• JDBC Drivers• Steps to Create JDBC Application• Operations– Insert– Update– Delete– Select
![Page 3: Chapter1:JDBC](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/5.jpg)
JDBC Drivers
JDBC
Type I“Bridge”
Type II“Native”
Type III“Middleware”
Type IV“Pure”
ODBC ODBCDriver
CLI (.lib)
MiddlewareServer
![Page 6: Chapter1:JDBC](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/10.jpg)
Steps for Creating JDBC Application
1.Establish a Connection2.Create JDBC Statements3.Execute SQL Statements4.GET ResultSet 5.Close connections
![Page 11: Chapter1:JDBC](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/11.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/13.jpg)
3)Executing SQL Statements
String insertStud = "Insert into Student values (1,’Vikrant’,’Pune’)";
st.executeUpdate(insertStud );
![Page 14: Chapter1:JDBC](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/14.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/15.jpg)
5) Close connection
st.close();con.close();
![Page 16: Chapter1:JDBC](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/16.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082904/589c4ec81a28ab227d8b64c9/html5/thumbnails/17.jpg)
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);}
}}