This will be the last regular homework assignment for this course. Aside from this homework, the remaining work in the course consists of the final project and the research report for Homework 5.
This homework is about using databases to store data for web applications. The first part of the lab consists of some SQL exercises using the MySQL console interface. You should fill out the answers to the exercises on this handout, and turn in the handout as part of your homework.
The second part of the homework asks you to work on a small web application that uses a database. The parts of the application that use the JDBC database API are missing. You need to write those parts. The application consists of a single JSP page, index.jsp. (It is an example of a single-page application, which is sort-of the polar opposite of the MVC pattern.) To turn in your work for this part of the homework, you should copy your completed index.jsp file to your homework directory inside /classes/s09/cs271/homework.
The file /classes/s09/cs271/pollsdb.init contains the same table-creation commands as the file, pollsdb_create_tables.txt, that was handed out in class, along with commands for inserting a lot of data into the tables.
Start up the MySQL console, log onto the MySQL RDBMS server on mathb.hws.edu, and give the use command to select your database, as described in the previous handout. Enter the command
source /classes/s09/cs271/pollsdb.init;
to execute the database initialization file. The command
show tables;
should then list the five tables that have been created. Note: In the MySQL console, you should end every statement with a semicolon. You can type in statements that extend over more than one line. The semicolon marks the end of the statement.
Note: You can ask MySQL to tell you the structure of a table with a command such as
describe answers;
which lists information about the table named answers. This can be useful for exploring a database, or if you forget the name of a column.
For the rest of this section, you should use the MySQL console to answer the questions, or to check your answer when the question asks for an SQL statement, and enter your the answers in the spaces provided.
Ex. 1. What values are output by the following command?
SELECT username FROM rolls WHERE roll="creator"; Answer:
Ex. 2. What value is output by the following command, and why?
SELECT count(*) FROM rolls WHERE roll="creator"; Answer:
Ex. 3. You are listed as a user in the database, under your regular HWS username. What password is listed for you in the database? What SQL command did you use to determine this?
Answer:
Ex. 4. How many votes were cast in poll number 3 (the total for all possible answers)? What SQL command did you use to determine this?
Answer:
Ex. 5. Describe the table produced by the following command, and explain how the command works. As part of your answer, explain the statement begins select polls.id rather than simply select id
SELECT polls.id, username, question FROM users, polls WHERE users.id = polls.creator_id; Answer:
Ex. 6. According the data in the database, how many votes did you cast altogether? What SQL statement or statements did you use to determine the answer?
Answer:
Ex. 7. Describe the table produced by the following SQL command, and explain the purpose of the command:
SELECT answer, count(*) FROM answers, responses WHERE answers.poll_id = responses.poll_id AND responses.answer_number = answers.answer_number AND answers.poll_id = 1 GROUP BY answers.answer_number; Answer:
Ex. 8 Give the two SQL commands that would be used to add a new user to the database, with username "chris", password "frazzled", and role "member". (Try it!)
Answer:
Ex. 9 Give the SQL command that would be used to change your password in the database to "foo". (Try it!)
Answer:
Ex. 10 Give the two SQL commands that would be used to delete from the database the answer number 6 for poll number 1, along with all the votes for that option. (Try it!)
Answer:
Ex. 11 The web application in the second part of this lab requires three database tables. Here are descriptions of the tables. Translate these descriptions into SQL CREATE statements, and write the CREATE statements in the space at the right. Use your statements to create the tables, so that you will be set to do Part 2.
Table Name: userlist | |
Column Name | Type of data |
id | integer |
user | string |
password | string |
The primary key for userlist is id;
id is an auto-incremented column.
Table Name: movies | |
Column Name | Type of data |
id | integer |
name | string |
The primary key for movies is id;
id is an auto-incremented column.
There is an index on the name column.
Table Name: ratings | |
Column Name | Type of data |
id | integer |
user_id | integer |
movie_id | integer |
rating | integer |
The primary key for movies is id;
id is an auto-incremented column.
there are indexes on user_id and movie_id.
Ex. 12 Add at least three users to the userlist table that you created in the previous exercise. What SQL statement or statements did you use to add the users?
Answer:
Ex. 13 Make sure that your name is on this handout before you turn it in!
The web application that you will work on for Part 2 uses a database to store a list of movies and user ratings for the movies. Only registered users can access the application. (Users can only be added by directly manipulating the database; no web interface is provided for adding users.)
The application is defined by one long file. I have written the application, then removed some of the code that uses the database. Your job is to restore the deleted functionality. You can find the sections that you have to write by searching for the string "*****".
The file will be available no later than Monday, March 30. It will be stored on the file server as /classes/s09/cs271/assg8/index.jsp.
You should start a new NetBeans Web project. Replace the contents of index.jsp in the project with the contents of the file /classes/s09/cs271/assg8/index.jsp. You need to start by creating a connection to your database on mathb.hws.edu, near the top of the file. Once that is done, you will be able to run the project, and you can start implementing the missing functionality -- starting with logging on.
Both parts of this homework are due on Friday, April 10.
(But an extension might be given.)