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

SQL Anywhere 12.0.0 (Deutsch) » UltraLiteJ » UltraLiteJ verwenden » UltraLiteJ-Anwendungen entwickeln » Codebeispiele

 

Beispiel: Inner-Join-Vorgänge

Dieses Beispiel zeigt, wie Sie einen Inner-Join-Vorgang durchführen. In diesem Szenario hat jeder Mitarbeiter entsprechende Abteilungsinformationen. Der Join-Vorgang verknüpft Daten aus der Employee-Tabelle mit entsprechenden Daten aus der Department-Tabelle. Die Verknüpfung wird mit der Abteilungsnummer in der Employee-Tabelle hergestellt, um zugehörige Informationen in der Department-Tabelle zu finden.

 ♦  So führen Sie das ReadInnerJoin.java-Beispiel aus
  1. Wechseln Sie zum folgenden Verzeichnis: Beispielverzeichnis\UltraLiteJ.

    Hinweise zum Speicherort von Beispielverzeichnis finden Sie unter Beispielverzeichnis.

  2. Führen Sie das CreateDb-Beispiel aus:

    rundemo CreateDb

    Siehe Beispiel: Eine Datenbank erstellen.

  3. Führen Sie das LoadDb-Beispiel aus:

    rundemo LoadDb

    Siehe Beispiel: Zeilen einfügen.

  4. Führen Sie den folgenden Befehl aus (mit Berücksichtigung der Groß- und Kleinschreibung):

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