SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
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…..

More Related Content

PDF
JDBC : Java Database Connectivity
PPS
Jdbc architecture and driver types ppt
PPT
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
PPT
JDBC – Java Database Connectivity
PPTX
Java database connectivity with MySql
PPTX
Jdbc ppt
PPTX
Advance Java Topics (J2EE)
JDBC : Java Database Connectivity
Jdbc architecture and driver types ppt
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC – Java Database Connectivity
Java database connectivity with MySql
Jdbc ppt
Advance Java Topics (J2EE)

What's hot (20)

PPT
Struts
PPT
Introduction to java beans
PPT
Jdbc ppt
PPT
Jsp ppt
PPSX
JDBC: java DataBase connectivity
PPT
SQLITE Android
PPT
Mvc architecture
PPTX
Java Beans
PPT
MYSQL - PHP Database Connectivity
PPT
MYSQL.ppt
PDF
Model View Controller (MVC)
PPTX
Introduction to Spring Boot
PDF
07 java collection
PPTX
Spring Boot
PDF
MVC Architecture
PPTX
Java Server Pages(jsp)
PPS
Java Exception handling
PPT
Java interfaces
PPTX
Session tracking in servlets
Struts
Introduction to java beans
Jdbc ppt
Jsp ppt
JDBC: java DataBase connectivity
SQLITE Android
Mvc architecture
Java Beans
MYSQL - PHP Database Connectivity
MYSQL.ppt
Model View Controller (MVC)
Introduction to Spring Boot
07 java collection
Spring Boot
MVC Architecture
Java Server Pages(jsp)
Java Exception handling
Java interfaces
Session tracking in servlets
Ad

Viewers also liked (20)

PPT
JDBC Java Database Connectivity
PPTX
Java Database Connectivity (JDBC)
PPTX
Power final entre iguals 18 desembre
PDF
Relco Brochure Mail
RTF
PDF
How much security is enough?
PDF
SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"
PDF
Veiktais darbs nedēļas laikā
PDF
Reference Corey Jay
DOC
Promes dan-pemetaan-smtr-1
KEY
JDBC Basics (In 20 Minutes Flat)
PPTX
Creating Teaching Videos in the Studio
PPS
Jdbc example program with access and MySql
PPTX
Database Access With JDBC
PDF
Digitization in supply chain management
JDBC Java Database Connectivity
Java Database Connectivity (JDBC)
Power final entre iguals 18 desembre
Relco Brochure Mail
How much security is enough?
SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"
Veiktais darbs nedēļas laikā
Reference Corey Jay
Promes dan-pemetaan-smtr-1
JDBC Basics (In 20 Minutes Flat)
Creating Teaching Videos in the Studio
Jdbc example program with access and MySql
Database Access With JDBC
Digitization in supply chain management
Ad

Similar to JDBC ppt (20)

PPT
Basic Java Database Connectivity(JDBC)
PDF
Jdbc[1]
PDF
JDBC programming
PDF
Chapter 5 JDBC.pdf for stufent of computer andtudent It s
PPT
jdbc_presentation.ppt
PDF
PPTX
Java database connectivity
PDF
Introduction to JDBC and JDBC Drivers
PDF
Introduction to JDBC and database access in web applications
PPTX
Java- JDBC- Mazenet Solution
PPT
Jdbc (database in java)
PPT
JDBC.ppt
PPT
Chap3 3 12
PPTX
PPTX
Jdbc introduction
PPT
Java database connectivity
PPT
Java database connectivity
PPT
Jdbc connectivity
Basic Java Database Connectivity(JDBC)
Jdbc[1]
JDBC programming
Chapter 5 JDBC.pdf for stufent of computer andtudent It s
jdbc_presentation.ppt
Java database connectivity
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and database access in web applications
Java- JDBC- Mazenet Solution
Jdbc (database in java)
JDBC.ppt
Chap3 3 12
Jdbc introduction
Java database connectivity
Java database connectivity
Jdbc connectivity

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
cuic standard and advanced reporting.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Big Data Technologies - Introduction.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Encapsulation_ Review paper, used for researhc scholars
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
cuic standard and advanced reporting.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Big Data Technologies - Introduction.pptx
Modernizing your data center with Dell and AMD
CIFDAQ's Market Insight: SEC Turns Pro Crypto
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Machine learning based COVID-19 study performance prediction
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
“AI and Expert System Decision Support & Business Intelligence Systems”
NewMind AI Weekly Chronicles - August'25 Week I
Building Integrated photovoltaic BIPV_UPV.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

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