Example: Reading a table

In this example, a PreparedStatement object is obtained from a connection, and a ResultSet object is obtained from the PreparedStatement. The next method on the ResultSet returns true each time a subsequent row can be obtained. Values for the columns in the current row can then be obtained from the ResultSet object.

Notes:
package ianywhere.ultralitej.demo;
import ianywhere.ultralitej.*;
/**
 * ReadSeq -- sample program to demonstrate reading a Database table
 * sequentially.
 */
public class ReadSeq
{
    /**
     * 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 * FROM Employee ORDER BY number" );
            ResultSet cursor = stmt.executeQuery();
            for( ; cursor.next(); ) {
                /* Can't access columns by name because no meta data */
                int emp_no = cursor.getInt( 1 /* "number" */ );
                String last_name = cursor.getString( 2 /* "last_name" */ );
                String first_name = cursor.getString( 3 /* "first_name" */ );
                int age = cursor.getInt( 4 /* "age" */ );
                Demo.display( first_name + ' ' + last_name );
                Demo.display( "  empl. no = "
                            , Integer.toString( emp_no )
                            , "  age = "
                            , Integer.toString( age ) );
            }
            cursor.close();
            stmt.close();
            conn.release();
        } catch( ULjException exc ) {
            Demo.displayException( exc );
        }
    }
}