Example: Inserting rows

This example demonstrates how to insert rows in an UltraLiteJ database.

Notes:
package ianywhere.ultralitej.demo;
import ianywhere.ultralitej.*;
/**
 * 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 );
        }
    }
}