First you should configure your environment for using PostgreSQL, by giving the following commands to the shell. (This assumes you are using csh or tcsh as your shell.)
set path=(/usr/local/pgsql/bin $path) setenv CLASSPATH /usr/local/pgsql/java/lib/postgresql.jar:.(To have these commands done each time you start a csh or tcsh, you can put them in the .cshrc file.) Now you should be able to run PostgreSQL commands and Java programs that make JDBC access to PostgreSQL.
In particular, start by replicating the example I gave. Create a database (named with your username, which is the default) on kilpinen, and connect to it using psql, the interactive SQL interface. This is done with the two commands below, each of which will prompt you for for your username and PostgreSQL password. Note that your password should be your PostgreSQL password, not your usual login password. The first command creates the database, which you will only have to do once. The second command connects to it, which you will need to do any time you want to interact with it using SQL.
createdb -h kilpinen -u psql -h kilpinen -uNow use SQL commands to create the movies, persons, and acts_in tables, put the data into the tables, and do a couple sample queries to make sure it works. To put the data in, you can use commands like the following:
\copy movies from /Net/solen/home/m/a/max/MC39/movies
Having reproduced the basic movie database functionality I demonstrated, you are now to extend it so as to track the specific tapes that our movie store chain owns, which are copies of the movies listed in the movies table. Those tapes may be owned by either of our two stores (St. Peter and Mankato, with the possibility of more stores later), and may be checked out to any of our customers or checked out to no customer, i.e., in the store. To track this information, add two tables:
Now, here is the crux of the assignment: formulate a SQL query to find what movies store X has one or more copies of on the shelf that include actor Y. For example, you might want to know what movies with Claude Rains in them are currently available (not all rented out) at St. Peter.
Once you have this working in psql, you can try programming it in Java using JDBC. I am providing a skeletal Java client program, which provides the graphical user interface (primitive, admittedly) but not the JDBC calls to actually get the data. (Instead, it has two fixed sample movies that it always claims are the answer, just to show how the output should be done once you retrieve the real movies.) The code is linked from the web version of this lab handout. You should save it in a file called FindActor.java, because the class is named FindActor. To compile and run it, you would use the commands
javac FindActor.java java FindActorTry it first as is, then add the JDBC calls to access your PostgreSQL database for the answers. (You should be able to confine your attention to the FindActorController class. In addition to modifying its getMovies method, you may want to add one or more instance variables and a constructor to initialize them.) Your code should be based on the SQL query you directly used in the prior portion of the lab. Getting the query to work directly in psql will get you most of the credit for the lab, but for full credit you need to get it to work from Java as well. Be sure your code works for Peter O'Toole, despite the apostrophe in his name. The easy way to do that is by using a PreparedStatement.
If you are looking for a little extra to do, you can rectify one of the FindActor program's shortcomings. Namely, it has hard-coded into it the names of the two stores (St. Peter and Mankato). This obviously will be a problem as the chain expands - we'll need to upgrade the client software and redistribute it to all the machines it is installed on. Instead, you should have FindActor query the database to find the names of the stores.
Instructor: Max Hailperin