You are on page 1of 6

CSCI 585 – Database Systems – Fall 2010

Professor Dennis McLeod


Assignment 3
Due: November 9, 2010 @ 02:00 PM PDT
[Electronic submission]

Java Database Connectivity (JDBC) is a standard database-independent API that could be used to
interact with virtually any database from within your application. In this assignment you will gain
practical experience in using the JDBC library to access and query a database from within a Java
program that you will write.

Preliminaries: Java
For this assignment, we will use Java Standard Edition JDK 6 Update 22. Note that we will not be
able to accommodate any compatibility issues if you use a different version.

Download & instructions: http://www.oracle.com/technetwork/java/javase/downloads/index.html


Documentation: http://download.oracle.com/javase/6/docs/
API Specification: http://download.oracle.com/javase/6/docs/api/

Resources for those who are new to Java:


The Java Tutorials: http://download.oracle.com/javase/tutorial/
Thinking in Java (free e-book): http://www.mindview.net/Books/TIJ/

You are free to use any Integrated Development Environment (IDE) such as Eclipse, NetBeans, Visual
Studio, etc. if you wish. However, you must be able to compile and execute your code from the
Windows command line (or Linux/Mac shell) environment, outside of the IDE.

Preliminaries: JDBC
To connect to your MySQL database from within your Java program, you will need to use JDBC. The
current Generally Available release of the official MySQL JDBC driver is MySQL Connector/J 5.1.13.
It can be obtained from http://www.mysql.com/downloads/connector/j/

JDBC Documentation: http://download.oracle.com/javase/6/docs/technotes/guides/jdbc/


API Specification: http://download.oracle.com/javase/6/docs/api/java/sql/package-summary.html
Tutorial: http://download.oracle.com/javase/tutorial/jdbc/basics/index.html
MySQL Connector Documentation: http://dev.mysql.com/doc/refman/5.1/en/connector-j.html

You may NOT use any other third-party libraries apart from MySQL Connector/J.

To connect to your MySQL database from within your Java program, you will need to supply it with the
necessary connection parameters. You must store these parameters in a separate configuration file to be
passed as an argument to your Java program during execution. A sample db.properties file has been
provided for your reference. The five lines of the file correspond to the host, port, database name,
username and password for the database. You will need to change the parameters according to your
individual system setup.
For grading, we will of course use a different db.properties configuration based on the setup on our
own machine. Make sure you follow the configuration file format. We will not be able to grade your
assignment if your program fails to parse our database configuration file.

Application domain description


Consider an air ticket reservation system in which we have a database backend which tracks information
about available flights and ticket reservations. A separate application frontend is used to connect to the
database, providing a user interface for looking up flight information and seat availability, and making and
updating reservations.

In this assignment, you will implement a rudimentary version of such a system.

Database Schema
The database schema is provided to you in the createdb.sql file. To create the database, run
createdb.sql from the MySQL prompt as follows:

mysql> source createdb.sql

The database schema is given in Fig. 1.

Flights
flight_no departure_time duration capacity price origin_city origin_state destination_city destination_state

Reservations
reservation_no flight_no departure_date customer_ssn num_seats

Fig. 1: Schema for cs585hw3

The system keeps track of various flights, each of which has a unique flight number, and operates
between a pair of origin and destination cities. Each flight has a fixed capacity and is scheduled to depart
at the same time every day, lasting the same estimated duration. For simplicity, we assume that there are
no separate cabins (economy class only) and that seat prices for each flight are fixed.

The system also keeps track of reservations, each of which has a unique reservation number, and is made
for some given number of seats on one of the available flights on some given departure date. We also
record the SSN of the customer who made the reservation. Note that reservation numbers are auto-
incremented.

Do not modify createdb.sql. We will use createdb.sql to create a standardized, empty database
upon which your submitted programs will be executed and graded.

Input Files
You are given samples of the following files, which contain data for the respective tables.
 flights.data
 reservations.data
These are simply plain text files, which you may open and examine using any standard text editor.
Observe that each row represents one record, and fields are comma-separated.
The data files provided to you are samples demonstrating file format only. When grading, we will test
your programs using a different set of unseen data. As such, you may wish to create additional data for
your own testing purposes.

