Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.0 (Français) » UltraLiteJ » Using UltraLiteJ » Developing UltraLiteJ applications » Coding examples

 

Example: Aggregation and grouping

This example demonstrates UltraLiteJ support for the aggregation of results.

 ♦  To run the SalesReport.java example
  1. Change to the following directory: samples-dir\UltraLiteJ.

    For information about the default location of samples-dir, see Samples directory.

  2. Run the CreateSales example:

    rundemo CreateSales

    See Example: Creating a sales database.

  3. Run the following command (the command is case sensitive):

    rundemo SalesReport


// *****************************************************
// Copyright (c) 2006-2010 iAnywhere Solutions, Inc.
// Portions copyright (c) 2006-2010 Sybase, Inc.
// All rights reserved. All unpublished rights reserved.
// *****************************************************
// This sample code is provided AS IS, without warranty or liability
// of any kind.
//
// You may use, reproduce, modify and distribute this sample code
// without limitation, on the condition that you retain the foregoing
// copyright notice and disclaimer as to the original iAnywhere code.
//
// *********************************************************************
package com.ianywhere.ultralitej.demo;
import com.ianywhere.ultralitej12.*;

/** Create a sales report to illustrate aggregation support.
 */
public class SalesReport
{
    /** Mainline.
     * @param args program arguments (not used)
     */
    public static void main( String[] args )
    {
	try {
	    Configuration config = DatabaseManager.createConfigurationFile( "Sales.ulj" );
	    Connection conn = DatabaseManager.connect( config );
	    PreparedStatement stmt = conn.prepareStatement(
		    "SELECT inv_no, SUM( quantity * price ) AS total"
		    + " FROM InvoiceItem"
		    + " GROUP BY inv_no ORDER BY inv_no"
		);
	    ResultSet agg_cursor = stmt.executeQuery();
	    for( ; agg_cursor.next(); ) {
		int inv_no = agg_cursor.getInt( 1 /* "inv_no" */ );
		String total = agg_cursor.getString( "total" ); // access by name
		Demo.display( Integer.toString( inv_no ) + ' ' + total );
	    }
	    Demo.display( "SalesReport completed successfully" );
	} catch( ULjException exc ) {
	    Demo.displayException( exc );
	}
    }
}