SlideShare uma empresa Scribd logo
1 de 22
Baixar para ler offline
Overview of JDBC(Java Database
Connectivity)
By Madhusmita Pradhan
Agenda
What is JDBC(Java Database Connectivity)

Architecture Of JDBC

Walk you through some JDBC basics

Few JDBC performance tips

What is JDBC ?







JDBC keep things simple and makes database access
easy.
Standardized SQL lets you talk to databases from
different vendors in a uniform way.
The java.sql calls go through a JDBC driver.
DB vendors are creating native JDBC drivers.
JDBC Architecture
Application








JDBC

Driver

Java code calls JDBC library
JDBC loads a driver
Driver talks to a particular database
Can have more than one driver -> more than one
database
Ideal: can change database engines without changing
any application code
JDBC Drivers






Type I: “Bridge”
Type II: “Native”
Type III: “Middleware”
Type IV: “Pure”
JDBC Drivers
Type I
“Bridge”

Type II
“Native”

ODBC

ODBC
Driver

CLI (.lib)

JDBC
Type III
“Middleware”

Type IV
“Pure”

Middleware
Server
Transaction
Transaction = more than one statement which must all
succeed (or all fail) together
If one fails, the system must reverse all previous actions
Also can’t leave DB in inconsistent state halfway through a
transaction
COMMIT = complete transaction
ROLLBACK = abort
Establish Connection
JDBC application connects to a target data source
using one of two classes:

DriverManager (Fully implemented class):
Automatically loads any JDBC 4.0 drivers found within the
class path, when this class first attempts to establish a
connection using database URL. Application must manually
load any JDBC drivers prior to version 4.0.

DataSource (Interface): A DataSource object represents
a particular DBMS. Objects instantiated by classes that
implement the DataSource represent a particular DBMS.
JDBC Class Usage
Loads, chooses drivers

DriverManager / DataSource
Connects to actual database

Driver

Connection
a single SQL statement
connection session

Statement

ResultSet
the records returned from a Statement
Database Connection URLs
Syntax: jdbc:subprotocol:source

Each driver has its own subprotocol

Each subprotocol has its own syntax for the source
For MySQL:
jdbc:mysql://localhost:3306/TestDB
Connection using DriverManager
Class.forName("com.mysql.jdbc.Driver"); // Only for JDBC
version prior to 4.0
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/
test","root", "mindfire");
Connection using DataSource
Context context = new InitialContext();
DataSource dataSource = (DataSource)
context.lookup("jdbc/DataSource");
Connection connection = dataSource.getConnection();
// Here JNDI has been used for register a DataSource
Advantages of Using DataSource Object
Programmers no longer have to hard code the driver name or
JDBC URL in their applications, which makes them more
portable.

DataSource properties make maintaining code much simpler
in terms of maintenance

Includes additional implementations like ConnectionPooling
and Distributed Transactions.

Statement Methods
ResultSet executeQuery(String)
Execute a SQL statement that returns a single ResultSet.


int executeUpdate(String)
Execute a SQL INSERT, UPDATE or DELETE statement.
Returns the number of rows changed.


boolean execute(String)
Execute a SQL statement that may return multiple results.
Statement.getMoreResults(), used for fetch next available
ResultSet, if any return true otherwise return false.


Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
Statement Methods (Cont.)
If you want to execute a Statement object many times, it
usually reduces execution time to use a PreparedStatement
object instead.
When the PreparedStatement is executed, the DBMS can just
run the PreparedStatement SQL statement without having to
compile it first.
PreparedStatement stmt = con.prepareStatement(SQLQueryString);
ResultSet rs = stmt.executeQuery();
ResultSet
A ResultSet object is a table of data representing a database
result set, which is usually generated by executing a
statement that queries the database.


Access data in ResultSet object through cursor(Not database
cursor). This cursor is a pointer that points to one row in a
resultset.


Only one ResultSet per Statement can be open at once.



