SlideShare a Scribd company logo
JDBC
Java Database Connectivity
Rohit Jain
DTU
B.Tech, Software Engineering
What is JDBC?
 JDBC API allows Java programs to
connect to DBs
 Provides cross-vendor connectivity and
data access across relational databases
from different vendors
 Classes and interfaces allow users to
access the database in a standard way
 The JVM uses the JDBC driver to
translate generalized JDBC calls into
vendor specific database calls
What Does JDBC Do ?
JDBC makes it possible to do three things:
Establish a connection with a database
Send SQL statements
Process the results.
JDBC Classes for DB
Connection
 java.sql.Driver
◦ Unless creating custom JDBC implementation, never
have to deal with it. It gives JDBC a launching point
for DB connectivity by responding to
DriverManager connection requests
 java.sql.DriverManager
◦ Maintains a list of Driver implementations and
presents an application with one that matches a
requested URL.
◦ getConnection(url, uid, password)
◦ getDrivers(), registerDriver()
 java.sql.Connection
◦ Represents a single logical DB connection; used for
sending SQL statements
JDBC Drivers
 JDBC-ODBC bridge
 Part Java, Part Native Driver
 Intermediate DAccess Server
 Pure Java Drivers
Driver Manager
 The purpose of the
java.sql.DriverManger class in JDBC is
to provide a common access layer on top
of different database drivers used in an
application
 DriverManager requires that each driver
required by the application must be
registered before use
 Load the database driver using
ClassLoader
 Class.forName(“oracle.jdbc.driver.Oracle
Driver”);
Registering a JDBC Driver
 Driver must be registered before it is
used.
 Drivers can be registered in three
ways.
Most Common Approach is
 To use Java’s Class.forName()
 Dynamically loads the driver’s class
file into memory
 Preferable because
It allows driver registration
configurable and portable.
 The second approach you can use to
register a driver is to use the static
DriverManager.registerDriver( )
method.
 Use the registerDriver( ) method if
you
are using a non-JDK compliant JVM.
For example:
DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver( ));
 The third approach is to use a
combination of Class.forName( ) to
dynamically load the Oracle driver and
then the driver classes' getInstance( )
method to work around noncompliant
JVMs.
 For example:
Class.forName("oracle.jdbc.driver.Oracle
Driver").newInstance();
Basic steps to use
a database in Java
 Establish a connection
 Create JDBC Statements
 Execute SQL Statements
 GET ResultSet
 Close connections
1. Establish a connection
 import java.sql.*;
 Load the vendor specific driver
◦ Class.forName("oracle.jdbc.driver.OracleDriver");
 What do you think this statement does, and how?
 Dynamically loads a driver class, for Oracle database
 Make the connection
◦ Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@oracle-prod:1521:OPROD", username,
passwd);
 What do you think this statement does?
 Establishes connection to database by obtaining
a Connection object
2. Create JDBC statement(s)
 Statement stmt = con.createStatement()
;
 Creates a Statement object for
sending SQL statements to the
database
3.Executing SQL Statements
 String createStudent = "Create table
Student " +
"(SSN Integer not null, Name
VARCHAR(32), " + "Marks Integer)";
stmt.executeUpdate(createStudent);
//What does this statement do?
 String insertStudent = "Insert into
Student values“ +
"(123456789,abc,100)";
stmt.executeUpdate(insertStudent);
Execute Statements
 This uses executeUpdate because the
SQL statement contained in
createTableCoffees is a data definition
language ( DDL ) statement
 DDL statements are executed with
executeUpdate
– Create a table
– Alter a table
– Drop a table
 executeUpdate is also used to execute
SQL statements that update a table
Execute Statements
 executeUpdate is used far more often
to update tables than to create them–
We create a table once but update it
many times
 The method used most often for
executing SQL statements is
executeQuery
 executeQuery is used to execute
