Server-Side Web Programming
Server-Side Web Programming
Server-Side Web Programming
Web Programming
Lecture 12:
Server-side Databases
and Drivers
Databases and E-Commerce
• Long term information stored in databases
– Queries used to produce lists of products
• Never hardwired in!
Generated by
querying for all items
in Books table
web container
control
servlet database
JSP
Queries to database to display
information to user
-- Query for all items in stock
-- Query to get order status, etc.
Database Servers
• Access to database controlled by database server
– Constantly running (like web container)
– Programs communicate with it
– Server runs database queries and updates for databases it controls
– Server handles security of database
• Most are password-controlled
– Examples: Database server
• MySQL (free)
• Oracle control
• MS Server servlet database
• Not Access!
JSP
Database Servers
• Programs create statement objects inside server
• Server executes them on the database
Database server
ResultSet object
productCode title price
productCode title price
productCode title price
MySQL
• Free database server
• See page 715 for instructions
– Go to www.mysql.com
– Follow downloads tab
– Go to MySQL Community
Server
– Get Windows version
– Pick a mirror
• You can skip registration
– Download and run
Installing MySQL
• Run the Service Instance Configuration when prompted to do so
• Should accept default settings
– Check “Include Bin Directory”
– Choose “sesame” for the root password
SQL and Databases
Commands to database server use SQL (structured query language)
Common examples:
• Query for all records matching some condition:
select field from table where condition
Example:
select * from books where price < 10
Example:
delete from books where productCode = ‘0004’
SQL and Databases
• Set new field value for all records matching some condition:
update table set field = value WHERE condition
Example:
update books set price = 9.95 where productCode = ‘0004’
– In javax.sql.*
package
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
web container
database server
control database
servlet driver
DBMS database
JSP JDBC
MySQL Database Driver
• From downloads page at mysql.com
• Go to MySQL Connector/J
• Download and save
MySQL Database Driver
• Extract the mysql-connector-java-5.1.6-bin.jar file
• Copy to jre\lib\ext directory of your Java
– Same as you did with JSP/servlet libraries
The MySQL Interface
• Command line interface to manipulate databases MySQL controls
– Available from PROGRAMS menu
– Will need to give password
web container
database server
control
servlet Model database
classes driver database
JDBC DBMS
JSP
Disaster Recovery
• Database server should be behind firewall to protect crucial data
– May be on separate machine from web server!
• Database access may fail
– Connection lost to database server, database server shuts down, etc.
• Need plan to back up any crucial data entered by user
– Store to file if server not available
– Read file contents into database later
web container
database server
control
servlet Model database
classes driver database
JDBC DBMS
JSP
Backup file