SlideShare a Scribd company logo
Java Servlets
JDBC and database connectivity
Eleonora Ciceri
ciceri@elet.polimi.it
Persistent data
¤  When data have to be stored persistently, i.e., so as to
restore them when needed, a database can be used
¤  A database is a structured collection of data, that are
typically organized to model relevant aspects of reality,
e.g., availability of rooms in a hotel
¤  A database management system (DBMS) is a set of
programs that enables you to store, modify and extract
information from a database
Relational databases
¤  A Relational Database Management System (RDMBS)
organizes data into tables
¤  Each table is organized into rows and columns
¤  Particular rows and columns in a table can be related to
one or more rows and columns in another table
¤  Data can be read, updated, appended and deleted
using the SQL language
¤  SQL: Structured Query Language, application-specific
declarative language used for handling a relational
database
Database connection flow
¤  Database interaction flow
¤  Connect to the database
¤  Send a SQL statement
¤  Read the results
¤  Close connection
Get connection
SQL query
Query result
Interfaces
¤  Web applications can connect to databases, but each
of them has its own interface that can be used in order to
manipulate data
¤  ODBC
¤  Open Database Connectivity. Interface written in C
¤  Problem: programs written in C are not as portable as the
ones written in Java
¤  JDBC:
¤  Java Database Connectivity. Interface written in Java
¤  It can be seen as a porting of ODBC in Java
JDBC
¤  JDBC is a SQL-level API that allows to execute SQL
statements and retrieve the results
Application
The driver
converts JDBC
method calls into
ODBC function
calls.
Package java.sql (1)
¤  java.sql.Connection
¤  Connection (session) with a specific database
¤  SQL statements are executed and results are returned within the
context of a connection
¤  java.sql.Statement
¤  Used for executing a static SQL statement and returning the
results it produces
¤  Only one ResultSet object per statement can be opened at
the same time. If two ResultSet are read at the same time,
they have been generated by different Statement objects
¤  All execution methods in the Statement interface implicitly close
a statement's current ResultSet object if an opened one exists
Package java.sql (2)
¤  java.sql.ResultSet
¤  A table of data representing a database result set, which
is usually generated by executing a statement that
queries the database
¤  java.sql.DriverManager
¤  The basic service for managing a set of JDBC drivers
¤  When initialized, it will attempt to load the driver classes
referenced in the "jdbc.drivers" system property
¤  When the method getConnection is called, it will attempt to
locate a suitable driver from amongst those loaded at
initialization and those loaded explicitly
Example: DBPhoneLookup (1)
import java.io.*;!
import java.sql.*;!
!
import javax.servlet.*;!
import javax.servlet.http.*;!
!
public class DBPhoneLookup extends HttpServlet {!
!
public void doGet(HttpServletRequest request,
HttpServletResponse response)!
! !throws ServletException, IOException {!
Connection connection = null;!
! !!
response.setContentType("text/plain");!
PrintWriter out = response.getWriter();!
! !!
Package for JDBC + SQL
Example: DBPhoneLookup (2)
try {!
Class.forName("com.mysql.jdbc.Driver");!
!
connection = DriverManager.getConnection!
("jdbc:mysql://localhost:3306/test", "root", "");!
!
Statement statement = connection.createStatement();!
ResultSet resultSet = statement.executeQuery("SELECT * !
FROM employees");!
! ! !!
out.println("Phone numbers:");!
while (resultSet.next())!
out.println("- " + resultSet.getString("name") + ":
" + resultSet.getString("phone"));!
}!
Driver used for connecting to a MySQL database
Retrieve a
connection
Database address
Username Password
SQL query
Navigate result set
Retrieve an attribute from
the current tuple
Example: DBPhoneLookup (3)
catch (ClassNotFoundException e) {!
out.println("Couldn't load database driver: " + e.getMessage());!
}!
!
catch (SQLException e) {!
out.println("SQLexception caught: " + e.getMessage());!
}!
! !!
finally {!
if (connection != null) {!
try {!
connection.close();!
}!
catch (SQLException ignored) {!
}!
}!
}!
}!
}
Always close the
connection!!
More syntax
¤  When the attributes names and types are unknown,
ResultSetMetadata gives us a way of reading them
¤  resultSet.getMetadata(): reads the result set metadata
¤  metadata.getColumnCount(): returns the number of columns
in the result set
¤  metadata.getColumnLabel(i): returns the name of the i-th
column in the result set
¤  Result set data retrieval
¤  resultSet.getObject(): extract a non-String value from the
result set
¤  resultSet.getString(): extract a String value from the result
set
Modify data
¤  Execute a generic query:
¤  boolean state = statement.execute(query);
¤  When used for updating: the state is false since a result set is not
retrieved (the updated tuples count is retrieved)
¤  Execute an update:
¤  int count = statement.executeUpdate(updateQuery);
¤  Example: “DELETE FROM products WHERE id = 7”
¤  Retrieve results:
¤  resultSet = statement.getResultSet();
¤  count = statement.getUpdateCount();
Prepared statements
¤  A PreparedStatement is a Statement objects that is
precompiled by the database for faster execution
¤  Example: PreparedStatement statement =
connection.prepareStatement(“INSERT INTO ORDERS(ID,
CLIENTID, TOTAL) VALUES (?,?,?)”);
¤  The placeholders are substituted later:
statement.clearParameters();
statement.setInt(1,2);
statement.setInt(2,4);
statement.setDouble(3, 53.43);
statement.executeUpdate();
Reuse database connections
¤  Since creating and deleting database connections is
very expensive, a good idea is to reuse them
¤  How?
¤  Create them in the init() method
¤  Another “good practice”?
¤  Store the database connection parameter in the context.
Pro: sharing across all servlets, not written in the code
Reuse database connections (2)
public void init(ServletConfig config) throws ServletException {!
try {!
Class.forName("com.mysql.jdbc.Driver");!
!
ServletContext context = config.getServletContext();!
String url = context.getInitParameter("dbUrl");!
String user = context.getInitParameter("dbUser");!
String password = context.getInitParameter("dbPassword");!
connection = DriverManager.getConnection(url, user, password); ! !!
}!
catch (ClassNotFoundException e) {!
! throw new UnavailableException("Couldn't load database driver");!
}!
catch (SQLException e) {!
! throw new UnavailableException("Couldn't get db connection");!
} ! !
}
Retrieve parameters
from the context
Examples: DBPhoneLookupReuse + HtmlSQLResult
Transactions
¤  A transaction comprises a unit of work performed within a
DBMS against a database
¤  Example: a series of SQL queries for updating data in a
database when a user performs an order
¤  A database transaction must be:
¤  Atomic: all occur, or nothing occurs
¤  Consistent: it does not violate any integrity constraint during the
execution
¤  Isolated: defines how and when the changes made by the
transaction become visible to other operations
¤  Durable: transactions that have committed will survive
permanently
Transactions vs. JDBC
¤  Transactions are managed by using the Connection
object
¤  Use the transactions:
¤  connection.setAutoCommit(false);
¤  Commit the result:
¤  connection.commit();
¤  Rollback when an error occurs:
¤  connection.rollback();
Transactions vs. JDBC: example
synchronized (connection) {!
connection.setAutoCommit(false);!
!
PreparedStatement statement = connection.prepareStatement("INSERT
INTO employees (NAME, PHONE, DepID) VALUES (?, ?, ?)");!
statement.setString(1, (String) request.getParameter("name"));!
statement.setString(2, (String) request.getParameter("phone"));!
statement.setInt(3, 2);!
statement.executeUpdate();!
!
Statement statement2 = connection.createStatement();!
statement2.executeUpdate("UPDATE departments SET numemployees =
(numemployees + 1) WHERE id = 2"); ! ! !
} ! ! ! ! !
!
connection.commit(); !
Activate transactions
Execute the
first query
Execute the
second query
rollback(): called whenever an Exception is captured
Examples: DBPhoneTransaction
CallableStatement (1)
¤  The interface used to execute SQL stored procedures
¤  The provided syntax allows stored procedures to be called in
a standard way for all RDBMSs.
¤  Call: connection.prepareCall(java.lang.String)
¤  The syntax requires one result parameter (optional,
indicated as OUT) and several IN/OUT parameters.
Parameters are referred using numbers, starting from 1
¤  {?= call <procedure-name>[<arg1>,<arg2>, ...]}
¤  {call <procedure-name>[<arg1>,<arg2>, ...]}
CallableStatement (2)
¤  Handling parameters:
¤  IN: set using the set methods of PreparedStatement
¤  OUT: set the type before the call, retrieve the values
after the execution using the get methods
¤  Result: multiple ResultSet objects are handled using
operations inherited from Statement
javax.sql.*
¤  The package provides the API for server side data source
access and processing
¤  It integrates java.sql.*
¤  The package provides for the following:
¤  The DataSource interface as an alternative to the
DriverManager for establishing a connection with a data
source
¤  Connection pooling
¤  Distributed transaction
Connection pooling
¤  This procedure improves performance, avoiding to
create new connections
¤  A connection is used and reused
¤  The number of new connections that need to be created is
cut down
¤  Connection pooling is transparent and done
automatically in the middle tier of a J2EE configuration
Connection pooling: example
¤  init()
ServletContext context = config.getServletContext(); 	
String loginUser = context.getInitParameter("dbUser");	
String loginPasswd = context.getInitParameter
("dbPassword");	
String driver = context.getInitParameter("dbDriver");	
String loginUrl = context.getInitParameter("dbUrl"); 	
	 	 	 		
dataSource = setupDataSource(driver, loginUrl, loginUser,
loginPasswd);
¤  doGet()
connection = dataSource.getConnection();
Examples: DBPhonePool+ DBPhoneSingle
References
References
¤  Java Servlet Programming, Jason Hunter and William
Crawford, O’Reilly

More Related Content

What's hot

Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
Nuha Noor
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
Sasidhar Kothuru
 
Knowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletKnowledge Sharing : Java Servlet
Knowledge Sharing : Java Servlet
Fahmi Jafar
 
Jsp servlets
Jsp servletsJsp servlets
Jsp servlets
Rajavel Dhandabani
 
Java Servlet
Java Servlet Java Servlet
Java Servlet
Rajiv Gupta
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
Ankit Minocha
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packagesvamsi krishna
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
slire
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
Vaishali Modi
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technology
Minal Maniar
 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet Solution
Mazenetsolution
 
Java server pages
Java server pagesJava server pages
Java server pages
Tanmoy Barman
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
BG Java EE Course
 
Java servlets
Java servletsJava servlets
Java servlets
Mukesh Tekwani
 
Servlets
ServletsServlets
JDBC
JDBCJDBC
Java EE 01-Servlets and Containers
Java EE 01-Servlets and ContainersJava EE 01-Servlets and Containers
Java EE 01-Servlets and Containers
Fernando Gil
 
Advance Java
Advance JavaAdvance Java
Advance Java
Vidyacenter
 

What's hot (20)

Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
 
Knowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletKnowledge Sharing : Java Servlet
Knowledge Sharing : Java Servlet
 
Jsp servlets
Jsp servletsJsp servlets
Jsp servlets
 
Advanced Java
Advanced JavaAdvanced Java
Advanced Java
 
JDBC
JDBCJDBC
JDBC
 
Java Servlet
Java Servlet Java Servlet
Java Servlet
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packages
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technology
 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet Solution
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Java servlets
Java servletsJava servlets
Java servlets
 
Servlets
ServletsServlets
Servlets
 
JDBC
JDBCJDBC
JDBC
 
Java EE 01-Servlets and Containers
Java EE 01-Servlets and ContainersJava EE 01-Servlets and Containers
Java EE 01-Servlets and Containers
 
Advance Java
Advance JavaAdvance Java
Advance Java
 

Similar to JDBC in Servlets

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
Fulvio Corno
 
Jdbc
JdbcJdbc
Jdbc Java Programming
Jdbc Java ProgrammingJdbc Java Programming
Jdbc Java Programming
chhaichivon
 
Core Java Programming Language (JSE) : Chapter XIII - JDBC
Core Java Programming Language (JSE) : Chapter XIII -  JDBCCore Java Programming Language (JSE) : Chapter XIII -  JDBC
Core Java Programming Language (JSE) : Chapter XIII - JDBC
WebStackAcademy
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
Arati Gadgil
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)Maher Abdo
 
Jdbc sasidhar
Jdbc  sasidharJdbc  sasidhar
Jdbc sasidhar
Sasidhar Kothuru
 
Database connect
Database connectDatabase connect
Database connect
Yoga Raja
 
JDBC (2).ppt
JDBC (2).pptJDBC (2).ppt
JDBC (2).ppt
manvibaunthiyal1
 
Sqlapi0.1
Sqlapi0.1Sqlapi0.1
Sqlapi0.1
jitendral
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
Atul Saurabh
 

Similar to JDBC in Servlets (20)

Jdbc
JdbcJdbc
Jdbc
 
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
 
Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
 
JDBC programming
JDBC programmingJDBC programming
JDBC programming
 
Java JDBC
Java JDBCJava JDBC
Java JDBC
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC
JDBCJDBC
JDBC
 
Jdbc Java Programming
Jdbc Java ProgrammingJdbc Java Programming
Jdbc Java Programming
 
Lecture17
Lecture17Lecture17
Lecture17
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
Core Java Programming Language (JSE) : Chapter XIII - JDBC
Core Java Programming Language (JSE) : Chapter XIII -  JDBCCore Java Programming Language (JSE) : Chapter XIII -  JDBC
Core Java Programming Language (JSE) : Chapter XIII - JDBC
 
Jdbc
JdbcJdbc
Jdbc
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
 
Jdbc sasidhar
Jdbc  sasidharJdbc  sasidhar
Jdbc sasidhar
 
Database connect
Database connectDatabase connect
Database connect
 
JDBC (2).ppt
JDBC (2).pptJDBC (2).ppt
JDBC (2).ppt
 
Sqlapi0.1
Sqlapi0.1Sqlapi0.1
Sqlapi0.1
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 

More from Eleonora Ciceri

DDD - 5 - Domain Driven Design_ Repositories.pdf
DDD - 5 - Domain Driven Design_ Repositories.pdfDDD - 5 - Domain Driven Design_ Repositories.pdf
DDD - 5 - Domain Driven Design_ Repositories.pdf
Eleonora Ciceri
 
DDD - 4 - Domain Driven Design_ Architectural patterns.pdf
DDD - 4 - Domain Driven Design_ Architectural patterns.pdfDDD - 4 - Domain Driven Design_ Architectural patterns.pdf
DDD - 4 - Domain Driven Design_ Architectural patterns.pdf
Eleonora Ciceri
 
DDD - 3 - Domain Driven Design: Event sourcing.pdf
DDD - 3 - Domain Driven Design: Event sourcing.pdfDDD - 3 - Domain Driven Design: Event sourcing.pdf
DDD - 3 - Domain Driven Design: Event sourcing.pdf
Eleonora Ciceri
 
DDD - 2 - Domain Driven Design: Tactical design.pdf
DDD - 2 - Domain Driven Design: Tactical design.pdfDDD - 2 - Domain Driven Design: Tactical design.pdf
DDD - 2 - Domain Driven Design: Tactical design.pdf
Eleonora Ciceri
 
DDD - 1 - A gentle introduction to Domain Driven Design.pdf
DDD - 1 - A gentle introduction to Domain Driven Design.pdfDDD - 1 - A gentle introduction to Domain Driven Design.pdf
DDD - 1 - A gentle introduction to Domain Driven Design.pdf
Eleonora Ciceri
 
Artificial Intelligence: an introduction.pdf
Artificial Intelligence: an introduction.pdfArtificial Intelligence: an introduction.pdf
Artificial Intelligence: an introduction.pdf
Eleonora Ciceri
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Eleonora Ciceri
 
Trees
TreesTrees
Linked lists - Exercises
Linked lists - ExercisesLinked lists - Exercises
Linked lists - Exercises
Eleonora Ciceri
 
Doubly Linked Lists
Doubly Linked ListsDoubly Linked Lists
Doubly Linked Lists
Eleonora Ciceri
 
Linked lists
Linked listsLinked lists
Linked lists
Eleonora Ciceri
 
Dynamic content generation
Dynamic content generationDynamic content generation
Dynamic content generationEleonora Ciceri
 
Multimedia Information Retrieval and User Behavior
Multimedia Information Retrieval and User BehaviorMultimedia Information Retrieval and User Behavior
Multimedia Information Retrieval and User BehaviorEleonora Ciceri
 
The CrowdSearch framework
The CrowdSearch frameworkThe CrowdSearch framework
The CrowdSearch frameworkEleonora Ciceri
 

More from Eleonora Ciceri (18)

DDD - 5 - Domain Driven Design_ Repositories.pdf
DDD - 5 - Domain Driven Design_ Repositories.pdfDDD - 5 - Domain Driven Design_ Repositories.pdf
DDD - 5 - Domain Driven Design_ Repositories.pdf
 
DDD - 4 - Domain Driven Design_ Architectural patterns.pdf
DDD - 4 - Domain Driven Design_ Architectural patterns.pdfDDD - 4 - Domain Driven Design_ Architectural patterns.pdf
DDD - 4 - Domain Driven Design_ Architectural patterns.pdf
 
DDD - 3 - Domain Driven Design: Event sourcing.pdf
DDD - 3 - Domain Driven Design: Event sourcing.pdfDDD - 3 - Domain Driven Design: Event sourcing.pdf
DDD - 3 - Domain Driven Design: Event sourcing.pdf
 
DDD - 2 - Domain Driven Design: Tactical design.pdf
DDD - 2 - Domain Driven Design: Tactical design.pdfDDD - 2 - Domain Driven Design: Tactical design.pdf
DDD - 2 - Domain Driven Design: Tactical design.pdf
 
DDD - 1 - A gentle introduction to Domain Driven Design.pdf
DDD - 1 - A gentle introduction to Domain Driven Design.pdfDDD - 1 - A gentle introduction to Domain Driven Design.pdf
DDD - 1 - A gentle introduction to Domain Driven Design.pdf
 
Artificial Intelligence: an introduction.pdf
Artificial Intelligence: an introduction.pdfArtificial Intelligence: an introduction.pdf
Artificial Intelligence: an introduction.pdf
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Trees
TreesTrees
Trees
 
Linked lists - Exercises
Linked lists - ExercisesLinked lists - Exercises
Linked lists - Exercises
 
Doubly Linked Lists
Doubly Linked ListsDoubly Linked Lists
Doubly Linked Lists
 
Linked lists
Linked listsLinked lists
Linked lists
 
AJAX - An introduction
AJAX - An introductionAJAX - An introduction
AJAX - An introduction
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Client side scripting
Client side scriptingClient side scripting
Client side scripting
 
HTML5 - An introduction
HTML5 - An introductionHTML5 - An introduction
HTML5 - An introduction
 
Dynamic content generation
Dynamic content generationDynamic content generation
Dynamic content generation
 
Multimedia Information Retrieval and User Behavior
Multimedia Information Retrieval and User BehaviorMultimedia Information Retrieval and User Behavior
Multimedia Information Retrieval and User Behavior
 
The CrowdSearch framework
The CrowdSearch frameworkThe CrowdSearch framework
The CrowdSearch framework
 

Recently uploaded

From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 

Recently uploaded (20)

From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 

JDBC in Servlets

  • 1. Java Servlets JDBC and database connectivity Eleonora Ciceri ciceri@elet.polimi.it
  • 2. Persistent data ¤  When data have to be stored persistently, i.e., so as to restore them when needed, a database can be used ¤  A database is a structured collection of data, that are typically organized to model relevant aspects of reality, e.g., availability of rooms in a hotel ¤  A database management system (DBMS) is a set of programs that enables you to store, modify and extract information from a database
  • 3. Relational databases ¤  A Relational Database Management System (RDMBS) organizes data into tables ¤  Each table is organized into rows and columns ¤  Particular rows and columns in a table can be related to one or more rows and columns in another table ¤  Data can be read, updated, appended and deleted using the SQL language ¤  SQL: Structured Query Language, application-specific declarative language used for handling a relational database
  • 4. Database connection flow ¤  Database interaction flow ¤  Connect to the database ¤  Send a SQL statement ¤  Read the results ¤  Close connection Get connection SQL query Query result
  • 5. Interfaces ¤  Web applications can connect to databases, but each of them has its own interface that can be used in order to manipulate data ¤  ODBC ¤  Open Database Connectivity. Interface written in C ¤  Problem: programs written in C are not as portable as the ones written in Java ¤  JDBC: ¤  Java Database Connectivity. Interface written in Java ¤  It can be seen as a porting of ODBC in Java
  • 6. JDBC ¤  JDBC is a SQL-level API that allows to execute SQL statements and retrieve the results Application The driver converts JDBC method calls into ODBC function calls.
  • 7. Package java.sql (1) ¤  java.sql.Connection ¤  Connection (session) with a specific database ¤  SQL statements are executed and results are returned within the context of a connection ¤  java.sql.Statement ¤  Used for executing a static SQL statement and returning the results it produces ¤  Only one ResultSet object per statement can be opened at the same time. If two ResultSet are read at the same time, they have been generated by different Statement objects ¤  All execution methods in the Statement interface implicitly close a statement's current ResultSet object if an opened one exists
  • 8. Package java.sql (2) ¤  java.sql.ResultSet ¤  A table of data representing a database result set, which is usually generated by executing a statement that queries the database ¤  java.sql.DriverManager ¤  The basic service for managing a set of JDBC drivers ¤  When initialized, it will attempt to load the driver classes referenced in the "jdbc.drivers" system property ¤  When the method getConnection is called, it will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly
  • 9. Example: DBPhoneLookup (1) import java.io.*;! import java.sql.*;! ! import javax.servlet.*;! import javax.servlet.http.*;! ! public class DBPhoneLookup extends HttpServlet {! ! public void doGet(HttpServletRequest request, HttpServletResponse response)! ! !throws ServletException, IOException {! Connection connection = null;! ! !! response.setContentType("text/plain");! PrintWriter out = response.getWriter();! ! !! Package for JDBC + SQL
  • 10. Example: DBPhoneLookup (2) try {! Class.forName("com.mysql.jdbc.Driver");! ! connection = DriverManager.getConnection! ("jdbc:mysql://localhost:3306/test", "root", "");! ! Statement statement = connection.createStatement();! ResultSet resultSet = statement.executeQuery("SELECT * ! FROM employees");! ! ! !! out.println("Phone numbers:");! while (resultSet.next())! out.println("- " + resultSet.getString("name") + ": " + resultSet.getString("phone"));! }! Driver used for connecting to a MySQL database Retrieve a connection Database address Username Password SQL query Navigate result set Retrieve an attribute from the current tuple
  • 11. Example: DBPhoneLookup (3) catch (ClassNotFoundException e) {! out.println("Couldn't load database driver: " + e.getMessage());! }! ! catch (SQLException e) {! out.println("SQLexception caught: " + e.getMessage());! }! ! !! finally {! if (connection != null) {! try {! connection.close();! }! catch (SQLException ignored) {! }! }! }! }! } Always close the connection!!
  • 12. More syntax ¤  When the attributes names and types are unknown, ResultSetMetadata gives us a way of reading them ¤  resultSet.getMetadata(): reads the result set metadata ¤  metadata.getColumnCount(): returns the number of columns in the result set ¤  metadata.getColumnLabel(i): returns the name of the i-th column in the result set ¤  Result set data retrieval ¤  resultSet.getObject(): extract a non-String value from the result set ¤  resultSet.getString(): extract a String value from the result set
  • 13. Modify data ¤  Execute a generic query: ¤  boolean state = statement.execute(query); ¤  When used for updating: the state is false since a result set is not retrieved (the updated tuples count is retrieved) ¤  Execute an update: ¤  int count = statement.executeUpdate(updateQuery); ¤  Example: “DELETE FROM products WHERE id = 7” ¤  Retrieve results: ¤  resultSet = statement.getResultSet(); ¤  count = statement.getUpdateCount();
  • 14. Prepared statements ¤  A PreparedStatement is a Statement objects that is precompiled by the database for faster execution ¤  Example: PreparedStatement statement = connection.prepareStatement(“INSERT INTO ORDERS(ID, CLIENTID, TOTAL) VALUES (?,?,?)”); ¤  The placeholders are substituted later: statement.clearParameters(); statement.setInt(1,2); statement.setInt(2,4); statement.setDouble(3, 53.43); statement.executeUpdate();
  • 15. Reuse database connections ¤  Since creating and deleting database connections is very expensive, a good idea is to reuse them ¤  How? ¤  Create them in the init() method ¤  Another “good practice”? ¤  Store the database connection parameter in the context. Pro: sharing across all servlets, not written in the code
  • 16. Reuse database connections (2) public void init(ServletConfig config) throws ServletException {! try {! Class.forName("com.mysql.jdbc.Driver");! ! ServletContext context = config.getServletContext();! String url = context.getInitParameter("dbUrl");! String user = context.getInitParameter("dbUser");! String password = context.getInitParameter("dbPassword");! connection = DriverManager.getConnection(url, user, password); ! !! }! catch (ClassNotFoundException e) {! ! throw new UnavailableException("Couldn't load database driver");! }! catch (SQLException e) {! ! throw new UnavailableException("Couldn't get db connection");! } ! ! } Retrieve parameters from the context Examples: DBPhoneLookupReuse + HtmlSQLResult
  • 17. Transactions ¤  A transaction comprises a unit of work performed within a DBMS against a database ¤  Example: a series of SQL queries for updating data in a database when a user performs an order ¤  A database transaction must be: ¤  Atomic: all occur, or nothing occurs ¤  Consistent: it does not violate any integrity constraint during the execution ¤  Isolated: defines how and when the changes made by the transaction become visible to other operations ¤  Durable: transactions that have committed will survive permanently
  • 18. Transactions vs. JDBC ¤  Transactions are managed by using the Connection object ¤  Use the transactions: ¤  connection.setAutoCommit(false); ¤  Commit the result: ¤  connection.commit(); ¤  Rollback when an error occurs: ¤  connection.rollback();
  • 19. Transactions vs. JDBC: example synchronized (connection) {! connection.setAutoCommit(false);! ! PreparedStatement statement = connection.prepareStatement("INSERT INTO employees (NAME, PHONE, DepID) VALUES (?, ?, ?)");! statement.setString(1, (String) request.getParameter("name"));! statement.setString(2, (String) request.getParameter("phone"));! statement.setInt(3, 2);! statement.executeUpdate();! ! Statement statement2 = connection.createStatement();! statement2.executeUpdate("UPDATE departments SET numemployees = (numemployees + 1) WHERE id = 2"); ! ! ! } ! ! ! ! ! ! connection.commit(); ! Activate transactions Execute the first query Execute the second query rollback(): called whenever an Exception is captured Examples: DBPhoneTransaction
  • 20. CallableStatement (1) ¤  The interface used to execute SQL stored procedures ¤  The provided syntax allows stored procedures to be called in a standard way for all RDBMSs. ¤  Call: connection.prepareCall(java.lang.String) ¤  The syntax requires one result parameter (optional, indicated as OUT) and several IN/OUT parameters. Parameters are referred using numbers, starting from 1 ¤  {?= call <procedure-name>[<arg1>,<arg2>, ...]} ¤  {call <procedure-name>[<arg1>,<arg2>, ...]}
  • 21. CallableStatement (2) ¤  Handling parameters: ¤  IN: set using the set methods of PreparedStatement ¤  OUT: set the type before the call, retrieve the values after the execution using the get methods ¤  Result: multiple ResultSet objects are handled using operations inherited from Statement
  • 22. javax.sql.* ¤  The package provides the API for server side data source access and processing ¤  It integrates java.sql.* ¤  The package provides for the following: ¤  The DataSource interface as an alternative to the DriverManager for establishing a connection with a data source ¤  Connection pooling ¤  Distributed transaction
  • 23. Connection pooling ¤  This procedure improves performance, avoiding to create new connections ¤  A connection is used and reused ¤  The number of new connections that need to be created is cut down ¤  Connection pooling is transparent and done automatically in the middle tier of a J2EE configuration
  • 24. Connection pooling: example ¤  init() ServletContext context = config.getServletContext(); String loginUser = context.getInitParameter("dbUser"); String loginPasswd = context.getInitParameter ("dbPassword"); String driver = context.getInitParameter("dbDriver"); String loginUrl = context.getInitParameter("dbUrl"); dataSource = setupDataSource(driver, loginUrl, loginUser, loginPasswd); ¤  doGet() connection = dataSource.getConnection(); Examples: DBPhonePool+ DBPhoneSingle
  • 26. References ¤  Java Servlet Programming, Jason Hunter and William Crawford, O’Reilly