A ResultSet object is automatically closed when the
Statement object that generated it is closed, re-executed,or
used to retrieve the next result from a sequence of multiple
results.

ResultSet Characteristics
ResultSet Type:

TYPE_FORWARD_ONLY(Default): cannot be scrolled, its
cursor moves forward only.

TYPE_SCROLL_INSENSITIVE: result can be scrolled, its
cursor can move both forward and backward relative to the
current position, and it can move to an absolute position. The
result set is insensitive to changes made to the underlying
data source while it is open.

TYPE_SCROLL_SENSITIVE: result can be scrolled, its
cursor can move both forward and backward relative to the
current position, and it can move to an absolute position. The
result set reflects changes made to the underlying data source
while the result set remains open.
ResultSet Characteristics (Cont.)
ResultSet Concurrency:

CONCUR_READ_ONLY(Default): The ResultSet object
cannot be updated.

CONCUR_UPDATABLE: The ResultSet object can be
updated.
NOTE: Not all JDBC drivers and databases support
concurrency.
DatabaseMetaData.supportsResultSetConcurrency(), method
can be used for this purpose.
ResultSet Characteristics (Cont.)
ResultSet Holdability: ResultSet property holdability gives
the application control over whether ResultSet objects
(cursors) are closed when commit is called.


HOLD_CURSORS_OVER_COMMIT: ResultSet cursors
are not closed; they are holdable: they are held open when the
method commit is called. Holdable cursors might be ideal if
your application uses mostly read-only ResultSet objects.


CLOSE_CURSORS_AT_COMMIT: ResultSet objects
(cursors) are closed when the commit method is called.
Closing cursors when this method is called can result in
better performance for some applications.

Batch Update
For execute list of associate commands that may contain
statements for update, insert, or delete a row; and it may also
contain DDL statements such as CREATE TABLE and
DROP TABLE.


