Contents Index Lesson 2: Run the UltraLite generator Lesson 4: Build and run the application pdf/preface.pdf

UltraLite User's Guide
  Tutorial: Build an Application Using Java

Lesson 3: Write the application code


The following code listing holds a very simple UltraLite application.

You can copy the code into a new file and save it as Sample.java in your c:\JavaTutorial directory, or open a new file and type the content. You can find this source code in Samples\UltraLite\JavaTutorial\Sample.java.

// (1) Import required packages
import java.sql.*;
import ISampleSQL.*;
import ianywhere.ultralite.jdbc.*;
import ianywhere.ultralite.support.*;
// (2) Class implements the interface containing SQL statements
public class Sample implements ISampleSQL
{
  public static void main( String[] args )
  {
    try{
        // (3) Connect to the database
        java.util.Properties p = new
            java.util.Properties();
        p.put( "persist", "file" );
        SampleDB db = new SampleDB( p );
        Connection conn = db.connect();
        // (4) Initialize the database with data
        PreparedStatement pstmt1 =
            conn.prepareStatement( INSERT_PRODUCT );
        pstmt1.setInt(1, 1);
        pstmt1.setInt(2, 400);
        pstmt1.setString(3, "4x8 Drywall x100");
        int rows1=pstmt1.executeUpdate();
        pstmt1.setInt(1, 2);
        pstmt1.setInt(2, 3000);
        pstmt1.setString(3, "8' 2x4 Studs x1000");
        int rows2=pstmt1.executeUpdate();
        // (5) Query the data and write out the results
        Statement stmt = conn.createStatement();
        ResultSet result = stmt.executeQuery(
             SELECT_PRODUCT );
        while( result.next() ) {
            int id = result.getInt( 1 );
            String name = result.getString( 2 );
            int price = result.getInt( 3 );
            System.out.println ( name +
                " \tId=" + id +
                " \tPrice=" + price );
        }
        // (6) Close the connection to end
        conn.close();
    } catch (SQLException e) {
        Support.printException( e );
    }
  }
}
Explanation of the sample program 

Although too simple to be useful, this example contains elements that must be present in all Java programs used for database access. The following describes the key elements in the sample program. Use these steps as a guide when creating your own Java UltraLite application.

The numbered steps correspond to the numbered comments in the source code.

  1. Import required packages.

    The sample program utilizes JDBC interfaces and classes and therefore must import this package. It also requires the UltraLite runtime classes, and the generated interface that contains the SQL statement strings.

  2. Define the class.

    The SQL statements used in the application are stored in a separate file, as an interface. The class must declare that it implements the interface to be able to use the SQL statements for the project. The class names are based on the statement names you provided when adding the statements to the database.

  3. Connect to the database.

    The connection is established using an instance of the database class. The database name must match the name of the generated Java class (in this case SampleDB). The file value of the persist Properties object states that the database should be persistent.

  4. Insert sample data.

    In a production application, you would generally not insert sample data. Instead, you would obtain an initial copy of data by synchronization. In the early stages of development, it can simplify your work to directly insert data.

  5. Execute a select SQL command using a Statement object

  6. End your Java UltraLite program


Contents Index Lesson 2: Run the UltraLite generator Lesson 4: Build and run the application pdf/preface.pdf