SELECT statements – SELECT
statements are the most common SQL
statements
Entering Data to a Table
 Statement stmt = con.createStatement();
 stmt.executeUpdate ( "INSERT INTO COFFEES
" +
"VALUES ('Colombian', 101, 7.99, 0, 0)");
 stmt.executeUpdate ("INSERT INTO COFFEES "
+
"VALUES ('French_Roast', 49, 8.99, 0, 0)");
 stmt.executeUpdate ("INSERT INTO COFFEES "
+
"VALUES ('Espresso', 150, 9.99, 0, 0)");
 stmt.executeUpdate ("INSERT INTO COFFEES "
+
"VALUES ('Colombian Decaf' 101 8 99 0 0)");
 stmt.executeUpdate ("INSERT INTO COFFEES "
+
Batch Update
 What is batch update?
- Send multiple update statement in a
single request to the database
 Why batch update?
-Better performance
 How do we perform batch update?
-Statement.addBatch (sqlString);
- Statement.executeBatch();
4.Get ResultSet
String queryStudent = "select * from Student";
ResultSet rs =
Stmt.executeQuery(queryStudent);
//What does this statement do?
while (rs.next()) {
int ssn = rs.getInt("SSN");
String name = rs.getString("NAME");
int marks = rs.getInt("MARKS");
}
5. Close connection
 stmt.close();
 con.close();
Sample Program
import java.sql.*;
class TestThinApp {
public static void main (String args[])
throws ClassNotFoundException,
SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver
");
// or you can use:
DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver());
Connection conn =
DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","scott","ti
Statement stmt = conn.createStatement( );
ResultSet rset = stmt.executeQuery(
"select 'Hello Thin driver tester '||USER||'!'
result from dual");
while(rset.next( ))
System.out.println(rset.getString(1));
rset.close( );
stmt.close( );
conn.close( );
}
}
SUMMARY
 JDBC makes it very easy to connect
to DBMS and to manipulate the data
in it.
 You have to install the oracle driver in
order to make the connection.
 It is crucial to setup PATH and
CLASSPATH properly
Thanks…..
Ad

More Related Content

What's hot (20)

JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
Manisha Keim
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
Dhyey Dattani
 
Jdbc Ppt
Jdbc PptJdbc Ppt
Jdbc Ppt
Centre for Budget and Governance Accountability (CBGA)
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
Vikas Jagtap
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
Jafar Nesargi
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
sandeep54552
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
kamal kotecha
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
Purbarun Chakrabarti
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Serhat Can
 
Jdbc
JdbcJdbc
Jdbc
Yamuna Devi
 
Servlets
ServletsServlets
Servlets
ZainabNoorGul
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
Aneega
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
Dzmitry Naskou
 
Json
JsonJson
Json
krishnapriya Tadepalli
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
Binoj T E
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
Vikas Jagtap
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Kasun Madusanke
 
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSINGINTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
Aaqib Hussain
 

Viewers also liked (20)

Jdbc
JdbcJdbc
Jdbc
Jussi Pohjolainen
 
Jdbc
JdbcJdbc
Jdbc
Nitesh Kumar Pandey
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
Information Technology
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database Connectivity
Ranjan Kumar
 
Jdbc
JdbcJdbc
Jdbc
Mumbai Academisc
 
Jdbc
JdbcJdbc
Jdbc
Ravi_Kant_Sahu
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
Pooja Talreja
 
Power final entre iguals 18 desembre
Power final entre iguals 18 desembre Power final entre iguals 18 desembre
Power final entre iguals 18 desembre
Associació SalutiFamília
 
Relco Brochure Mail
Relco Brochure MailRelco Brochure Mail
Relco Brochure Mail
Abhinavv Dayal
 
Rpp ix 2
Rpp ix 2Rpp ix 2
Rpp ix 2
sifatulfalah3120
 
How much security is enough?
How much security is enough?How much security is enough?
How much security is enough?
Sherry Jones
 
SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"
SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"
SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"
SOLOMOTO_RU
 
Veiktais darbs nedēļas laikā
Veiktais darbs nedēļas laikāVeiktais darbs nedēļas laikā
Veiktais darbs nedēļas laikā
rlycky
 
Reference Corey Jay
Reference Corey JayReference Corey Jay
Reference Corey Jay
Corey Jay
 
Promes dan-pemetaan-smtr-1
Promes dan-pemetaan-smtr-1Promes dan-pemetaan-smtr-1
Promes dan-pemetaan-smtr-1
sifatulfalah3120
 
JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)
Craig Dickson
 
Creating Teaching Videos in the Studio
Creating Teaching Videos in the StudioCreating Teaching Videos in the Studio
Creating Teaching Videos in the Studio
Kristen Sosulski
 
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
kamal kotecha
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
Dharani Kumar Madduri
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database Connectivity
Ranjan Kumar
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
Pooja Talreja
 
How much security is enough?
How much security is enough?How much security is enough?
How much security is enough?
Sherry Jones
 
SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"
SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"
SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"
SOLOMOTO_RU
 
Veiktais darbs nedēļas laikā
Veiktais darbs nedēļas laikāVeiktais darbs nedēļas laikā
Veiktais darbs nedēļas laikā
rlycky
 
Reference Corey Jay
Reference Corey JayReference Corey Jay
Reference Corey Jay
Corey Jay
 
