ADVANCE JAVA
ADVANCE JAVA
Author Profile
 Ankit Desai
 Ph.D. Scholar, IET, Ahmedabad University
 Education: M. Tech. (C.E.), B. E. (I. T.)
 Experience: 8 years (Academic and Research)
 Research Interest: IoT, Big Data Analytics, Machine
Learning, Data Mining, Algorithms.
Classified e-Material 2
ADVANCE JAVA
Classified e-Material
Java Database Connectivity
JDBC Basics
Application
ADVANCE JAVA
Classified e-Material
Agenda
 What is JDBC?
 JDBC API
 JDBC Driver
 Database URL
 Step By Step Usage of JDBC API
 Load DB-specific JDBC driver
 Get a Connection object
 Get a Statement object
 Execute queries and/or updates
 Read results
 Read Meta-data (optional step)
 Close Statement and Connection objects
ADVANCE JAVA
What is JDBC?
 Java Database
Connectivity (JDBC) is a
programming framework
to get data from
 databases
 spreadsheets
 flat files
 Standard Java API for
accessing relational
database
 Hides database specific
details from application
Classified e-Material
DBMS
Java Application
JDBC
Client Machine
DBMS.propritary protocol
Database Server
ADVANCE JAVA
Four types of JDBC Drivers
Classified e-Material
JDBC
Type I
“Bridge”
Type II
“Native”
Type III
“Middleware”
Type IV
“Pure”
ODBC
ODBC
Driver
CLI (.lib)
Middleware
Server
ADVANCE JAVA
Classified e-Material
JDBC API
 Defines a set of Java Interfaces, which are
implemented by vendor-specific JDBC Drivers
 Applications use this set of Java interfaces for
performing database operations
JDBC API Layers
Java Application JDBC API
JDBC Manager JDBC Driver API
JDBC NetDriver
JDBC.ODBC
Bridge
ODBC
Driver
Driver 1 Driver 2
ADVANCE JAVA
Classified e-Material
JDBC API
 Majority of JDBC API is located in java.sql package
 DriverManager, Connection, ResultSet,
DatabaseMetaData, ResultSetMetaData,
PreparedStatement, CallableStatement and Types
 Other advanced functionality exists in the
javax.sql package
 DataSource
ADVANCE JAVA
JDBC Class Usage
Classified e-Material
DriverManager
Driver
Connection
Statement
ResultSet
ADVANCE JAVA
Classified e-Material
JDBC Driver
 Database specific
implementation of JDBC
interfaces
 Every database server
has corresponding JDBC
driver(s)
 You can see the list of
available drivers from
 http://industry.java.sun.com/products/jdbc/drivers
ADVANCE JAVA
Classified e-Material
Database URL
 Used to make a connection to the database
 Can contain server, port, protocol etc…
 jdbc:subprotocol_name:driver_dependant_databasename
 Oracle thin driver
 Jdbc:oracle:thin:@machinename:1521:dbname
 Derby
 jdbc:derby://localhost:1527/sample
 Pointbase
 jdbc:pointbase:server://localhost/sample
ADVANCE JAVA
Classified e-Material
Agenda
 What is JDBC?
 Step By Step Usage of JDBC API
ADVANCE JAVA
Classified e-Material
Steps of Using JDBC
 1.Load DB-specific JDBC driver
 2.Get a Connection object
 3.Get a Statement object
 4.Execute queries and/or updates
 5.Read results
 6.Read Meta-data (optional step)
 7.Close Statement and Connection objects
ADVANCE JAVA
Classified e-Material
1. Load DB-Specific Database Driver
 To manually load the database driver and register it
with the DriverManager, load its class file
 Class.forName(<database-driver>)
try {
// This loads an instance of the Pointbase DB Driver.
// The driver has to be in the classpath.
Class.forName("org.apache.derby.jdbc.ClientDriver")
;
}catch (ClassNotFoundException cnfe){
System.out.println("" + cnfe);
}
Static Method of ClassClass Name
Database Driver String
ADVANCE JAVA
Classified e-Material
2. Get a Connection Object
 DriverManager class is responsible for selecting the
