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

What's hot

Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introductionJonathan Holloway
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Pooja Talreja
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API07.pallav
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database ConnectivityRanjan Kumar
 
Introduction à spring boot
Introduction à spring bootIntroduction à spring boot
Introduction à spring bootAntoine Rey
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivityVaishali Modi
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets Hitesh-Java
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepGuo Albert
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework tola99
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!Jakub Kubrynski
 
Spring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesSpring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesVMware Tanzu
 

What's hot (20)

Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database Connectivity
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Introduction à spring boot
Introduction à spring bootIntroduction à spring boot
Introduction à spring boot
 
JDBC ppt
JDBC pptJDBC ppt
JDBC ppt
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Java 8 Streams
Java 8 StreamsJava 8 Streams
Java 8 Streams
 
JPA For Beginner's
JPA For Beginner'sJPA For Beginner's
JPA For Beginner's
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
 
Spring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesSpring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutes
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 

Viewers also liked (18)

Weather patterns
Weather patternsWeather patterns
Weather patterns
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
 
Java.sql package
Java.sql packageJava.sql package
Java.sql package
 
Swing
SwingSwing
Swing
 
Jtextarea
JtextareaJtextarea
Jtextarea
 
011 more swings_adv
011 more swings_adv011 more swings_adv
011 more swings_adv
 
Power editor basics
Power editor basicsPower editor basics
Power editor basics
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC Tutorial
JDBC TutorialJDBC Tutorial
JDBC Tutorial
 
KMUTNB - Internet Programming 6/7
KMUTNB - Internet Programming 6/7KMUTNB - Internet Programming 6/7
KMUTNB - Internet Programming 6/7
 
Abzolute Logistic Solution
Abzolute Logistic SolutionAbzolute Logistic Solution
Abzolute Logistic Solution
 
java swing programming
java swing programming java swing programming
java swing programming
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
Java GUI PART II
Java GUI PART IIJava GUI PART II
Java GUI PART II
 
Java Swing
Java SwingJava Swing
Java Swing
 
Jdbc Ppt
Jdbc PptJdbc Ppt
Jdbc Ppt
 
java2 swing
java2 swingjava2 swing
java2 swing
 

Similar to JDBC

jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.pptDrMeenakshiS
 
Introduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsIntroduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsFulvio Corno
 
Advance Java Practical file
Advance Java Practical fileAdvance Java Practical file
Advance Java Practical filevarun arora
 
Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Luzan Baral
 
Enterprise Spring
Enterprise SpringEnterprise Spring
Enterprise SpringEmprovise
 
Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3IMC Institute
 
Java Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedJava Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedIMC Institute
 
3.java database connectivity
3.java database connectivity3.java database connectivity
3.java database connectivityweb360
 

Similar to JDBC (20)

jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC
JDBCJDBC
JDBC
 
Data access
Data accessData access
Data access
 
Introduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsIntroduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applications
 
Advance Java Practical file
Advance Java Practical fileAdvance Java Practical file
Advance Java Practical file
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
 
JDBC programming
JDBC programmingJDBC programming
JDBC programming
 
Lecture17
Lecture17Lecture17
Lecture17
 
Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc api
Jdbc apiJdbc api
Jdbc api
 
Enterprise Spring
Enterprise SpringEnterprise Spring
Enterprise Spring
 
Lecture 1. java database connectivity
Lecture 1. java database connectivityLecture 1. java database connectivity
Lecture 1. java database connectivity
 
Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3
 
Java Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedJava Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet Advanced
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
 
3.java database connectivity
3.java database connectivity3.java database connectivity
3.java database connectivity
 
JDBC Part - 2
JDBC Part - 2JDBC Part - 2
JDBC Part - 2
 

More from Ankit Desai

java code and document security
java code and document securityjava code and document security
java code and document securityAnkit Desai
 
java drag and drop and data transfer
java drag and drop and data transferjava drag and drop and data transfer
java drag and drop and data transferAnkit Desai
 