Promes dan-pemetaan-smtr-1
Promes dan-pemetaan-smtr-1Promes dan-pemetaan-smtr-1
Promes dan-pemetaan-smtr-1
sifatulfalah3120
 
JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)
Craig Dickson
 
Creating Teaching Videos in the Studio
Creating Teaching Videos in the StudioCreating Teaching Videos in the Studio
Creating Teaching Videos in the Studio
Kristen Sosulski
 
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
kamal kotecha
 
Ad

Similar to JDBC ppt (20)

Jdbc
JdbcJdbc
Jdbc
lathasiva
 
Lecture 1. java database connectivity
Lecture 1. java database connectivityLecture 1. java database connectivity
Lecture 1. java database connectivity
Waheedullah Suliman Khail
 
Select query in JDBC
Select query in JDBCSelect query in JDBC
Select query in JDBC
Maulik Togadiya
 
Java JDBC
Java JDBCJava JDBC
Java JDBC
Jussi Pohjolainen
 
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)
Daroko blog(www.professionalbloggertricks.com)
 
10 J D B C
10  J D B C10  J D B C
10 J D B C
guest04b824
 
Jdbc
JdbcJdbc
Jdbc
roopa manoharan
 
Data Access with JDBC
Data Access with JDBCData Access with JDBC
Data Access with JDBC
BG Java EE Course
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
backdoor
 
Jdbc
Jdbc   Jdbc
Jdbc
Ishucs
 
Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
Fulvio Corno
 
JDBC programming
JDBC programmingJDBC programming
JDBC programming
Fulvio Corno
 
Jdbc
JdbcJdbc
Jdbc
Ishucs
 
Jdbc connectivity
Jdbc connectivityJdbc connectivity
Jdbc connectivity
arikazukito
 
JDBC.ppt
JDBC.pptJDBC.ppt
JDBC.ppt
ChagantiSahith
 
Chapter 5 JDBC.pdf for stufent of computer andtudent It s
Chapter 5 JDBC.pdf for stufent of computer andtudent  It sChapter 5 JDBC.pdf for stufent of computer andtudent  It s
Chapter 5 JDBC.pdf for stufent of computer andtudent It s
anuwaradisu19
 
JDBC with MySQL.pdf
JDBC with MySQL.pdfJDBC with MySQL.pdf
JDBC with MySQL.pdf
Arumugam90
 
JDBC with MySQL.pdf
JDBC with MySQL.pdfJDBC with MySQL.pdf
JDBC with MySQL.pdf
Arumugam90
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
Dhyey Dattani
 
Jdbc
JdbcJdbc
Jdbc
mishaRani1
 
Ad

Recently uploaded (20)

UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à GenèveUiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPathCommunity
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Top-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptxTop-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptx
BR Softech
 
Artificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptxArtificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptx
03ANMOLCHAURASIYA
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)
Kaya Weers
 
Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Šimek
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
CSUC - Consorci de Serveis Universitaris de Catalunya
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à GenèveUiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPathCommunity
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Top-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptxTop-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptx
BR Softech
 
Artificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptxArtificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptx
03ANMOLCHAURASIYA
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)
Kaya Weers
 
Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Šimek
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 

