SlideShare una empresa de Scribd logo
1 de 27
Java Database Connectivity

info@icareercraft.com
JDBC
JDBC is an API with set of classes and interfaces
present in package java.sql
Thick Client
JAVA
APP
Thin
Client
JAVA
APP

DRIVER
C LIENT
DRIVER

C LIENT

Supporting Software

DB
SERVER

DB
SERVER
JDBC

JDBC supports 4 types of Drivers
Thick Drivers:
JDBC-ODBC Bridge Driver.
Native-API Partly Java Driver
Thin Drivers:
Net-Protocol Fully Java Driver
Native-Protocol Fully Java Driver
JDBC-ODBC Bridge Driver (TYPE – I)
JDBC API access via one or more ODBC drivers.
Binary code be loaded on each client machine
JDBC-ODBC BR
IDGE

JAVA
APP

J
D
B
C

O
D
B
C

DB
SERVER

C LIENT
ODBC
The Open Database Connectivity (ODBC) interface by Microsoft
allows applications to access data in database management
systems (DBMS) using SQL as a standard for accessing the data.
DRIVER 1
CLIENT
A pplication

Request

connection

DRIVER 2

connection

DRIVER 3

connection

ODBC Driver
Manager

DB
DB
DB
SERVER
Native-API Partly Java Driver (TYPE – II)
Binary code should be loaded on each client machine
Converts JDBC calls into calls on the client API for all
databases

J
JAVA
App

D
B
C
C LIENT

OCI

DB Library

ORACLE

SQL
Server

SERVER
Net-Protocol Fully Java Driver (TYPE – III)
The specific protocol used depends on the vendor
Type 1

JA VA
App

Type 3
Dr iver

Protocol 1

Middle
ware
Appli
cation

DB1

Type 2

DB2

Type 4

CLIENT

APPLICATION
SERVER

SERVER

DB3
Native-Protocol Fully Java Driver(TYPE – IV)
Direct call from the client to the DBMS server and is a
practical solution for Intranet access.
protocols are proprietary to database vendors, primary
source for this style of driver.

JAVA
A pp

JDBC
Driver

Data Base
PROTOCOL

User Sockets & Streams
JDBC

The 4 steps to connect to database are:
Loading the Driver.
Getting the Connection to database.
Executing the SQL Query.
Closing the Connection.
Loading Driver
Syntax for loading the Class file
Class.forName(“Driver Class Name”);
Example for Jdbc-Odbc bridge driver:
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Class.forName("oracle.jdbc.driver.OracleDriver");
JDBC drivers by various Vendors
Jdbc-Odbc Bridge : sun.jdbc.odbc.JdbcOdbcDriver
Oracle : oracle.jdbc.driver.OracleDriver
DB2 : com.ibm.db2.jdbc.app.DB2Driver
Pointbase : com.pointbase.jdbc.JdbcUniversalDriver
Sybase : com.sybase.jdbc2.jdbc.SybDriver
SQL-Server : weblogic.jdbc.mssqlserver4.Driver
Data Source Name (DSN)

User DSN
Available for user who creates and
stores in registry
System DSN
Available for all users and stores in
registry
Various ways of getting database connection
Connection con =
DriverManager.getConnection(“jdbc:odbc:dsnname”);
DriverManager.getConnection(“jdbc:odbc:dsnname”,
“username”, “password”);
DriverManager.getConnection(“jdbc:oracle:oci”, “username”,
“password”);
DriverManager.getConnection(“jdbc:oracle:thin:@ipaddress:port
:serviceId”, “username”, “password”)
Statement
Statement
It is used to execute SQL statements
Prepared Statement
Used to prepare statements with place holders(?)
to set the values at run time
Callable Statement
Used to execute functions or procedures available
in data base
Statement
A Statement object is used for executing a static SQL
statement and obtaining the results produced by it.
Statement smt = con.createStatement();
R
esultSet rs = smt.executeQuery(“Select_Queries”);
int n = smt.executeUpdate(“DML_Queries”);
boolean b = smt.execute(“Any_Query”);
ResultSet
Is an Object which stores data of the select statement
result in records and fields form.
By default it is Forward Only and Read Only.
Result Set Navigation and updating is possible from new
API version 1.2 onwards.
ResultSet
Navigating from one record to another
boolean b = rs.next()
Extracting values from ResultSet is possible either by Field Name or
Field Index
Column Index