Populating the Database


You are required to implement a Java program Populate.java that takes the names of the input data
files as command line parameters, parses the files, and populates the data contained within them into
your database via JDBC. It should be compiled like this:
$ javac –classpath .:mysql-connector-java-5.1.13-bin.jar Populate.java

and executed like this:


$ java –classpath .:mysql-connector-java-5.1.13-bin.jar ↵
Populate db.properties flights.data reservations.data

Note that every time you run this program, it should start by first removing any previously existing data
in your tables; otherwise the tables will have redundant or incorrect data.

Querying the Database


You are required to implement a Java program Hw3.java that provides the capability to run queries on
the system from the Windows command line or Linux/Mac shell environment. It should be compiled
like this:
$ javac –classpath .: mysql-connector-java-5.1.13-bin.jar Hw3.java

and executed like this:


$ java –classpath .:mysql-connector-java-5.1.13-bin.jar ↵
Hw3 db.properties <query_number> <arguments>

Depending on the query number, your program should take the arguments provided and process the
corresponding query, then terminate. Query details are as follows:

Q1. Fetch information about a flight


Given a flight number, display all information about that flight. For instance, given:
$ java –classpath .:mysql-connector-java-5.1.13-bin.jar ↵
Hw3 db.properties q1 CSC585

Your program should execute the following SQL:


SELECT * FROM flights WHERE flight_no="CSC585";

And print the results to the screen.

Q2. Check seat availability of a particular flight on a particular date


Given a flight number and date, find out how many seats are remaining on that flight on that date. For
instance, given:
$ java –classpath .:mysql-connector-java-5.1.13-bin.jar ↵
Hw3 db.properties q2 CSC585 2010-05-31
Your program should execute the following SQL:
SELECT f.capacity-COALESCE(SUM(r.num_seats),0) AS seats_avail
FROM flights f, reservations r
WHERE f.flight_no="CSC585" AND r.flight_no="CSC585"
AND r.departure_date="2010-05-31";

And print the results to the screen.

Q3. Fetch all reservations for a particular flight on a particular date


Given a flight number and date, list all reservations for that flight on that date. For instance, given:
$ java –classpath .:mysql-connector-java-5.1.13-bin.jar ↵
Hw3 db.properties q3 CSC585 2010-05-31

Your program should execute the following SQL:


SELECT * FROM reservations
WHERE flight_no="CSC585" AND departure_date="2010-05-31";

And print the results to the screen in table form.

Q4. Fetch all reservations of a customer


Given a customer’s SSN, list all reservations made by that customer. For instance, given:
$ java –classpath .:mysql-connector-java-5.1.13-bin.jar ↵
Hw3 db.properties q4 5851234567

Your program should execute the following SQL:


SELECT
r.reservation_no, f.flight_no, r.departure_date, f.departure_time,
f.duration, f.origin_city, f.origin_state, f.destination_city,
f.destination_state, r.num_seats, r.num_seats*f.price AS cost
FROM flights f, reservations r
WHERE f.flight_no=r.flight_no AND r.customer_ssn="5851234567";

And print the results to the screen in table form.

Q5. Make a reservation


Given a customer’s SSN, a flight number and date, and number of seats required, make a reservation for
that customer, on that flight, on that date, for that number of seats. For instance, given:
$ java –classpath .:mysql-connector-java-5.1.13-bin.jar ↵
Hw3 db.properties q5 5851234567 CSC585 2010-05-31 3

Your program should execute the following SQL:


INSERT INTO
reservations (flight_no, departure_date, customer_ssn, num_seats)
VALUES ("CSC585", "2010-05-31", "5851234567", 3);

And provide confirmation whether the operation succeeded or not.


Q6. Change the date of a reservation
Given a reservation number and a date, change the date of that reservation accordingly. For instance,
given:
$ java –classpath .:mysql-connector-java-5.1.13-bin.jar ↵
Hw3 db.properties q6 88349 2010-05-31

Your program should execute the following SQL:


