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

SQL Anywhere 12.0.0 (中文) » UltraLiteJ » 使用 UltraLiteJ » 开发 UltraLiteJ 应用程序 » 代码示例

 

示例:集合和分组

本示例演示 UltraLiteJ 对结果集合的支持。

 ♦  运行 SalesReport.java 示例
  1. 转到以下目录:samples-dir\UltraLiteJ

    有关 samples-dir 缺省位置的信息,请参见示例目录

  2. 运行 CreateSales 示例:

    rundemo CreateSales

    请参见示例:创建销售数据库

  3. 运行以下命令(此命令区分大小写):

    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 );
	}
    }
}