int n=rs.getInt(1);
or
int n = rs.get( “empID”);
String s=rs.getString(1);
or
String s=rs.getString( “empName”);

Column Name
ResultSetMetaData
It is Data about Data of ResultSet like field names, no.
of Columns etc.
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
String fname = rsmd.getColumnName(int index);
String dn = rsmd.getColumnTypeName(int index);
PreparedStatement
A PreparedStatement object is used when an application plans to
reuse a statement multiple times.
The application prepares the SQL Statement it plans to use. Once
prepared, the application can specify values for parameters (if any)
in the prepared SQL statement.
PreparedStatement ps = con.prepareStatement(“Query with Place
Holders”);
Examples:
PreparedStatement ps = con.prepareStatement(“select * from emp
where empno=?”);
Place Holder
PreparedStatement
PreparedStatement ps = con.prepareStatement(“insert into emp
(empno,ename) values(?,?)”);
ps.setInt(1,102);
ps.setString(2, “Scott”);

Executing the PreparedStatement
ResultSet rs=ps.executeQuery();
int n=ps.executeUpdate();
boolean b=ps.execute();

Set values for
place holders
Statement vs PreparedStatement
A Statement object
Doesn’t contain a SQL statement at the time of creation.
Compiled and executed every time

A PreparedStatement object
Is a precompiled Statement
Contains a SQL statement at the time of creation.
Support for Place holders, (Ex: For data type blob, clob,
binaryStream)
Function
create or replace function DemoFunction (id number)
return varchar2 is
temp varchar2(20);
begin
select name into temp from Student where
rollno=id;
return temp;
end DemoFunction;
Procedure
create or replace procedure DemoProcedure(sname
varchar2, rno number) is
begin
insert into Student values(sname,rno);
end DemoProcedure;
CallableStatement
A CallableStatement is used to call stored procedures that return
values. The CallableStatement has methods for retrieving the return
values of the stored procedure.
CallableStatement cs = con.prepareCall( “{call prod(?,?)}”);
= con.prepareCall( "{?=call fun(?)}");
Example:
CallableStatement cs = con.prepareCall( “{call emp.insert(?,?)}”);
CallableStatement
CallableStatement cs = con.prepareCall(“{?=call empSal(?)}”);
cs.registerOutParameter(1, java.sql.Types.INTEGER);
cs.setInt(2,102);
cs.executeUpdate();
int result = cs.getInt(1);
CallableStatement
java.sql.Types
java.sql.Types.DATE
java.sql.Types.DOUBLE
java.sql.Types.TIME
java.sql.Types.FLOAT
java.sql.Types.VARCHAR
java.sql.Types.INTEGER
java.sql.Types.TIMESTAMP
java.sql.Types.NUMERIC
java.sql.Types. CHAR
java.sql.Types.BOOLEAN
java.sql.Types.BLOB
java.sql.Types.CLOB
Example Program
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott", "tiger");
CallableStatement cs = con.prepareCall("{?=call empSal(?)}");
cs.setInt(2, 7369);
cs.registerOutParameter(1,Types.INTEGER);
cs.executeUpdate();
int x=cs.getInt(1);
System.out.println(" The Value is: "+ x);
con.close();

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
JDBC ppt
JDBC pptJDBC ppt
JDBC ppt
 
Jdbc example program with access and MySql
Jdbc example program with access and MySqlJdbc example program with access and MySql
Jdbc example program with access and MySql
 
Lecture 1. java database connectivity
Lecture 1. java database connectivityLecture 1. java database connectivity
Lecture 1. java database connectivity
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
Jdbc
JdbcJdbc
Jdbc
 
Java EE Introduction
Java EE IntroductionJava EE Introduction
Java EE Introduction
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
Jdbc api
Jdbc apiJdbc api
Jdbc api
 
Hibernate
HibernateHibernate
Hibernate
 
JDBC
JDBCJDBC
JDBC
 
DataBase Connectivity
DataBase ConnectivityDataBase Connectivity
DataBase Connectivity
 