UPDATE reservations SET departure_date="2010-05-31"
WHERE reservation_no=88349;

And provide confirmation whether the operation succeeded or not.

Notes
You are expected to incorporate sensible business logic into your programs. For example, you should
not allow new reservations on a flight if it is already full on the given date. The same applies for
reservation changes. Or, the user may try to run a query about an inexistent flight or reservation. In such
cases, you should return an informative error message.

Likewise, your programs are expected to handle database errors gracefully, and return informative
error messages before exiting rather than terminating abruptly. For instance, the connection to the
database may fail, or the database user (in db.properties) may not have the necessary permissions to
perform the required operations.

You should use all relevant JDBC features in your programs; for example, PreparedStatement.

Submission Instructions:

 This assignment is to be submitted electronically via Digital Dropbox, which can be found
under the “Tools” menu of the course page on USC DEN Blackboard at https://www.uscden.net.
 You must prepare a readme.txt containing your name, id and email address. It should also
contain a list of all the submitted files, and instructions on how to compile and run your Java
programs. Failure to provide this file or any of the required information will incur a 10 point
penalty. You may also optionally include other comments in the file.
 You should have (at least) 2 Java source files: Populate.java and Hw3.java. Failure to
adhere to these filenames will incur a 5 point penalty.
 You may optionally organize your program(s) into multiple Java classes. If you do so, make
sure to include all the necessary source files and indicate accordingly in the compilation and
execution instructions in your readme.txt.
 There is no need to include the MySQL Connector/J libraries in your submission.
 You are NOT allowed to use any other third-party libraries. Penalty: -10 points.
 Do NOT include the *.class files. We will compile your programs from your *.java files.
 Do NOT include db.properties or input files. We will use our own db.properties
settings and provide different input files for grading.
 Your code must compile and run from command line or shell environment as described in the
assignment instructions without requiring any classpath or specific file locations. You will be
penalized as follows if your programs do not compile or execute as required:
 Classpath or similar minor problems with compilation or execution: -5 points
 Hardcoded configuration, input file location, or other similar issues requiring us to
make edits to your code: -10 points
 Programs only work when you demonstrate on your own computer: -20 points
 Compress your readme.txt and *.java files into a single zip archive with the filename
lastname_firstname_hw3.zip. Use only standard zip format. Do not use other formats
such as zipx, rar, ace, etc. Improper filename or format will incur a 5 point penalty.
 Successful submission will be indicated by message in Digital Dropbox that the file has been
“sent to users with the role of Instructor”. We advise that you verify the timestamp, download
and double check your zip file for good measure.
 In Digital Dropbox, make sure that you send the file rather than just adding it. Failure to do so
will be treated as non-submission. Late policy applies until correctly submitted. How to tell the
difference between having sent the file vs. only added the file:
 Files sent to the Instructor will show the date and time submitted.
 Files that are added to the Drop Box but not sent will show the date and time posted.
 Sending a file to a Dropbox for a different course or to a different system such as USC
Blackboard will be treated as non-submission. Late policy applies until correctly submitted.
 It is ok to modify and re-send your file, but only until the deadline. If you do send any amended
submission(s), please indicate which one is the final version.
 DEN students should submit electronically via the Digital Dropbox as described above.
 This assignment is due before the class on the due date shown above.
 There is a 20% deduction per day for late submissions. Assignments more than 5 days late will
not be accepted.
 Note that you must do all the assignments and take both exams in order to pass the course.

Discussion Board and Student Collaboration Policy


 You should work on this assignment individually and within the realm of the USC Academic
Integrity Guidelines.
 A discussion board for this assignment is available on DEN's Blackboard system.
 Use the discussion board as your main resource to post questions related to the assignment.
 We encourage you to discuss issues related to this assignment with other students without
revealing and/or hinting at any answers.
 The TAs will participate in the discussions and answer questions on the board.
 Do not ask TAs homework-specific questions by email.
 Start your homework early. Although the discussion board will remain open until the deadline,
the TAs cannot guarantee that they will be able to answer any/all last minute questions posted
less than 24 hours before the deadline.