Lab 6: Databases, SQL, JDBC
Goals
After completing this lab, you should be able to
- use the JDBC API effectively
- load the driver
- make connections to the database
- execute queries, updates to the database in SQL using Statements and PreparedStatements
- iterate through a ResultSet and display it in a meaningful way
PostgreSQL
For these exercises and for development, we'll use the PostgreSQL database management system (DBMS) set up on hopper. For more information about using PostgreSQL, see the PostgreSQL page on the Wiki.
Using PostgreSQL on hopper
- In a terminal, ssh into hopper, using the command
ssh hopper
- Run the PostgreSQL client and connect to the cs335
database:
psql cs335
Here is a dump from the (original) cs335 database so that you can see the definitions of the tables and initial data values. Focus on the
CREATE TABLE
statements.Alternatively, you can use the
\d tablename
command to view a table's columns and types,
e.g.,\d Users
- List all the tables in the database using the
command
\dt
- Write and execute the SQL statement to select all the columns and rows from the Users table
- Write and execute the SQL statement to select the id from the Users table where the username='username' and the password=md5('passw0rd').
- Modify the previous command so that the password is 'password' and execute the command.
Alternatively, you can try using pgadmin3
to look at the databse.
JDBC (45 pts)
For the rest of the lab, you don't need to be logged into hopper. However, you may find it useful to try out your SQL statements on the command-line before putting them into JDBC code.
Getting the PosgreSQL JDBC Driver
Copy postgresql.jar
from /csdept/local/courses/cs335/shared
into your cs335
directory.
JDBC Practice
Create a new Dynamic Web Project for this lab. Create a new Java
class (not a servlet)
called JDBCPractice
that has a main
method.
What is an appropropriate package for this class?
- Copy postgresql.jar into
your
WEB-INF/lib
directory. Refresh to see the jar file. - Follow the instructions from the lecture about how you connect
to a database using JDBC and apply those in the
main
method. What packages do you need? What steps do you need to take? You may want to look at the examples.Run this class as a Java application.
Note: you won't be able to do this from a non-cs.wlu.edu computer. The DB isn't set up to handle connections from other users.
- Execute the statement that finds out the names of the groups
that a given user belongs to. (You decide which user.) Iterate
through the
ResultSet
and display the results in a nice way.Test your program.
- Create and execute a
PreparedStatement
that inserts a new group into theGroups
table, providing the group's name and description as parameters. - Create and execute
PreparedStatement
that updates a user's group membership to include your newly created group. (A user of your choice.) - Create and execute
PreparedStatement
that finds out the names of the groups that a user belongs to, providing the username as a parameter. Display the results of the query. - Don't forget to close the DB connection when you're done with it.
- Run and test your program. (During this process, you'll add rows to the table that the rest of the class can also see.)
LoginServlet (55 pts)
- Copy your
LoginServlet
from Lab5 into an appropriate package in this lab/project. (You may need to updateweb.xml
, depending on how you do this step.)
Also, copy your JSPs from Lab5 into the appropriate directory in this project. - Modify your
LoginServlet
so that it checks if the username and password match an entry in the database. Note the password was given when you were writing your practice queries above. (Keep your old error messages/attributes that check if the username or password wasn't entered.)For this lab, put the JDBC setup (e.g., DriverManager, Connection) in the
init
method. You'll needConnection
to be an instance variable of your servlet so that you can use it in thedoPost
method.Close the connection in the servlet's
destroy
method. - Get the user's "real" name from the database and save it as an
attribute in the
HttpSession
object because you'll want to use the user's real name for more than just this request. - Modify the congratulations JSP so that it says "Welcome Back,
<name>!" with the name (that was stored in
the
HttpSession
) plugged in. - Modify the LoginServlet so that it creates an ArrayList of Strings of the
logged-in user's groups (sorted by name, ascending) and passes the ArrayList
as an attribute (whose attribute is most fitting for this case?) to the
Congratulations JSP to display. Practice your SQL statement in the terminal
before attempting to put it into Java code. Label the groups and display them
as an unordered list.
In the end, the response may look something like:
Welcome Back, Test McUser
Your Group Membership
- Coin Collecting
- Gardening
- Test your code several times.
Turnin
Export the project as a WAR file and save in your turnin directory.
Grading (100 pts)
This lab is due tonight at midnight.
- JDBC Practice (45 pts)
- LoginServlet and Congratulations.jsp (55 pts)