database and and creating the database connection
 Using DataSource is a prefered means of getting a
conection object
 Create the database connection as follows:
try {
Connection connection =
DriverManager.getConnection("jdbc:derby://localhost
:1527/sample", “app"," app ");
} catch(SQLException sqle) {
System.out.println("" + sqle);
}
Create the object of Connection Class
ADVANCE JAVA
Classified e-Material
DriverManager & Connection
 java.sql.DriverManager
 getConnection(String url, String user, String
password) throws SQLException
 java.sql.Connection
 Statement createStatement() throws SQLException
 void close() throws SQLException
 void setAutoCommit(boolean b) throws SQLException
 void commit() throws SQLException
 void rollback() throws SQLException
ADVANCE JAVA
Classified e-Material
3. Get a Statement Object
 Create a Statement Object from Connection object
 java.sql.Statement
 ResultSet executeQuery(string sql)
 int executeUpdate(String sql)
 Example:
 Statement statement = connection.createStatement();
 The same Statement object can be used for many,
unrelated queries
Create Statement Object
ADVANCE JAVA
Classified e-Material
4. Executing Query or Update
 From the Statement object, the 2 most used
commands are
 (a) QUERY (SELECT)
 ResultSet rs = statement.executeQuery("select *
from customer_tbl");
 (b) ACTION COMMAND (UPDATE/DELETE)
 int iReturnValue = statement.executeUpdate("update
manufacture_tbl set name = ‘IBM' where mfr_num =
19985678");
Execute select query
Execute update query
ADVANCE JAVA
Classified e-Material
Steps of Using JDBC
 1.Load DB-specific JDBC driver
 2.Get a Connection object
 3.Get a Statement object
 4.Execute queries and/or updates
 5.Read results
 6.Read Meta-data (optional step)
 7.Close Statement and Connection objects
ADVANCE JAVA
Classified e-Material
5. Reading Results
 Loop through ResultSet retrieving information
 java.sql.ResultSet
 boolean next()
 xxx getXxx(int columnNumber)
 xxx getXxx(String columnName)
 void close()
 The iterator is initialized to a position before the first
row
 You must call next() once to move it to the first row
ADVANCE JAVA
Classified e-Material
5. Reading Results (Continued)
 Once you have the ResultSet, you can easily
retrieve the data by looping through it
while (rs.next()){
// Wrong this will generate an error
String value0 = rs.getString(0);
// Correct!
String value1 = rs.getString(1);
int value2 = rs.getInt(2);
int value3 = rs.getInt(“ADDR_LN1");
}
Move to the next touple while end
Index starts from 1
ADVANCE JAVA
Classified e-Material
5. Reading Results (Continued)
 When retrieving data from the ResultSet, use the
appropriate getXXX() method
 getString()
 getInt()
 getDouble()
 getObject()
 There is an appropriate getXXX method of each
java.sql.Types datatype
ADVANCE JAVA
Classified e-Material
6. Read ResultSet MetaData and DatabaseMetaData (Optional)
 Once you have the ResultSet or Connection objects,
you can obtain the Meta Data about the database
or the query
 This gives valuable information about the data that
you are retrieving or the database that you are
using
 ResultSetMetaData rsMeta = rs.getMetaData();
 DatabaseMetaData dbmetadata =
connection.getMetaData();
 There are approximately 150 methods in the
DatabaseMetaData class.
Create the metadata object
ADVANCE JAVA
Classified e-Material
ResultSetMetaData Example
ResultSetMetaData meta = rs.getMetaData();
//Return the column count
int iColumnCount = meta.getColumnCount();
for (int i =1 ; i <= iColumnCount ; i++){
System.out.println(“Column Name: " +
meta.getColumnName(i));
System.out.println(“Column Type" +
meta.getColumnType(i));
System.out.println("Display Size: " +
meta.getColumnDisplaySize(i) );
System.out.println("Precision: " +
meta.getPrecision(i));
}
No of Cols
Name of Col
Datatype of Col
Precision of Col
Col Display Size
ADVANCE JAVA
Classified e-Material
Summary
 Introduction to JDBC
 JDBC APIs
 JDBC Drivers
 Database URL
 7 Steps to use JDBC
 Load DB-specific JDBC driver
 Get a Connection object
 Get a Statement object
 Execute queries and/or updates
 Read results
 Read Meta-data (optional step)


More Related Content

PPT
Jdbc ppt
PPTX
JDBC ppt
PDF
Generics
PPT
Java database connectivity
PPTX
Inner classes in java
PDF
Introduction to Java
PPTX
Java Database Connectivity (JDBC)
PPTX
Java - Generic programming
Jdbc ppt
JDBC ppt
Generics
Java database connectivity
Inner classes in java
Introduction to Java
Java Database Connectivity (JDBC)
Java - Generic programming

What's hot (20)

PPT
9. Input Output in java
PPT
Jdbc complete
PPTX
Session bean
PPT
Introduction to java beans
PPTX
JAVA AWT
DOCX
Learning sql from w3schools
PDF
SQL Overview
PPTX
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
PPTX
Socket programming in Java (PPTX)
PPS
Interface
PDF
Socket programming using java
PPTX
Interface in java ,multiple inheritance in java, interface implementation
PPT
Core java concepts
PPS
Java Exception handling
PDF
Introduction to web programming with JavaScript
PDF
Arrays in Java
PPTX
SQLite database in android
PPSX
Introduction to java
PPT
Java Networking
9. Input Output in java
Jdbc complete
Session bean
Introduction to java beans
JAVA AWT
Learning sql from w3schools
SQL Overview
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
Socket programming in Java (PPTX)
Interface
Socket programming using java
Interface in java ,multiple inheritance in java, interface implementation
Core java concepts
Java Exception handling
Introduction to web programming with JavaScript
Arrays in Java
SQLite database in android
Introduction to java
Java Networking
Ad

Viewers also liked (19)

PDF
Weather patterns
PPT
Jdbc (database in java)
PPTX
Java.sql package
PPT
PPTX
PPT
011 more swings_adv
PPTX
Power editor basics
PPTX
Chapter 11.4
PPT
JDBC Tutorial
PPT
KMUTNB - Internet Programming 6/7
PPS
Abzolute Logistic Solution
PPT
java swing programming
PPT
JDBC – Java Database Connectivity
PPS
Jdbc architecture and driver types ppt
PDF
Java GUI PART II
PPT
Java Swing
PPT
java2 swing
Weather patterns
Jdbc (database in java)
Java.sql package
011 more swings_adv
Power editor basics
Chapter 11.4
JDBC Tutorial
KMUTNB - Internet Programming 6/7
Abzolute Logistic Solution
java swing programming
JDBC – Java Database Connectivity
Jdbc architecture and driver types ppt
Java GUI PART II
Java Swing
java2 swing
Ad

Similar to JDBC (20)

PPT
jdbc_presentation.ppt
PPTX
PDF
Data access
PDF
Introduction to JDBC and database access in web applications
PDF
Advance Java Practical file
PPTX
JDBC PPT(4).pptx java Database Connectivity
PPTX
Database Access With JDBC
PDF
Jdbc[1]
PDF
JDBC programming
PDF
Lecture17
PPTX
Java DataBase Connectivity API (JDBC API)
PDF
PPTX
Java Database Connectivity (JDBC) ppt by Aamir Rafique.pptx
PPS
Jdbc api
PPTX
Enterprise Spring
PPTX
Lecture 1. java database connectivity
PDF
Java Web Programming Using Cloud Platform: Module 3
PDF
Java Web Programming [3/9] : Servlet Advanced
PPT
Java jdbc
jdbc_presentation.ppt
Data access
Introduction to JDBC and database access in web applications
Advance Java Practical file
JDBC PPT(4).pptx java Database Connectivity
Database Access With JDBC
Jdbc[1]
JDBC programming
Lecture17
Java DataBase Connectivity API (JDBC API)
Java Database Connectivity (JDBC) ppt by Aamir Rafique.pptx
Jdbc api
Enterprise Spring
Lecture 1. java database connectivity
Java Web Programming Using Cloud Platform: Module 3
Java Web Programming [3/9] : Servlet Advanced
Java jdbc

More from Ankit Desai (19)

PPTX
java Jdbc
PPTX
java code and document security
PPTX
Java Beans
PPTX
java drag and drop and data transfer
PPTX
Java RMI
PPT
Java Networking
PPTX
Presentation14 audio play
PPTX
Presentation15 parse xml
PPTX
Presentation11 sq lite
PPTX
Hadoop installation
PPTX
Presentation10 view navigation
PPTX
Presentation7 segment control
PPTX
Presentation6 ui image_view
PPTX
Presentation5 picker view
PPTX
Presentation4 date picker
PPTX
Presentation3 actionsheet alertview
PPTX
Presentation1 password
PPTX
Presentation2 gesture control
PPTX
Presentation8 silder switch_progress
java Jdbc
java code and document security
Java Beans
java drag and drop and data transfer
Java RMI
Java Networking
Presentation14 audio play
Presentation15 parse xml
Presentation11 sq lite
Hadoop installation
Presentation10 view navigation
Presentation7 segment control
Presentation6 ui image_view
Presentation5 picker view
Presentation4 date picker
Presentation3 actionsheet alertview
Presentation1 password
Presentation2 gesture control
Presentation8 silder switch_progress

Recently uploaded (20)

PDF
Rapid Prototyping: A lecture on prototyping techniques for interface design
PDF
giants, standing on the shoulders of - by Daniel Stenberg
PPTX
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
PDF
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
PDF
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
PDF
Aug23rd - Mulesoft Community Workshop - Hyd, India.pdf
PDF
The-2025-Engineering-Revolution-AI-Quality-and-DevOps-Convergence.pdf
PDF
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
PDF
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
PDF
Comparative analysis of machine learning models for fake news detection in so...
PPTX
AI-driven Assurance Across Your End-to-end Network With ThousandEyes
PDF
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PDF
Electrocardiogram sequences data analytics and classification using unsupervi...
PDF
Data Virtualization in Action: Scaling APIs and Apps with FME
PPTX
Module 1 Introduction to Web Programming .pptx
PDF
SaaS reusability assessment using machine learning techniques
PPTX
Training Program for knowledge in solar cell and solar industry
PDF
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
Rapid Prototyping: A lecture on prototyping techniques for interface design
giants, standing on the shoulders of - by Daniel Stenberg
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
Aug23rd - Mulesoft Community Workshop - Hyd, India.pdf
The-2025-Engineering-Revolution-AI-Quality-and-DevOps-Convergence.pdf
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
Comparative analysis of machine learning models for fake news detection in so...
AI-driven Assurance Across Your End-to-end Network With ThousandEyes
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
Convolutional neural network based encoder-decoder for efficient real-time ob...
Electrocardiogram sequences data analytics and classification using unsupervi...
Data Virtualization in Action: Scaling APIs and Apps with FME
Module 1 Introduction to Web Programming .pptx
SaaS reusability assessment using machine learning techniques
Training Program for knowledge in solar cell and solar industry
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf

JDBC

  • 2. ADVANCE JAVA Author Profile  Ankit Desai  Ph.D. Scholar, IET, Ahmedabad University  Education: M. Tech. (C.E.), B. E. (I. T.)  Experience: 8 years (Academic and Research)  Research Interest: IoT, Big Data Analytics, Machine Learning, Data Mining, Algorithms. Classified e-Material 2
  • 3. ADVANCE JAVA Classified e-Material Java Database Connectivity JDBC Basics Application
  • 4. ADVANCE JAVA Classified e-Material Agenda  What is JDBC?  JDBC API  JDBC Driver  Database URL  Step By Step Usage of JDBC API  Load DB-specific JDBC driver  Get a Connection object  Get a Statement object  Execute queries and/or updates  Read results  Read Meta-data (optional step)  Close Statement and Connection objects
  • 5. ADVANCE JAVA What is JDBC?  Java Database Connectivity (JDBC) is a programming framework to get data from  databases  spreadsheets  flat files  Standard Java API for accessing relational database  Hides database specific details from application Classified e-Material DBMS Java Application JDBC Client Machine DBMS.propritary protocol Database Server
  • 6. ADVANCE JAVA Four types of JDBC Drivers Classified e-Material JDBC Type I “Bridge” Type II “Native” Type III “Middleware” Type IV “Pure” ODBC ODBC Driver CLI (.lib) Middleware Server
  • 7. ADVANCE JAVA Classified e-Material JDBC API  Defines a set of Java Interfaces, which are implemented by vendor-specific JDBC Drivers  Applications use this set of Java interfaces for performing database operations JDBC API Layers Java Application JDBC API JDBC Manager JDBC Driver API JDBC NetDriver JDBC.ODBC Bridge ODBC Driver Driver 1 Driver 2
  • 8. ADVANCE JAVA Classified e-Material JDBC API  Majority of JDBC API is located in java.sql package  DriverManager, Connection, ResultSet, DatabaseMetaData, ResultSetMetaData, PreparedStatement, CallableStatement and Types  Other advanced functionality exists in the javax.sql package  DataSource
  • 9. ADVANCE JAVA JDBC Class Usage Classified e-Material DriverManager Driver Connection Statement ResultSet
  • 10. ADVANCE JAVA Classified e-Material JDBC Driver  Database specific implementation of JDBC interfaces  Every database server has corresponding JDBC driver(s)  You can see the list of available drivers from  http://industry.java.sun.com/products/jdbc/drivers
  • 11. ADVANCE JAVA Classified e-Material Database URL  Used to make a connection to the database  Can contain server, port, protocol etc…  jdbc:subprotocol_name:driver_dependant_databasename  Oracle thin driver  Jdbc:oracle:thin:@machinename:1521:dbname  Derby  jdbc:derby://localhost:1527/sample  Pointbase  jdbc:pointbase:server://localhost/sample
  • 12. ADVANCE JAVA Classified e-Material Agenda  What is JDBC?  Step By Step Usage of JDBC API
  • 13. ADVANCE JAVA Classified e-Material Steps of Using JDBC  1.Load DB-specific JDBC driver  2.Get a Connection object  3.Get a Statement object  4.Execute queries and/or updates  5.Read results  6.Read Meta-data (optional step)  7.Close Statement and Connection objects
  • 14. ADVANCE JAVA Classified e-Material 1. Load DB-Specific Database Driver  To manually load the database driver and register it with the DriverManager, load its class file  Class.forName(<database-driver>) try { // This loads an instance of the Pointbase DB Driver. // The driver has to be in the classpath. Class.forName("org.apache.derby.jdbc.ClientDriver") ; }catch (ClassNotFoundException cnfe){ System.out.println("" + cnfe); } Static Method of ClassClass Name Database Driver String
  • 15. ADVANCE JAVA Classified e-Material 2. Get a Connection Object  DriverManager class is responsible for selecting the database and and creating the database connection  Using DataSource is a prefered means of getting a conection object  Create the database connection as follows: try { Connection connection = DriverManager.getConnection("jdbc:derby://localhost :1527/sample", “app"," app "); } catch(SQLException sqle) { System.out.println("" + sqle); } Create the object of Connection Class
  • 16. ADVANCE JAVA Classified e-Material DriverManager & Connection  java.sql.DriverManager  getConnection(String url, String user, String password) throws SQLException  java.sql.Connection  Statement createStatement() throws SQLException  void close() throws SQLException  void setAutoCommit(boolean b) throws SQLException  void commit() throws SQLException  void rollback() throws SQLException
  • 17. ADVANCE JAVA Classified e-Material 3. Get a Statement Object  Create a Statement Object from Connection object  java.sql.Statement  ResultSet executeQuery(string sql)  int executeUpdate(String sql)  Example:  Statement statement = connection.createStatement();  The same Statement object can be used for many, unrelated queries Create Statement Object
  • 18. ADVANCE JAVA Classified e-Material 4. Executing Query or Update  From the Statement object, the 2 most used commands are  (a) QUERY (SELECT)  ResultSet rs = statement.executeQuery("select * from customer_tbl");  (b) ACTION COMMAND (UPDATE/DELETE)  int iReturnValue = statement.executeUpdate("update manufacture_tbl set name = ‘IBM' where mfr_num = 19985678"); Execute select query Execute update query
  • 19. ADVANCE JAVA Classified e-Material Steps of Using JDBC  1.Load DB-specific JDBC driver  2.Get a Connection object  3.Get a Statement object  4.Execute queries and/or updates  5.Read results  6.Read Meta-data (optional step)  7.Close Statement and Connection objects
  • 20. ADVANCE JAVA Classified e-Material 5. Reading Results  Loop through ResultSet retrieving information  java.sql.ResultSet  boolean next()  xxx getXxx(int columnNumber)  xxx getXxx(String columnName)  void close()  The iterator is initialized to a position before the first row  You must call next() once to move it to the first row
  • 21. ADVANCE JAVA Classified e-Material 5. Reading Results (Continued)  Once you have the ResultSet, you can easily retrieve the data by looping through it while (rs.next()){ // Wrong this will generate an error String value0 = rs.getString(0); // Correct! String value1 = rs.getString(1); int value2 = rs.getInt(2); int value3 = rs.getInt(“ADDR_LN1"); } Move to the next touple while end Index starts from 1
  • 22. ADVANCE JAVA Classified e-Material 5. Reading Results (Continued)  When retrieving data from the ResultSet, use the appropriate getXXX() method  getString()  getInt()  getDouble()  getObject()  There is an appropriate getXXX method of each java.sql.Types datatype
  • 23. ADVANCE JAVA Classified e-Material 6. Read ResultSet MetaData and DatabaseMetaData (Optional)  Once you have the ResultSet or Connection objects, you can obtain the Meta Data about the database or the query  This gives valuable information about the data that you are retrieving or the database that you are using  ResultSetMetaData rsMeta = rs.getMetaData();  DatabaseMetaData dbmetadata = connection.getMetaData();  There are approximately 150 methods in the DatabaseMetaData class. Create the metadata object
  • 24. ADVANCE JAVA Classified e-Material ResultSetMetaData Example ResultSetMetaData meta = rs.getMetaData(); //Return the column count int iColumnCount = meta.getColumnCount(); for (int i =1 ; i <= iColumnCount ; i++){ System.out.println(“Column Name: " + meta.getColumnName(i)); System.out.println(“Column Type" + meta.getColumnType(i)); System.out.println("Display Size: " + meta.getColumnDisplaySize(i) ); System.out.println("Precision: " + meta.getPrecision(i)); } No of Cols Name of Col Datatype of Col Precision of Col Col Display Size
  • 25. ADVANCE JAVA Classified e-Material Summary  Introduction to JDBC  JDBC APIs  JDBC Drivers  Database URL  7 Steps to use JDBC  Load DB-specific JDBC driver  Get a Connection object  Get a Statement object  Execute queries and/or updates  Read results  Read Meta-data (optional step) 