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 应用程序 » 代码示例

 

示例:内连接操作

本示例演示如何执行内连接操作。在本方案中,每个员工都具有相应的部门信息。连接操作将 employee 表中的数据与 department 表中的相应数据相关联。通过使用 employee 表的部门编号找到 department 表中的相关信息来实现关联。

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

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

  2. 运行 CreateDb 示例:

    rundemo CreateDb

    请参见示例:创建数据库

  3. 运行 LoadDb 示例:

    rundemo LoadDb

    请参见示例:插入行

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

    rundemo ReadInnerJoin


// *****************************************************
// 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.*;

/**
 * ReadInnerJoin -- sample program to demonstrate reading the Employee table
 * and joining to each row the corresponding Department row.
 */
public class ReadInnerJoin
{
    /**
     * mainline for program.
     *
     * @param args command-line arguments
     *
     */
    public static void main
	( String[] args )
    {
	try {
	    Configuration config = DatabaseManager.createConfigurationFile( "Demo1.ulj" );
	    Connection conn = DatabaseManager.connect( config );
	    PreparedStatement stmt = conn.prepareStatement(
		    "SELECT E.number, E.last_name, E.first_name, E.age,"
		    + " E.dept_no, D.name"
		    + " FROM Employee E"
		    + " JOIN Department D ON E.dept_no = D.dept_no"
		    + " ORDER BY E.number"
		);
	    ResultSet cursor = stmt.executeQuery();
	    for( ; cursor.next(); ) {
		int emp_no = cursor.getInt( 1 /* "E.number" */ );
		String last_name = cursor.getString( "E.last_name" );
		String first_name = cursor.getString( 3 /* "E.first_name" */ );
		int age = cursor.getInt( 4 /* "E.age" */ );
		int dept_no = cursor.getInt( 5 /* "E.dept_no" */ );
		String dept_name = cursor.getString( 6 /* "D.name" */ );
		System.out.println( first_name + ' ' + last_name );
		System.out.print( "  empl. no = " );
		System.out.print( emp_no );
		System.out.print( "  dept = " );
		System.out.println( dept_no );
		System.out.print( "  age = " );
		System.out.print( age );
		System.out.println( ", " + dept_name );
	    }
	    cursor.close();
	    stmt.close();
	    conn.release();
	    Demo.display( "ReadInnerJoin completed successfully" );
	} catch( ULjException exc ) {
	    Demo.displayException( exc );
	}
    }
}