本示例演示如何执行内连接操作。在本方案中,每个员工都具有相应的部门信息。连接操作将 employee 表中的数据与 department 表中的相应数据相关联。通过使用 employee 表的部门编号找到 department 表中的相关信息来实现关联。
// ***************************************************** // 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 ); } } } |
![]() |
使用DocCommentXchange 讨论此页。
|
版权 © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |