This example demonstrates how to insert rows in an UltraLiteJ database.
Change to the following directory: samples-dir\UltraLiteJ.
For information about the default location of samples-dir, see Samples directory.
Run the CreateDb example:
rundemo CreateDb |
Run the following command (the command is case sensitive):
rundemo LoadDb |
See .
// ***************************************************** // 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.*; /** * LoadDb -- sample program to demonstrate loading a Database. */ public class LoadDb { /** * Add a Department row. * @param conn connection to Database * @param dept_no department number * @param dept_name department name */ private static void addDepartment( PreparedStatement inserter, int dept_no, String dept_name ) throws ULjException { inserter.set( 1 /* "dept_no" */, dept_no ); inserter.set( 2 /* "name" */, dept_name ); inserter.execute(); } /** * Add an Employee row. * @param conn connection to Database * @param emp_no employee number * @param last_name employee last name * @param first_name employee first name * @param age employee age * @param dept_no department number where employee works */ private static void addEmployee( PreparedStatement inserter, int emp_no, String last_name , String first_name, int age, int dept_no ) throws ULjException { inserter.set( 1 /* "number" */, emp_no ); inserter.set( 2 /* "last_name" */, last_name ); inserter.set( 3 /* "first_name" */, first_name ); inserter.set( 4 /* "age" */, age ); inserter.set( 5 /* "dept_no" */, dept_no ); inserter.execute(); } /** * 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 inserter; inserter = conn.prepareStatement( "INSERT INTO Department( dept_no, name ) VALUES( ?, ? )" ); addDepartment( inserter, 100, "Engineering" ); addDepartment( inserter, 110, "Sales" ); addDepartment( inserter, 103, "Marketing" ); inserter.close(); inserter = conn.prepareStatement( "INSERT INTO employee( \"number\", last_name, first_name, age, dept_no ) VALUES( ?, ?, ?, ?, ? )" ); addEmployee( inserter, 1000, "Welch", "James", 58, 100 ); addEmployee( inserter, 1010, "Iverson", "Victoria", 23, 103 ); inserter.close(); conn.commit(); conn.release(); Demo.display( "LoadDb completed successfully" ); } catch( ULjException exc ) { Demo.displayException( exc ); } } } |
![]() |
Discuss this page in DocCommentXchange.
|
Copyright © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |