UCSCBaskin School of Engineering  
General Information Events, News & Organizations Degrees & Departments Research Classes Admissions & Advising People & Jobs Administration

Lab Assignment 3 (due May 18 <extended> )

General Description

In this last lab assignment, you will be asked to write simple command line Java programs to query Postgresql DB according to the specified parameters. These programs are kept very simple--in real world, you will find more complicated code in Java Applet, Java Servlet, Java Beans, etc. And you will find more advanced usages like stored procedure, transaction, connection pool, etc. But the basic idea remains the same: to get required data from the DB, correctly and efficiently.

Assignments

0. Preparation:

  • DB setup: we are using the same DB schema and data from lab2, so if your DB is different, please run those three script files from lab2.
  • Java platform setup: J2SE 1.4.2_01 has already been installed on db.ic.ucsc.edu and you can directly type javac, java and other SDK commands from the console. Type java to make sure your SDK works. For security reasons, the Postgresql server is configured to accept local connections from db.ic.ucsc.edu, so please log on db.ic.ucsc.edu and do your Java development.
  • CLASSPATH setup: Postgresql driver is located at /home/share/postgresql/java/postgresql.jar. To make your life easier, you should add Postgresql driver to your classpath. You also need to add current directory (.) for your Java program to the classpath.

For csh, sh, add this line to your .cshrc file: setenv CLASSPATH /home/share/postgresql/java/postgresql.jar:.

For bash, add this line to your .bash_profile: export CLASSPATH=/home/share/postgresql/java/postgresql.jar:.

 

1. A Sample Program (Sample.java) is provided here to give you some flavor of JDBC programming. The code select every tuple in the Bar table. You can try it out. Please make sure you properly change line 19.

conn = DriverManager.getConnection("jdbc:postgresql://db.ic.ucsc.edu/username", "username", "username1234" ); //Making the Connection

jdbc:postgresql://db.ic.ucsc.edu/username is the URL for your DB, replace the username with your CATS username, e.g. gaurav.

username is the DB username, replace it with your CATS username, e.g. gaurav.

username1234 is the password for your DB, replace it with your CATS username+1234, e.g. gaurav1234.

Use javac to compile the file:

%db> javac Sample.java

Use java to run the comipled class file:

%db> java Sample

OR (if you did not set CLASSPATH to include postgresql.jar)

%db> java -classpath /home/share/postgresql/java/postgresql.jar:. Sample

Previous TA Gaurav Vijayvargiya has written a short tutorial about this Sample file with explanation of possible errors, you can find it here.

 

2. Write four Java programs, one for each of the following questions.

Question 1. Table Creation. Create a new Beer table in the DB. The schema is described as follows:

Beer(Brand, Supplier_Price, Supplier_ID)

Brand: VARCHAR(20) PRIMARY KEY

Supplier_Price: NUMERIC(3,2)

Supplier_ID: INTEGER

Question 2 Query Execution. Find names, age and occupations for drinkers whose age is below 21. One possible output of your query results can be like this:

Name: John | Age: 18 | Occupation: Student

Name: Jane | Age: 20 | Occupation: Student

Quersion 3 Tuple Insertion. Insert a new tuple into Drinker table with the value ('Mary', 25, 'Graphics Designer', 75080.53).

Question 4 Tuple Update. Update Jack's salary because he got a raise of 10000, so his new salary is 96098.15 now.

 

3.Optional assignment (not for submission). For those who have extra time, you can try to play with EMS Postgresql Manager, a GUI client for Postgresql. Most of the commercial DBMS provides similar GUI-based management tools, so you might find it useful to get some flavor of such tools. Here are the basic instructions:

1) Download and install EMS Postgresql Manager from its download site. Both Win32 and Linux 30-day trial versions are available. I tried Professional for Windows(full installation package) v2.4.0.1, just download, unzip and run the setup file. Linux version should be similar.

2) Register your DB. Open EMS Postgresql Manager and select Database->Register Database... from menu or press shift+alt+R. Because db.ic.ucsc.edu requires ssh connection, we need to configure SSH tunneling. Figure 1 shows how to configure SSH tunneling on SSH tab. Figure 2 shows how to configure the rest parts on General tab. When you are done, you can press the "Test Connect" button and see if the connection is successfully set up.

3) When you finish the basic configuration, you can connect to the DB and start viewing and managing the DB. Refer to the manual and help file on specific topics.

Submission Summary (please read carefully about items to submit)

Items to submit Description

CreateBeerTbl.java and CreateBeerTbl.log

CreateBeerTbl.java creates the new Beer table;

CreateBeerTbl.log records the session when you execute \d Beer in psql after the Beer table is created.

FindIllegalDrinker.java FindIllegalDrinker.java shows the name, age, and occupation for drinkers whose age is below 21.
InsertTuple.java and InsertTuple.log

InsertTuple.java inserts the new tuple;

InsertTuple.log records the session when you execute SELECT * from Drinker; in psql after the tuple is inserted.

UpdateSalary.java and UpdateSalary.log

UpdateSalary.java updates Jack's salary;

UpdateSalary.log records the session when you execute SELECT * from Drinker; in psql after the tuple is updated.

You should use submit program to submit the assignments. If you dont know how to use submit, please read "how to use submit" for details. Sample submit commands for this lab assignment:

submit cmps180-wt.s04 lab3 CreateBeerTbl.java //to submit CreateBeerTbl.java

submit cmps180-wt.s04 lab3 CreateBeerTbl.java CreateBeerTbl.log //to submit two files together

peek cmps180-wt.s04 lab3 CreateBeerTbl.java //to examine the CreateBeerTbl.java file you submitted

submit cmps180-wt.s04 lab3-late CreateBeerTbl.java //late submissions go to lab3-late directory

Useful Links

 

General info · News · Events · Degree Programs · Research · Classes · Admissions · Advising · People · Jobs · Administration
SOE Webmail · SOE SSH · SOE Wiki · Search · Sitemap · Contact us · Driving directions · Privacy · UCSC
© Baskin School of Engineering, University of California, Santa Cruz
1156 High St., Santa Cruz, CA 95064 · (831) 459-2158 · webmaster@soe.ucsc.edu