Hibernate Developer Reference
Hibernate Developer ReferenceHibernate Developer Reference
Hibernate Developer Reference
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
 
JDBC in Servlets
JDBC in ServletsJDBC in Servlets
JDBC in Servlets
 
Change RelationalDB to GraphDB with OrientDB
Change RelationalDB to GraphDB with OrientDBChange RelationalDB to GraphDB with OrientDB
Change RelationalDB to GraphDB with OrientDB
 
Introduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsIntroduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applications
 
Prashanthi
PrashanthiPrashanthi
Prashanthi
 
Java Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedJava Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet Advanced
 

Destacado (18)

Jdbc
JdbcJdbc
Jdbc
 
Core jdbc basics
Core jdbc basicsCore jdbc basics
Core jdbc basics
 
Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
 
Java DataBase Connectivity -JDBC Part-1
Java DataBase Connectivity -JDBC Part-1Java DataBase Connectivity -JDBC Part-1
Java DataBase Connectivity -JDBC Part-1
 
Jdbc introduction
Jdbc introductionJdbc introduction
Jdbc introduction
 
Jdbc
JdbcJdbc
Jdbc
 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet Solution
 
JDBC
JDBCJDBC
JDBC
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Java Course 13: JDBC & Logging
Java Course 13: JDBC & LoggingJava Course 13: JDBC & Logging
Java Course 13: JDBC & Logging
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Advanced JAVA
Advanced JAVAAdvanced JAVA
Advanced JAVA
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Jdbc Ppt
Jdbc PptJdbc Ppt
Jdbc Ppt
 

Similar a Java Database Connectivity API (JDBC) Overview

Similar a Java Database Connectivity API (JDBC) Overview (20)

Jdbc
Jdbc   Jdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC.ppt
JDBC.pptJDBC.ppt
JDBC.ppt
 
Final Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptFinal Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.ppt
 
JDBC
JDBCJDBC
JDBC
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Java Data Base Connectivity concepts.pptx
Java Data Base Connectivity concepts.pptxJava Data Base Connectivity concepts.pptx
Java Data Base Connectivity concepts.pptx
 
Jdbc connectivity
Jdbc connectivityJdbc connectivity
Jdbc connectivity
 
Jdbc
JdbcJdbc
Jdbc
 
Unit 5-jdbc2
Unit 5-jdbc2Unit 5-jdbc2
Unit 5-jdbc2
 
jdbc
jdbcjdbc
jdbc
 
Advance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-databaseAdvance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-database
 
Jdbc 1
Jdbc 1Jdbc 1
Jdbc 1
 
Jdbc sasidhar
Jdbc  sasidharJdbc  sasidhar
Jdbc sasidhar
 
High Performance Jdbc
High Performance JdbcHigh Performance Jdbc
High Performance Jdbc
 
Lecture17
Lecture17Lecture17
Lecture17
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
 
Jdbc new
Jdbc newJdbc new
Jdbc new
 
java arlow jdbc tutorial(java programming tutorials)
java arlow jdbc tutorial(java programming tutorials)java arlow jdbc tutorial(java programming tutorials)
java arlow jdbc tutorial(java programming tutorials)
 
Jdbc
JdbcJdbc
Jdbc
 

Más de Rajesh Roky

File splitter and joiner
File splitter and joinerFile splitter and joiner
File splitter and joinerRajesh Roky
 
INTEGRATED SHOPPING ASSISTANCE WITH FREDGE AND MOBILE
INTEGRATED SHOPPING ASSISTANCE WITH FREDGE AND MOBILEINTEGRATED SHOPPING ASSISTANCE WITH FREDGE AND MOBILE
INTEGRATED SHOPPING ASSISTANCE WITH FREDGE AND MOBILERajesh Roky
 
