HW08: JDBC: Query Anything due Wed 08 Mar 13:00

In this assignment you will:
\item use JDBC ...
...esultSetMetaData} to produce aligned query results

Display Any Query

Write a Java program that will connect to the hotel reservations database you created in the previous assignment. The program should provide a method named displayQuery that will accept a query (String) as a parameter and will display the query results formatted so all columns are aligned. Each column should be labeled at the top with the heading name provided in the query result. The columns should be displayed at precisely the width of the longest data value plus a single space.

To accomplish this you will need to make two passes through the ResultSet. In the first pass you need to figure out the widest string to be display in each column. On the second pass you need to pad output with spaces to ensure that all items have the same width. REMEMBER: The column name may be the widest value.

As with the in-class assignment you should store your credentials in a file named dbconnect.txt rather than in the Java program itself.

Hints and Suggestions

Access to port 5432 on the csci server is open, but HSU's firewalls prevent off-campus access. You can work on this assignment in a couple of ways:
  1. Do your work on campus (HSUWireless or plugged in to the wall in JB 202).
  2. Do your work by connecting to the csci.hsutx.edu server using an ssh client. This will require you to learn a text-only editor (such as vim or nano). If you have not worked in this way you need to leave a couple of days just for the purpose of getting proficient in working with files in that environment. This document can help you get started: https://josephus.hsutx.edu/classes/all/remoteaccess/

To determine column header names and other useful metadata about the query result you will make use of a ResultSetMetaData object.

Since you will be making two passes through the ResultSet you need to make the result set scrollable. See the paragraphs of documentation at the top of https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html for information on how to do that.

I recommend (but don't require) that you use an object-oriented design for this program. In particular it would be helpful to have a class to manage a database connection whose constructor will connect to the database. Then the connection object (which will be a classwide attribute) will be available to your displayQuery() method without have to re-establish a connection to the database every time a query is issued.

Submission and Grading

Combine all of your source files (including the dbconnect.txt file) into a single zipped archive called hw8.zip and upload into Canvas before the due date/time.