con.setAutoCommit(false);
PreparedStatement pstmt = con.prepareStatement("INSERT INTO
products VALUES( ?)");
pstmt.setString(1, "Amaretto");
pstmt.addBatch();
pstmt.setString(1, "Hazelnut");
pstmt.addBatch();
// ... and so on for each new
int [] updateCounts = pstmt.executeBatch();
con.commit();
Performance Tips
Use PreparedStatement



Use Batch Update



Disable auto-commit



Always close Statement, PreparedStatement and Connection



Choose suitable(latest) JDBC driver because it directly
effects the performance at DAO layer.

Thank You

Mais conteúdo relacionado

Mais procurados

JDBC Architecture and Drivers
JDBC Architecture and DriversJDBC Architecture and Drivers
JDBC Architecture and DriversSimoniShah6
 
Inter thread communication
Inter thread communicationInter thread communication
Inter thread communicationsubash andey
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Pooja Talreja
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - CoreDzmitry Naskou
 
Unit I Database concepts - RDBMS & ORACLE
Unit I  Database concepts - RDBMS & ORACLEUnit I  Database concepts - RDBMS & ORACLE
Unit I Database concepts - RDBMS & ORACLEDrkhanchanaR
 
SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredDanish Mehraj
 
Advance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-databaseAdvance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-databasePayal Dungarwal
 
JAVA ENVIRONMENT
JAVA  ENVIRONMENTJAVA  ENVIRONMENT
JAVA ENVIRONMENTjosemachoco
 
Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...Duckademy IT courses
 
Java Servlets
Java ServletsJava Servlets
Java ServletsNitin Pai
 

Mais procurados (20)

JDBC Architecture and Drivers
JDBC Architecture and DriversJDBC Architecture and Drivers
JDBC Architecture and Drivers
 
Sql server basics
Sql server basicsSql server basics
Sql server basics
 
Inter thread communication
Inter thread communicationInter thread communication
Inter thread communication
 
Java J2EE
Java J2EEJava J2EE
Java J2EE
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
Jdbc complete
Jdbc completeJdbc complete
Jdbc complete
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
Introduction to EJB
Introduction to EJBIntroduction to EJB
Introduction to EJB
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Unit I Database concepts - RDBMS & ORACLE
Unit I  Database concepts - RDBMS & ORACLEUnit I  Database concepts - RDBMS & ORACLE
Unit I Database concepts - RDBMS & ORACLE
 
SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics Covered
 
Advance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-databaseAdvance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-database
 
Session bean
Session beanSession bean
Session bean
 
JAVA ENVIRONMENT
JAVA  ENVIRONMENTJAVA  ENVIRONMENT
JAVA ENVIRONMENT
 
Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Advanced sql
Advanced sqlAdvanced sql
Advanced sql
 
Java Swing
Java SwingJava Swing
Java Swing
 
Java bean
Java beanJava bean
Java bean
 

Destaque

Types of Drivers in JDBC
Types of Drivers in JDBCTypes of Drivers in JDBC
Types of Drivers in JDBCHemant Sharma
 
Applet skelton58
Applet skelton58Applet skelton58
Applet skelton58myrajendra
 
Jdbc Java Programming
Jdbc Java ProgrammingJdbc Java Programming
Jdbc Java Programmingchhaichivon
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Appletsamitksaha
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivityTanmoy Barman
 

Destaque (7)

Types of Drivers in JDBC
Types of Drivers in JDBCTypes of Drivers in JDBC
Types of Drivers in JDBC
 
Applet skelton58
Applet skelton58Applet skelton58
Applet skelton58
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
 
Jdbc Java Programming
Jdbc Java ProgrammingJdbc Java Programming
Jdbc Java Programming
 
Jdbc
JdbcJdbc
Jdbc
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 

Semelhante a Overview Of JDBC

Semelhante a Overview Of JDBC (20)

Core jdbc basics
Core jdbc basicsCore jdbc basics
Core jdbc basics
 
Jdbc introduction
Jdbc introductionJdbc introduction
Jdbc introduction
 
JDBC.ppt
JDBC.pptJDBC.ppt
JDBC.ppt
 
03-JDBC.pptx
03-JDBC.pptx03-JDBC.pptx
03-JDBC.pptx
 
Advance Java Practical file
Advance Java Practical fileAdvance Java Practical file
Advance Java Practical file
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
Jdbc
JdbcJdbc
Jdbc
 
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 java for learning java for learn.ppt
JDBC java for learning java for learn.pptJDBC java for learning java for learn.ppt
JDBC java for learning java for learn.ppt
 
Unit 5.pdf
Unit 5.pdfUnit 5.pdf
Unit 5.pdf
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC-Introduction
JDBC-IntroductionJDBC-Introduction
JDBC-Introduction
 
Final Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptFinal Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.ppt
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC
JDBCJDBC
JDBC
 
Prashanthi
PrashanthiPrashanthi
Prashanthi
 
JDBC : Java Database Connectivity
JDBC : Java Database Connectivity JDBC : Java Database Connectivity
JDBC : Java Database Connectivity
 

Mais de Mindfire Solutions (20)

Physician Search and Review
Physician Search and ReviewPhysician Search and Review
Physician Search and Review
 
diet management app
diet management appdiet management app
diet management app
 
Business Technology Solution
Business Technology SolutionBusiness Technology Solution
Business Technology Solution
 
Remote Health Monitoring
Remote Health MonitoringRemote Health Monitoring
Remote Health Monitoring
 
Influencer Marketing Solution
Influencer Marketing SolutionInfluencer Marketing Solution
Influencer Marketing Solution
 
ELMAH
ELMAHELMAH
ELMAH
 
High Availability of Azure Applications
High Availability of Azure ApplicationsHigh Availability of Azure Applications
High Availability of Azure Applications
 
IOT Hands On
IOT Hands OnIOT Hands On
IOT Hands On
 
Glimpse of Loops Vs Set
Glimpse of Loops Vs SetGlimpse of Loops Vs Set
Glimpse of Loops Vs Set
 
Oracle Sql Developer-Getting Started
Oracle Sql Developer-Getting StartedOracle Sql Developer-Getting Started
Oracle Sql Developer-Getting Started
 
Adaptive Layout In iOS 8
Adaptive Layout In iOS 8Adaptive Layout In iOS 8
Adaptive Layout In iOS 8
 
Introduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/MacIntroduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/Mac
 
LINQPad - utility Tool
LINQPad - utility ToolLINQPad - utility Tool
LINQPad - utility Tool
 
Get started with watch kit development
Get started with watch kit developmentGet started with watch kit development
Get started with watch kit development
 
Swift vs Objective-C
Swift vs Objective-CSwift vs Objective-C
Swift vs Objective-C
 
Material Design in Android
Material Design in AndroidMaterial Design in Android
Material Design in Android
 
Introduction to OData
Introduction to ODataIntroduction to OData
Introduction to OData
 
Ext js Part 2- MVC
Ext js Part 2- MVCExt js Part 2- MVC
Ext js Part 2- MVC
 
ExtJs Basic Part-1
ExtJs Basic Part-1ExtJs Basic Part-1
ExtJs Basic Part-1
 
Spring Security Introduction
Spring Security IntroductionSpring Security Introduction
Spring Security Introduction
 

Último

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
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
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)wesley chun
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
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 2024Rafal Los
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
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 Scriptwesley chun
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
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
 

Último (20)

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
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
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)
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
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
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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
 

Overview Of JDBC

  • 1. Overview of JDBC(Java Database Connectivity) By Madhusmita Pradhan
  • 2. Agenda What is JDBC(Java Database Connectivity)  Architecture Of JDBC  Walk you through some JDBC basics  Few JDBC performance tips 
  • 3. What is JDBC ?     JDBC keep things simple and makes database access easy. Standardized SQL lets you talk to databases from different vendors in a uniform way. The java.sql calls go through a JDBC driver. DB vendors are creating native JDBC drivers.
  • 4. JDBC Architecture Application      JDBC Driver Java code calls JDBC library JDBC loads a driver Driver talks to a particular database Can have more than one driver -> more than one database Ideal: can change database engines without changing any application code
  • 5. JDBC Drivers     Type I: “Bridge” Type II: “Native” Type III: “Middleware” Type IV: “Pure”
  • 6. JDBC Drivers Type I “Bridge” Type II “Native” ODBC ODBC Driver CLI (.lib) JDBC Type III “Middleware” Type IV “Pure” Middleware Server
  • 7. Transaction Transaction = more than one statement which must all succeed (or all fail) together If one fails, the system must reverse all previous actions Also can’t leave DB in inconsistent state halfway through a transaction COMMIT = complete transaction ROLLBACK = abort
  • 8. Establish Connection JDBC application connects to a target data source using one of two classes:  DriverManager (Fully implemented class): Automatically loads any JDBC 4.0 drivers found within the class path, when this class first attempts to establish a connection using database URL. Application must manually load any JDBC drivers prior to version 4.0.  DataSource (Interface): A DataSource object represents a particular DBMS. Objects instantiated by classes that implement the DataSource represent a particular DBMS.
  • 9. JDBC Class Usage Loads, chooses drivers DriverManager / DataSource Connects to actual database Driver Connection a single SQL statement connection session Statement ResultSet the records returned from a Statement
  • 10. Database Connection URLs Syntax: jdbc:subprotocol:source  Each driver has its own subprotocol  Each subprotocol has its own syntax for the source For MySQL: jdbc:mysql://localhost:3306/TestDB
  • 11. Connection using DriverManager Class.forName("com.mysql.jdbc.Driver"); // Only for JDBC version prior to 4.0 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ test","root", "mindfire");
  • 12. Connection using DataSource Context context = new InitialContext(); DataSource dataSource = (DataSource) context.lookup("jdbc/DataSource"); Connection connection = dataSource.getConnection(); // Here JNDI has been used for register a DataSource
  • 13. Advantages of Using DataSource Object Programmers no longer have to hard code the driver name or JDBC URL in their applications, which makes them more portable.  DataSource properties make maintaining code much simpler in terms of maintenance  Includes additional implementations like ConnectionPooling and Distributed Transactions. 
  • 14. Statement Methods ResultSet executeQuery(String) Execute a SQL statement that returns a single ResultSet.  int executeUpdate(String) Execute a SQL INSERT, UPDATE or DELETE statement. Returns the number of rows changed.  boolean execute(String) Execute a SQL statement that may return multiple results. Statement.getMoreResults(), used for fetch next available ResultSet, if any return true otherwise return false.  Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query);
  • 15. Statement Methods (Cont.) If you want to execute a Statement object many times, it usually reduces execution time to use a PreparedStatement object instead. When the PreparedStatement is executed, the DBMS can just run the PreparedStatement SQL statement without having to compile it first. PreparedStatement stmt = con.prepareStatement(SQLQueryString); ResultSet rs = stmt.executeQuery();
  • 16. ResultSet A ResultSet object is a table of data representing a database result set, which is usually generated by executing a statement that queries the database.  Access data in ResultSet object through cursor(Not database cursor). This cursor is a pointer that points to one row in a resultset.  Only one ResultSet per Statement can be open at once.  A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed,or used to retrieve the next result from a sequence of multiple results. 
  • 17. ResultSet Characteristics ResultSet Type:  TYPE_FORWARD_ONLY(Default): cannot be scrolled, its cursor moves forward only.  TYPE_SCROLL_INSENSITIVE: result can be scrolled, its cursor can move both forward and backward relative to the current position, and it can move to an absolute position. The result set is insensitive to changes made to the underlying data source while it is open.  TYPE_SCROLL_SENSITIVE: result can be scrolled, its cursor can move both forward and backward relative to the current position, and it can move to an absolute position. The result set reflects changes made to the underlying data source while the result set remains open.
  • 18. ResultSet Characteristics (Cont.) ResultSet Concurrency:  CONCUR_READ_ONLY(Default): The ResultSet object cannot be updated.  CONCUR_UPDATABLE: The ResultSet object can be updated. NOTE: Not all JDBC drivers and databases support concurrency. DatabaseMetaData.supportsResultSetConcurrency(), method can be used for this purpose.
  • 19. ResultSet Characteristics (Cont.) ResultSet Holdability: ResultSet property holdability gives the application control over whether ResultSet objects (cursors) are closed when commit is called.  HOLD_CURSORS_OVER_COMMIT: ResultSet cursors are not closed; they are holdable: they are held open when the method commit is called. Holdable cursors might be ideal if your application uses mostly read-only ResultSet objects.  CLOSE_CURSORS_AT_COMMIT: ResultSet objects (cursors) are closed when the commit method is called. Closing cursors when this method is called can result in better performance for some applications. 
  • 20. Batch Update For execute list of associate commands that may contain statements for update, insert, or delete a row; and it may also contain DDL statements such as CREATE TABLE and DROP TABLE.  con.setAutoCommit(false); PreparedStatement pstmt = con.prepareStatement("INSERT INTO products VALUES( ?)"); pstmt.setString(1, "Amaretto"); pstmt.addBatch(); pstmt.setString(1, "Hazelnut"); pstmt.addBatch(); // ... and so on for each new int [] updateCounts = pstmt.executeBatch(); con.commit();
  • 21. Performance Tips Use PreparedStatement  Use Batch Update  Disable auto-commit  Always close Statement, PreparedStatement and Connection  Choose suitable(latest) JDBC driver because it directly effects the performance at DAO layer. 