Example: Filtering rows to match criteria

This example demonstrates how to read rows where the age field is greater than 50.

Notes:
package ianywhere.ultralitej.demo;
import ianywhere.ultralitej.*;
/**
 * ReadFilter -- sample program to demonstrate reading a Database table sequentially
 * and filtering rows.
 * <p> Rows are accepted when the age column has a value
 * greater than 50.
 */
public class ReadFilter
{
    /**
     * 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 WHERE age > 50 ORDER BY number"
            );
            ResultSet cursor =  stmt.executeQuery();
            for( ; cursor.next(); ) {
                int emp_no = cursor.getInt( 1 /* "Employee.number" */ );
                String last_name = cursor.getString( 2 /* "Employee.last_name" */ );
                String first_name = cursor.getString( 3 /* "Employee.first_name" */ );
                int age = cursor.getInt( 4 /* "Employee.age" */ );
                System.out.println( first_name + ' ' + last_name );
                System.out.print( "  empl. no = " );
                System.out.print( emp_no );
                System.out.print( "  age = " );
                System.out.println( age );
            }
            cursor.close();
            stmt.close();
            conn.release();
            Demo.display( "ReadFilter completed successfully" );
        } catch( ULjException exc ) {
            Demo.displayException( exc );
        }
    }

    /**
     * Filter class
     *
     */
    private static class AgeFilter
        implements Filter
    {
        /**
         * Accept child rows when age > 50.
         *
         * @param child The child ResultSet.
         * @return true, if age > 50.
         *
         */
        public boolean evaluate( ResultSet child )
        {
            try {
                ResultSet cursor = (ResultSet) child;
                int age = cursor.getInt( "Employee.age" );
                return age > 50;
            } catch( ULjException exc ) {
                Demo.displayException( exc );
                return false;
            }
        }
    }
}