JDBC ppt

  • 1. JDBC Java Database Connectivity Rohit Jain DTU B.Tech, Software Engineering
  • 2. What is JDBC?  JDBC API allows Java programs to connect to DBs  Provides cross-vendor connectivity and data access across relational databases from different vendors  Classes and interfaces allow users to access the database in a standard way  The JVM uses the JDBC driver to translate generalized JDBC calls into vendor specific database calls
  • 3. What Does JDBC Do ? JDBC makes it possible to do three things: Establish a connection with a database Send SQL statements Process the results.
  • 4. JDBC Classes for DB Connection  java.sql.Driver ◦ Unless creating custom JDBC implementation, never have to deal with it. It gives JDBC a launching point for DB connectivity by responding to DriverManager connection requests  java.sql.DriverManager ◦ Maintains a list of Driver implementations and presents an application with one that matches a requested URL. ◦ getConnection(url, uid, password) ◦ getDrivers(), registerDriver()  java.sql.Connection ◦ Represents a single logical DB connection; used for sending SQL statements
  • 5. JDBC Drivers  JDBC-ODBC bridge  Part Java, Part Native Driver  Intermediate DAccess Server  Pure Java Drivers
  • 6. Driver Manager  The purpose of the java.sql.DriverManger class in JDBC is to provide a common access layer on top of different database drivers used in an application  DriverManager requires that each driver required by the application must be registered before use  Load the database driver using ClassLoader  Class.forName(“oracle.jdbc.driver.Oracle Driver”);
  • 7. Registering a JDBC Driver  Driver must be registered before it is used.  Drivers can be registered in three ways.
  • 8. Most Common Approach is  To use Java’s Class.forName()  Dynamically loads the driver’s class file into memory  Preferable because It allows driver registration configurable and portable.
  • 9.  The second approach you can use to register a driver is to use the static DriverManager.registerDriver( ) method.  Use the registerDriver( ) method if you are using a non-JDK compliant JVM. For example: DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver( ));
  • 10.  The third approach is to use a combination of Class.forName( ) to dynamically load the Oracle driver and then the driver classes' getInstance( ) method to work around noncompliant JVMs.  For example: Class.forName("oracle.jdbc.driver.Oracle Driver").newInstance();
  • 11. Basic steps to use a database in Java  Establish a connection  Create JDBC Statements  Execute SQL Statements  GET ResultSet  Close connections
  • 12. 1. Establish a connection  import java.sql.*;  Load the vendor specific driver ◦ Class.forName("oracle.jdbc.driver.OracleDriver");  What do you think this statement does, and how?  Dynamically loads a driver class, for Oracle database  Make the connection ◦ Connection con = DriverManager.getConnection( "jdbc:oracle:thin:@oracle-prod:1521:OPROD", username, passwd);  What do you think this statement does?  Establishes connection to database by obtaining a Connection object
  • 13. 2. Create JDBC statement(s)  Statement stmt = con.createStatement() ;  Creates a Statement object for sending SQL statements to the database
  • 14. 3.Executing SQL Statements  String createStudent = "Create table Student " + "(SSN Integer not null, Name VARCHAR(32), " + "Marks Integer)"; stmt.executeUpdate(createStudent); //What does this statement do?  String insertStudent = "Insert into Student values“ + "(123456789,abc,100)"; stmt.executeUpdate(insertStudent);
  • 15. Execute Statements  This uses executeUpdate because the SQL statement contained in createTableCoffees is a data definition language ( DDL ) statement  DDL statements are executed with executeUpdate – Create a table – Alter a table – Drop a table  executeUpdate is also used to execute SQL statements that update a table
  • 16. Execute Statements  executeUpdate is used far more often to update tables than to create them– We create a table once but update it many times  The method used most often for executing SQL statements is executeQuery  executeQuery is used to execute SELECT statements – SELECT statements are the most common SQL statements
  • 17. Entering Data to a Table  Statement stmt = con.createStatement();  stmt.executeUpdate ( "INSERT INTO COFFEES " + "VALUES ('Colombian', 101, 7.99, 0, 0)");  stmt.executeUpdate ("INSERT INTO COFFEES " + "VALUES ('French_Roast', 49, 8.99, 0, 0)");  stmt.executeUpdate ("INSERT INTO COFFEES " + "VALUES ('Espresso', 150, 9.99, 0, 0)");  stmt.executeUpdate ("INSERT INTO COFFEES " + "VALUES ('Colombian Decaf' 101 8 99 0 0)");  stmt.executeUpdate ("INSERT INTO COFFEES " +
  • 18. Batch Update  What is batch update? - Send multiple update statement in a single request to the database  Why batch update? -Better performance  How do we perform batch update? -Statement.addBatch (sqlString); - Statement.executeBatch();
  • 19. 4.Get ResultSet String queryStudent = "select * from Student"; ResultSet rs = Stmt.executeQuery(queryStudent); //What does this statement do? while (rs.next()) { int ssn = rs.getInt("SSN"); String name = rs.getString("NAME"); int marks = rs.getInt("MARKS"); }
  • 20. 5. Close connection  stmt.close();  con.close();
  • 21. Sample Program import java.sql.*; class TestThinApp { public static void main (String args[]) throws ClassNotFoundException, SQLException { Class.forName("oracle.jdbc.driver.OracleDriver "); // or you can use: DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","scott","ti
  • 22. Statement stmt = conn.createStatement( ); ResultSet rset = stmt.executeQuery( "select 'Hello Thin driver tester '||USER||'!' result from dual"); while(rset.next( )) System.out.println(rset.getString(1)); rset.close( ); stmt.close( ); conn.close( ); } }
  • 23. SUMMARY  JDBC makes it very easy to connect to DBMS and to manipulate the data in it.  You have to install the oracle driver in order to make the connection.  It is crucial to setup PATH and CLASSPATH properly
  翻译: