SlideShare a Scribd company logo
1 of 22
Download to read 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

More Related Content

What's hot

basic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptxbasic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptxAnusha sivakumar
 
Dotnet difference questions and answers compiled- 1(updated-2)
Dotnet difference questions and answers compiled- 1(updated-2)Dotnet difference questions and answers compiled- 1(updated-2)
Dotnet difference questions and answers compiled- 1(updated-2)Umar Ali
 
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFDatabase Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFOum Saokosal
 
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...Edureka!
 
servlet in java
servlet in javaservlet in java
servlet in javasowfi
 
Project Presentation on Advance Java
Project Presentation on Advance JavaProject Presentation on Advance Java
Project Presentation on Advance JavaVikas Goyal
 
Top 1000 Java Interview Questions Includes Spring, Hibernate, Microservices, ...
Top 1000 Java Interview Questions Includes Spring, Hibernate, Microservices, ...Top 1000 Java Interview Questions Includes Spring, Hibernate, Microservices, ...
Top 1000 Java Interview Questions Includes Spring, Hibernate, Microservices, ...Ankit Kumar
 
SQL Queries
SQL QueriesSQL Queries
SQL QueriesNilt1234
 
Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Sakthi Durai
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)Ishucs
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
Java database connectivity with MYSQL
Java database connectivity with MYSQLJava database connectivity with MYSQL
Java database connectivity with MYSQLAdil Mehmoood
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)slire
 

What's hot (20)

basic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptxbasic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptx
 
Databases: Normalisation
Databases: NormalisationDatabases: Normalisation
Databases: Normalisation
 
Dotnet difference questions and answers compiled- 1(updated-2)
Dotnet difference questions and answers compiled- 1(updated-2)Dotnet difference questions and answers compiled- 1(updated-2)
Dotnet difference questions and answers compiled- 1(updated-2)
 
Cursors
CursorsCursors
Cursors
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
MySQL Views
MySQL ViewsMySQL Views
MySQL Views
 
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFDatabase Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
 
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
 
servlet in java
servlet in javaservlet in java
servlet in java
 
Project Presentation on Advance Java
Project Presentation on Advance JavaProject Presentation on Advance Java
Project Presentation on Advance Java
 
Mysql joins
Mysql joinsMysql joins
Mysql joins
 
Acid properties
Acid propertiesAcid properties
Acid properties
 
Top 1000 Java Interview Questions Includes Spring, Hibernate, Microservices, ...
Top 1000 Java Interview Questions Includes Spring, Hibernate, Microservices, ...Top 1000 Java Interview Questions Includes Spring, Hibernate, Microservices, ...
Top 1000 Java Interview Questions Includes Spring, Hibernate, Microservices, ...
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1
 
Jdbc Ppt
Jdbc PptJdbc Ppt
Jdbc Ppt
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Java database connectivity with MYSQL
Java database connectivity with MYSQLJava database connectivity with MYSQL
Java database connectivity with MYSQL
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 

Viewers also liked

Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types pptkamal kotecha
 
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
 

Viewers also liked (8)

Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
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
 

Similar to Overview Of JDBC

Similar to 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 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
 
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
 

More from 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
 

Recently uploaded

SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 

Recently uploaded (20)

SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 

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. 