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

jGuru: Help: Data Retrieval

 


[Exercise | API Docs | Short Course| Exercises]

Help is available for each task.



Task 1

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

      ResultSet result = stmt.executeQuery(
        "SELECT Entry, Customer, DOW, Cups, Type " +
        "FROM JJJJData " +
        "ORDER BY Cups DESC");

Task 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.

      if( result.next() ) // get first row 
      {                   // if data was returned
        sCustomer = result.getString("Customer");
        iCups = result.getInt("Cups");
        System.out.println(
          "On " + result.getString("DOW") +
         " 4J Customer " + sCustomer + 
         " consumed the most coffee." +
         " Cups: " + iCups + 
         ", Type: " + result.getString("Type") +
         ".\n");

        iTotalCups = iCups;  // increment total

Task 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.

        while(result.next())   // for each row of data
        {
          iEntry = result.getInt("Entry");
          sCustomer = result.getString("Customer");
          sDOW = result.getString("DOW");
          iCups = result.getInt("Cups");
          iTotalCups += iCups;  // increment total
          sType = result.getString("Type");

          // Report each Customer
          System.out.println( iEntry    + ",\t" +
                              sCustomer + ",\t" +
                              sDOW      + ",\t" +
                              iCups     + ",\t" +
                              sType );        }

Task 4

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

        // Report total
        System.out.println(
          "\n4J Cafe Total Weekly Sales: " +
             iTotalCups + " cups of coffee.");

      } // end if( result.next() )

    } // end try
    catch (Exception e) { e.printStackTrace(); }
    finally
    {
      try { stmt.close(); }
      catch( Exception e ) {}

      try { con.close(); }
      catch( Exception e ) {}
    } // end finally clause

A ResultSet is automatically closed when its associated Statement is closed. In addition, the current ResultSet is closed when a new Statement.execute method, of any variety, is invoked. Even so, it is good practice to close a ResultSet when you are finished with it. This was not done here, because the program essentially obtained the ResultSet, operated on the data as we got it, then closed the Statement and ended the program.

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