Presentation14 audio play
Presentation14 audio playPresentation14 audio play
Presentation14 audio playAnkit Desai
 
Presentation15 parse xml
Presentation15 parse xmlPresentation15 parse xml
Presentation15 parse xmlAnkit Desai
 
Presentation11 sq lite
Presentation11 sq litePresentation11 sq lite
Presentation11 sq liteAnkit Desai
 
Hadoop installation
Hadoop installationHadoop installation
Hadoop installationAnkit Desai
 
Presentation10 view navigation
Presentation10 view navigationPresentation10 view navigation
Presentation10 view navigationAnkit Desai
 
Presentation7 segment control
Presentation7 segment controlPresentation7 segment control
Presentation7 segment controlAnkit Desai
 
Presentation6 ui image_view
Presentation6 ui image_viewPresentation6 ui image_view
Presentation6 ui image_viewAnkit Desai
 
Presentation5 picker view
Presentation5 picker viewPresentation5 picker view
Presentation5 picker viewAnkit Desai
 
Presentation4 date picker
Presentation4 date pickerPresentation4 date picker
Presentation4 date pickerAnkit Desai
 
Presentation3 actionsheet alertview
Presentation3 actionsheet alertviewPresentation3 actionsheet alertview
Presentation3 actionsheet alertviewAnkit Desai
 
Presentation1 password
Presentation1 passwordPresentation1 password
Presentation1 passwordAnkit Desai
 
Presentation2 gesture control
Presentation2 gesture controlPresentation2 gesture control
Presentation2 gesture controlAnkit Desai
 
Presentation8 silder switch_progress
Presentation8 silder switch_progressPresentation8 silder switch_progress
Presentation8 silder switch_progressAnkit Desai
 

More from Ankit Desai (19)

java Jdbc
java Jdbc java Jdbc
java Jdbc
 
java code and document security
java code and document securityjava code and document security
java code and document security
 
Java Beans
Java BeansJava Beans
Java Beans
 
java drag and drop and data transfer
java drag and drop and data transferjava drag and drop and data transfer
java drag and drop and data transfer
 
Java RMI
Java RMIJava RMI
Java RMI
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Presentation14 audio play
Presentation14 audio playPresentation14 audio play
Presentation14 audio play
 
Presentation15 parse xml
Presentation15 parse xmlPresentation15 parse xml
Presentation15 parse xml
 
Presentation11 sq lite
Presentation11 sq litePresentation11 sq lite
Presentation11 sq lite
 
Hadoop installation
Hadoop installationHadoop installation
Hadoop installation
 
Presentation10 view navigation
Presentation10 view navigationPresentation10 view navigation
Presentation10 view navigation
 
Presentation7 segment control
Presentation7 segment controlPresentation7 segment control
Presentation7 segment control
 
Presentation6 ui image_view
Presentation6 ui image_viewPresentation6 ui image_view
Presentation6 ui image_view
 
Presentation5 picker view
Presentation5 picker viewPresentation5 picker view
Presentation5 picker view
 
Presentation4 date picker
Presentation4 date pickerPresentation4 date picker
Presentation4 date picker
 
Presentation3 actionsheet alertview
Presentation3 actionsheet alertviewPresentation3 actionsheet alertview
Presentation3 actionsheet alertview
 
Presentation1 password
Presentation1 passwordPresentation1 password
Presentation1 password
 
Presentation2 gesture control
Presentation2 gesture controlPresentation2 gesture control
Presentation2 gesture control
 
Presentation8 silder switch_progress
Presentation8 silder switch_progressPresentation8 silder switch_progress
Presentation8 silder switch_progress
 

Recently uploaded

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 DevelopmentsTrustArc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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 StrategiesBoston Institute of Analytics
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
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 2024The Digital Insurer
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
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 educationjfdjdjcjdnsjd
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
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 2024The Digital Insurer
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Recently uploaded (20)

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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
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
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

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) 