JDBC: Query Anything due Thu 19 Oct 10:30

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.

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.

Hints and Suggestions

Access to port 5432 on the csci server is limited. At a minimum you will have access from the computer lab. Because of the limited availability you need to be especially proactive in managing your time. This program is harder than it sounds. You should plan to have it done by Tuesday and bring any questions you have to class on that day.

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 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 into a single zipped archive called hw7.zip and upload into Canvas before the due date/time.

Quick Links