Example: Inner join operations

This example demonstrates how to perform an inner join operation. In this scenario, every employee has corresponding department information. The join operation associates data from the employee table with corresponding data from the department table. The association is made with the department number in the employee table to locate the related information in the department table.

 To run the ReadInnerJoin.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 CreateDb example:

    rundemo CreateDb

    See Example: Creating a database.

  3. Run the LoadDb example:

    rundemo LoadDb

    See Example: Inserting rows.

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

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