The main purpose of the project is to manage the supermarket efficiently (rep...
The main purpose of the project is to manage the supermarket efficiently (rep...The main purpose of the project is to manage the supermarket efficiently (rep...
The main purpose of the project is to manage the supermarket efficiently (rep...Rajesh Roky
 
File Splitter Table of contents
File Splitter Table of contents File Splitter Table of contents
File Splitter Table of contents Rajesh Roky
 
FILE SPLITTER AND JOINER
FILE SPLITTER AND JOINERFILE SPLITTER AND JOINER
FILE SPLITTER AND JOINERRajesh Roky
 
Rainbow technology-ppt
Rainbow technology-pptRainbow technology-ppt
Rainbow technology-pptRajesh Roky
 

Más de Rajesh Roky (8)

Servlet
ServletServlet
Servlet
 
wrapper classes
wrapper classeswrapper classes
wrapper classes
 
File splitter and joiner
File splitter and joinerFile splitter and joiner
File splitter and joiner
 
INTEGRATED SHOPPING ASSISTANCE WITH FREDGE AND MOBILE
INTEGRATED SHOPPING ASSISTANCE WITH FREDGE AND MOBILEINTEGRATED SHOPPING ASSISTANCE WITH FREDGE AND MOBILE
INTEGRATED SHOPPING ASSISTANCE WITH FREDGE AND MOBILE
 
The main purpose of the project is to manage the supermarket efficiently (rep...
The main purpose of the project is to manage the supermarket efficiently (rep...The main purpose of the project is to manage the supermarket efficiently (rep...
The main purpose of the project is to manage the supermarket efficiently (rep...
 
File Splitter Table of contents
File Splitter Table of contents File Splitter Table of contents
File Splitter Table of contents
 
FILE SPLITTER AND JOINER
FILE SPLITTER AND JOINERFILE SPLITTER AND JOINER
FILE SPLITTER AND JOINER
 
Rainbow technology-ppt
Rainbow technology-pptRainbow technology-ppt
Rainbow technology-ppt
 

Último

USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 

Último (20)

USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 

Java Database Connectivity API (JDBC) Overview

  • 2. JDBC JDBC is an API with set of classes and interfaces present in package java.sql Thick Client JAVA APP Thin Client JAVA APP DRIVER C LIENT DRIVER C LIENT Supporting Software DB SERVER DB SERVER
  • 3. JDBC JDBC supports 4 types of Drivers Thick Drivers: JDBC-ODBC Bridge Driver. Native-API Partly Java Driver Thin Drivers: Net-Protocol Fully Java Driver Native-Protocol Fully Java Driver
  • 4. JDBC-ODBC Bridge Driver (TYPE – I) JDBC API access via one or more ODBC drivers. Binary code be loaded on each client machine JDBC-ODBC BR IDGE JAVA APP J D B C O D B C DB SERVER C LIENT
  • 5. ODBC The Open Database Connectivity (ODBC) interface by Microsoft allows applications to access data in database management systems (DBMS) using SQL as a standard for accessing the data. DRIVER 1 CLIENT A pplication Request connection DRIVER 2 connection DRIVER 3 connection ODBC Driver Manager DB DB DB SERVER
  • 6. Native-API Partly Java Driver (TYPE – II) Binary code should be loaded on each client machine Converts JDBC calls into calls on the client API for all databases J JAVA App D B C C LIENT OCI DB Library ORACLE SQL Server SERVER
  • 7. Net-Protocol Fully Java Driver (TYPE – III) The specific protocol used depends on the vendor Type 1 JA VA App Type 3 Dr iver Protocol 1 Middle ware Appli cation DB1 Type 2 DB2 Type 4 CLIENT APPLICATION SERVER SERVER DB3
  • 8. Native-Protocol Fully Java Driver(TYPE – IV) Direct call from the client to the DBMS server and is a practical solution for Intranet access. protocols are proprietary to database vendors, primary source for this style of driver. JAVA A pp JDBC Driver Data Base PROTOCOL User Sockets & Streams
  • 9. JDBC The 4 steps to connect to database are: Loading the Driver. Getting the Connection to database. Executing the SQL Query. Closing the Connection.
  • 10. Loading Driver Syntax for loading the Class file Class.forName(“Driver Class Name”); Example for Jdbc-Odbc bridge driver: Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); Class.forName("oracle.jdbc.driver.OracleDriver");
  • 11. JDBC drivers by various Vendors Jdbc-Odbc Bridge : sun.jdbc.odbc.JdbcOdbcDriver Oracle : oracle.jdbc.driver.OracleDriver DB2 : com.ibm.db2.jdbc.app.DB2Driver Pointbase : com.pointbase.jdbc.JdbcUniversalDriver Sybase : com.sybase.jdbc2.jdbc.SybDriver SQL-Server : weblogic.jdbc.mssqlserver4.Driver
  • 12. Data Source Name (DSN) User DSN Available for user who creates and stores in registry System DSN Available for all users and stores in registry
  • 13. Various ways of getting database connection Connection con = DriverManager.getConnection(“jdbc:odbc:dsnname”); DriverManager.getConnection(“jdbc:odbc:dsnname”, “username”, “password”); DriverManager.getConnection(“jdbc:oracle:oci”, “username”, “password”); DriverManager.getConnection(“jdbc:oracle:thin:@ipaddress:port :serviceId”, “username”, “password”)
  • 14. Statement Statement It is used to execute SQL statements Prepared Statement Used to prepare statements with place holders(?) to set the values at run time Callable Statement Used to execute functions or procedures available in data base
  • 15. Statement A Statement object is used for executing a static SQL statement and obtaining the results produced by it. Statement smt = con.createStatement(); R esultSet rs = smt.executeQuery(“Select_Queries”); int n = smt.executeUpdate(“DML_Queries”); boolean b = smt.execute(“Any_Query”);
  • 16. ResultSet Is an Object which stores data of the select statement result in records and fields form. By default it is Forward Only and Read Only. Result Set Navigation and updating is possible from new API version 1.2 onwards.
  • 17. ResultSet Navigating from one record to another boolean b = rs.next() Extracting values from ResultSet is possible either by Field Name or Field Index Column Index int n=rs.getInt(1); or int n = rs.get( “empID”); String s=rs.getString(1); or String s=rs.getString( “empName”); Column Name
  • 18. ResultSetMetaData It is Data about Data of ResultSet like field names, no. of Columns etc. ResultSetMetaData rsmd = rs.getMetaData(); int count = rsmd.getColumnCount(); String fname = rsmd.getColumnName(int index); String dn = rsmd.getColumnTypeName(int index);
  • 19. PreparedStatement A PreparedStatement object is used when an application plans to reuse a statement multiple times. The application prepares the SQL Statement it plans to use. Once prepared, the application can specify values for parameters (if any) in the prepared SQL statement. PreparedStatement ps = con.prepareStatement(“Query with Place Holders”); Examples: PreparedStatement ps = con.prepareStatement(“select * from emp where empno=?”); Place Holder
  • 20. PreparedStatement PreparedStatement ps = con.prepareStatement(“insert into emp (empno,ename) values(?,?)”); ps.setInt(1,102); ps.setString(2, “Scott”); Executing the PreparedStatement ResultSet rs=ps.executeQuery(); int n=ps.executeUpdate(); boolean b=ps.execute(); Set values for place holders
  • 21. Statement vs PreparedStatement A Statement object Doesn’t contain a SQL statement at the time of creation. Compiled and executed every time A PreparedStatement object Is a precompiled Statement Contains a SQL statement at the time of creation. Support for Place holders, (Ex: For data type blob, clob, binaryStream)
  • 22. Function create or replace function DemoFunction (id number) return varchar2 is temp varchar2(20); begin select name into temp from Student where rollno=id; return temp; end DemoFunction;
  • 23. Procedure create or replace procedure DemoProcedure(sname varchar2, rno number) is begin insert into Student values(sname,rno); end DemoProcedure;
  • 24. CallableStatement A CallableStatement is used to call stored procedures that return values. The CallableStatement has methods for retrieving the return values of the stored procedure. CallableStatement cs = con.prepareCall( “{call prod(?,?)}”); = con.prepareCall( "{?=call fun(?)}"); Example: CallableStatement cs = con.prepareCall( “{call emp.insert(?,?)}”);
  • 25. CallableStatement CallableStatement cs = con.prepareCall(“{?=call empSal(?)}”); cs.registerOutParameter(1, java.sql.Types.INTEGER); cs.setInt(2,102); cs.executeUpdate(); int result = cs.getInt(1);
  • 27. Example Program Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger"); CallableStatement cs = con.prepareCall("{?=call empSal(?)}"); cs.setInt(2, 7369); cs.registerOutParameter(1,Types.INTEGER); cs.executeUpdate(); int x=cs.getInt(1); System.out.println(" The Value is: "+ x); con.close();