L05 - Database
L05 - Database
Database
JSP
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
1
11/2/2019
DATABASE
̵ A database is an organized collection of data.
It is the collection of schemes, tables, queries,
reports, views and other objects.
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
2
11/2/2019
DATABASE SERVER
Updates to databases when orders placed
Queries to database to validate order
- Does item exist?
- Is it still in stock?
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
JSP
6
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
3
11/2/2019
Database server
Statement object
JSP select * from books
database
ResultSet object
productCode title price
productCode title price
productCode title price 7
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
MYSQL
̵ The MySQL software delivers a very fast, multi-
threaded, multi-user, and robust SQL (Structured
Query Language) database server.
̵ MySQL is a database system used on the web.
̵ MySQL is ideal for both small and large applications
̵ MySQL uses standard SQL
̵ MySQL compiles on a number of platforms
̵ MySQL is free to download and use
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
4
11/2/2019
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
Note
10
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
10
5
11/2/2019
11
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
11
12
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
12
6
11/2/2019
13
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
13
14
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
14
7
11/2/2019
15
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
15
16
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
16
8
11/2/2019
New Database
̵ Right click to localhost -> choose New Database
17
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
17
New Database
̵ Enter information of new database
18
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
18
9
11/2/2019
New Database
Enter Database
name
Choose charset of
database
Choose collation of
charset
19
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
19
New Database
20
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
20
10
11/2/2019
21
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
21
SQL review
̵ SQL (Structured Query Language) is a special
purpose programming language designed for
managing data held in a relational database
management system.
22
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
22
11
11/2/2019
Select
̵ Syntax:
▪ SELECT field FROM table WHERE condition;
̵ EX:
▪ SELECT ID, NAME, SALARY FROM CUSTOMERS;
23
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
23
Insert
̵ Syntax:
▪ INSERT INTO TABLE (list column) VALUES (list value);
or
▪ INSERT INTO TABLE VALUES (list value);
̵ EX:
▪ INSERT INTO CUSTOMERS (ID,NAME,AGE,
ADDRESS,SALARY) VALUES (1, 'Ramesh', 32, 'Ahmedabad',
2000.00 );
▪ INSERT INTO CUSTOMERS VALUES (2, 'Muffy', 24, 'Indore',
10000.00 );
24
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
24
12
11/2/2019
Update
̵ Syntax:
▪ UPDATE table SET [data change] WHERE [condition];
̵ Ex: UPDATE CUSTOMERS SET ADDRESS = 'Pune'
WHERE ID = 6;
25
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
25
Delete
̵ Syntax:
▪ DELETE FROM table WHERE [condition];
̵ Ex:
▪ DELETE FROM CUSTOMERS WHERE ID = 6;
26
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
26
13
11/2/2019
JDBC
̵ JDBC is a Java database connectivity technology (Java
Standard Edition platform) from Oracle Corporation.
This technology is an API for the Java programming
language that defines how a client may access
a database.
27
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
27
Database Drivers
̵ Database server does not understand JDBC commands
̵ Only understands its own DBMS protocols
▪ Each server has its own DBMS
̵ Need a database driver to perform translation
▪ Obtain from database server provider
▪ Install in Java libraries
control database
servlet driver
DBMS database
JSP JDBC
28
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
28
14
11/2/2019
29
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
29
30
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
30
15
11/2/2019
31
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
31
32
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
32
16
11/2/2019
33
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
33
JDBC Connection
❖Major objects involved:
• Connection: represents connection to a database through a server
• Statement: represents SQL statement executed on database via that
connection
• ResultSet: represents “list” of records matching a query( select query)
ResultSet object
Select * from table
34
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
34
17
11/2/2019
Create connection
̵ Load driver
▪ Class.forName(“driver name")
̵ Create Connection Object to open connection
to database
Load driver mysql
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(
"jdbc:mysql://localhost/test", "root", "");
%>
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
35
36
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
36
18
11/2/2019
<%
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/test", "root", "");
} catch (ClassNotFoundException e1) {
// NO DRiVER
} catch (SQLException e2){
// CONNECTION ERROR
}
%>
37
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
37
Insert data
̵ Create statement:
▪ Statement s=conn.createStatement();
̵ Execute query: using method executeUpdate()
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
38
19
11/2/2019
Updata data
̵ Using like insert data.
39
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
39
Select data
̵ Create statement:
▪ Statement s=conn.createStatement();
̵ Execute query: using method executeUpdate()
▪ Using ResultSet object to get data return.
40
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
40
20
11/2/2019
Reading ResultSets
̵ How to access:
▪ Only access current record.
▪ Move next record to continues read ResultSets.
̵ Syntax:
▪ Next record: using next() to move next record.
Method return a value Boolean, true if can read
next record, false if can’t read next record.
while(ResultSets.next()){
// read data here
}
41
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
41
▪ Read data:
• Read by column index:
DATATYPE value = ResultSets.getTYPE(int column_index).
• Read by column name:
DATATYPE value = ResultSets.getTYPE(int column_name).
42
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
42
21
11/2/2019
try {
String listProduct= ...
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/test", "root", "");
Statement s=conn.createStatement();
ResultSet rs=s.executeQuery("SELECT * FROM product
where listProduct = "+listProduct);
while(rs.next()){
rs.getString(1);//read by column index
rs.getString("name");//read by column name
}
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
43
44
22
11/2/2019
Problem
̵ We have a product. Now quantity of this product is
100, and 2 client modify data with a processor.
̵ Processor working with client 1:
▪ Client 1 load quantity of this product and view, quantity
of product is 100.
̵ Processor working with client 2:
▪ Client 2 load quantity of this product, too. And quantity
still is 100.
▪ Client 2 set quantity reduced 1 product. (quantity =99)
̵ Processor swap back client 1:
▪ Client 1 Client 2 set quantity reduced 1 product(➔
quantity =99)
̵ Expected results: 98
45
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
45
46
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
46
23
11/2/2019
Solution
47
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
47
Statement s;
synchronized(s){
try{
s=conn.createStatement();
ResultSet rs=s.executeQuery("SELECT quantity FROM
product where idproduct = "+id);
rs.next() ;
int quantity=rs.getInt("quantity");
quantity-=1;
s.executeUpdate("UPDATE product SET quantity = “
+quantity+" WHERE idproduct = "+id);
}catch(SQLException e){
//BAD SQL
}
}
48
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
48
24
11/2/2019
Problem
̵ We have a login from with 2 input
▪ Username
▪ Password
̵ And SQL to login:
▪ SELECT * FROM user where username =“username
value” and password = “password value”
▪ Ex: SELECT * FROM user where username =“ti”
and password = “123456”
49
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
49
50
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
50
25
11/2/2019
Solution ➔ PrepareStatement
̵ Prepared statement is a feature used to
execute the same or similar database
statements repeatedly with high efficiency.
▪ Prepare: The statement template is created by the
application and sent to the database management
system (DBMS). Certain values are left unspecified,
called parameters, placeholders or bind variables
(labelled "?" below):
• INSERT INTO PRODUCT (name, price) VALUES
(?,?)
51
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
51
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
52
26
11/2/2019
Using prepareStatement
̵ Define prepareStatement:
▪ PreparedStatement psupdate=null;
̵ Create prepateStatement:
▪ psupdate=conn.prepareStatement("SELECT
quantity FROM product where idproduct
= ?");
̵ Set value to SQL:
▪ Systax: using method setTYPE(index, value);
psupdate.setInt(1, id);
53
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
53
54
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
54
27
11/2/2019
Connection Pooling
̵ When connection requested:
▪ Get unused connection from pool
Connections currently in
web container use
JSP/servlet
Connection pool database server
JSP/servlet
database
Request for new
connection
New
JSP/servlet
Free unused connection 55
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
55
Connection Pooling
̵ When connection requested:
▪ Connection used by servlet/JSP
Connections currently in
web container use
JSP/servlet
Connection pool database server
JSP/servlet
database
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
56
28
11/2/2019
Connection Pooling
̵ When finished, JSP/servlet returns the connection back to the
pool
▪ Now free for use by another
Connections currently in
web container use
JSP/servlet
Connection pool database server
JSP/servlet
database
Return to pool
New
JSP/servlet
Free unused connection 57
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
57
Connection Pooling
̵ Unlike prepared statement, no built in Java
methods/classes
▪ Write your own
• http://java.sun.com/developer/onlin
eTraining/Programming/JDCBook/conpo
ol.html
▪ Third party classes
• dbConnectionBroker, etc.
▪ Build components directly into
web.xml/context.xml files
• Page 466 of text
• Not well supported by Tomcat
58
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
58
29
11/2/2019
Connection Pooling
̵ Usually static object
▪ Automatically constructs connections first time getConnection
called
̵ Usually provide following methods:
▪ ConnectionPool.getInstance()
▪ freeConnection()
̵ Example:
Connection connection = ConnectionPool.getInstance();
connection.freeConnection();
59
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
59
Connection Pooling
̵ Required parameters:
▪ Driver name
• "com.mysql.jdbc.Driver“
▪ Url, name, and password Necessary so connection
pool can connect to
• "jdbc:mysql://localhost/bookstore",
“root", “sesame" database
▪ Number of initial connections to create
• Usually a few hundred to a few thousand
▪ Timeout for idle connections
• Time after which idle connections are returned to pool automatically
• Important to prevent pool running out!
60
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
60
30
11/2/2019
DEMO
61
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
61
Q&A
62
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 43
62
31