SlideShare a Scribd company logo
1 of 24
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

What's hot

Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
backdoor
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
Vadym Lotar
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 

What's hot (20)

Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Ajax
AjaxAjax
Ajax
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Servlet life cycle
Servlet life cycleServlet life cycle
Servlet life cycle
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 

Viewers also liked

How much security is enough?
How much security is enough?How much security is enough?
How much security is enough?
Sherry Jones
 
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
 

Viewers also liked (20)

Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database Connectivity
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
Power final entre iguals 18 desembre
Power final entre iguals 18 desembre Power final entre iguals 18 desembre
Power final entre iguals 18 desembre
 
Relco Brochure Mail
Relco Brochure MailRelco Brochure Mail
Relco Brochure Mail
 
Rpp ix 2
Rpp ix 2Rpp ix 2
Rpp ix 2
 
How much security is enough?
How much security is enough?How much security is enough?
How much security is enough?
 
SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"
SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"
SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"
 
Veiktais darbs nedēļas laikā
Veiktais darbs nedēļas laikāVeiktais darbs nedēļas laikā
Veiktais darbs nedēļas laikā
 
Reference Corey Jay
Reference Corey JayReference Corey Jay
Reference Corey Jay
 
ABC
ABCABC
ABC
 
Promes dan-pemetaan-smtr-1
Promes dan-pemetaan-smtr-1Promes dan-pemetaan-smtr-1
Promes dan-pemetaan-smtr-1
 
JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)
 
Creating Teaching Videos in the Studio
Creating Teaching Videos in the StudioCreating Teaching Videos in the Studio
Creating Teaching Videos in the Studio
 
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
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Digitization in supply chain management
Digitization in supply chain managementDigitization in supply chain management
Digitization in supply chain management
 

Similar to JDBC ppt

Similar to JDBC ppt (20)

Jdbc
JdbcJdbc
Jdbc
 
Lecture 1. java database connectivity
Lecture 1. java database connectivityLecture 1. java database connectivity
Lecture 1. java database connectivity
 
Select query in JDBC
Select query in JDBCSelect query in JDBC
Select query in JDBC
 
Java JDBC
Java JDBCJava JDBC
Java JDBC
 
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)
 
10 J D B C
10  J D B C10  J D B C
10 J D B C
 
Jdbc
JdbcJdbc
Jdbc
 
Data Access with JDBC
Data Access with JDBCData Access with JDBC
Data Access with JDBC
 
Jdbc
Jdbc   Jdbc
Jdbc
 
Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
 
JDBC programming
JDBC programmingJDBC programming
JDBC programming
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc connectivity
Jdbc connectivityJdbc connectivity
Jdbc connectivity
 
JDBC.ppt
JDBC.pptJDBC.ppt
JDBC.ppt
 
JDBC with MySQL.pdf
JDBC with MySQL.pdfJDBC with MySQL.pdf
JDBC with MySQL.pdf
 
JDBC with MySQL.pdf
JDBC with MySQL.pdfJDBC with MySQL.pdf
JDBC with MySQL.pdf
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
 
Jdbc
JdbcJdbc
Jdbc
 
Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

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