Contents Index Lesson 2: Insert data into the database Lesson 4: Deploy your application to a Windows CE device

Native UltraLite for Java User's Guide
  Tutorial: An Introductory Application

Lesson 3: Select the rows from the table


This lesson retrieves rows from the table, and prints them on the command line. It shows how to loop over the rows of a table.

To list the rows in the table

  1. Add the following method to the Customer.java file:

    private void select() throws SQLException
    {
      // Fetch rows
      // Open the Customer table
      Table t = conn.getTable( "customer" );
      t.open();
    
      short id = t.schema.getColumnID( "id" );
      short fname = t.schema.getColumnID( "fname" );
      short lname = t.schema.getColumnID( "lname" );
    
    
      t.moveBeforeFirst();
      while( t.moveNext() ) {
          System.out.println(
            "id= " + t.getInt( id )
            + ", name= " + t.getString( fname )
            + " " + t.getString( lname )
            );
      }
      t.close();
    }

    This code carries out the following tasks:

  2. Add the following line to the main() method, immediately after the call to the insert method:

    cust.select();
  3. Compile and run your application, as in Lesson 1: Connect to the database.

The application prints the following message:

Connected to an existing database.
The table has 2 rows.
id= 1, name= Gene Poole
id= 2, name= Penny Stamp

Contents Index Lesson 2: Insert data into the database Lesson 4: Deploy your application to a Windows CE device