Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

jGuru: Data Retrieval

 


[Help | API Docs | Short Course| Exercises]

This exercise outlines the Report4J application, which retrieves and reports on data from the JJJJData table. As a general rule, the same code or functionality will not be explained twice in the exercises and examples; please refer to the earlier example for anything that does not yet seem familiar. This is the last exercise that is primarily meant to serve as an example and you are not expected to provide any code. Instead, you should pull up the suggested solution source and the help section for some detailed explanation of important areas. While reviewing the source, note the similarity to the code in the previous program for the basics of loading a JDBC driver, connecting to the database, creating a Statement, doing the actual work of the program, then closing the Statement and Connection.

As in the initial example, this program, as it stands, will only run properly against a Cloudscape database because the driver and connection information are hardcoded with literals. You can change this information and recompile to run against other databases.

Prerequisites



Tasks

  1. Query the data, using the SQL SELECT statement, in descending order by the JJJJData column Cups and obtain a ResultSet.

  2. Determine if at least one row was returned. If so, the first row contains the information regarding the customer who drank the most coffee in one day. Use the ResultSet.getXXX methods to store the column data into program variables. Print "On (DOW) 4J Customer (Customer) consumed the most coffee. Cups: (Cups), Type: (Type)." Set the Cups total to the initial Cups column value.

  3. While the ResultSet contains more data, move through each row. Use the ResultSet.getXXX methods to store the column data into program variables. Add the current Cups column value to the Cups total. Print all column data for the row.

  4. Print the total cups of coffee consumed. Close the Statement and the Connection.

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

Demonstration

When this program has been compiled and run against the database, it will generate the output for the customer who consumed the most coffee in one day, total cups consumed for the week, and display each row's data from the JJJJData table, as discussed in Retrieving Information from a Database.

Next Exercise

Exercises

Short Course



Copyright 1996-2000 jGuru.com. All Rights Reserved.