Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.0 » UltraLiteJ » Using UltraLiteJ » Tutorial: Build a BlackBerry application » Part 1: Creating an UltraLiteJ application on BlackBerry


Lesson 3: Create an UltraLiteJ database

In this lesson, you write code to create and connect to an UltraLiteJ database. The code to create a new database is defined in a singleton class called DataAccess, and is invoked from the HomeScreen constructor. Using a singleton class ensures that only one database connection is open at a time. While UltraLiteJ supports multiple connections, it is a common design pattern to use a single connection.

  1. Modify the HomeScreen constructor to instantiate a DataAccess object.

    Here is a complete, updated HomeScreen class. The DataAccess object is held as a class-level variable, so that it can be accessed from other parts of the code.

    class HomeScreen extends MainScreen {
        HomeScreen() {
            // Set the window title
            LabelField applicationTitle = new LabelField("Hello BlackBerry");
            // Add a label to show application status
            _statusLabel = new LabelField( "Status: Started");
            add( _statusLabel );
            // Create database and connect
                _da = DataAccess.getDataAccess();
                _statusLabel.setText("Status: Connected");
            catch( Exception ex)
                _statusLabel.setText("Exception: " + ex.toString() );
        private LabelField _statusLabel;
        private DataAccess _da;
  2. Create a file named myapp\ in the HelloBlackBerry project.
  3. Provide a getDataAccess method that ensures a single database connection.
    package myapp;
    import ianywhere.ultralitej.*;
    import java.util.*;
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
    class DataAccess {
        DataAccess() {    }
        public static synchronized DataAccess getDataAccess(boolean reset) 
                      throws Exception
            if( _da == null ){
                _da = new DataAccess();
                ConfigObjectStore _config =
                    _conn = DatabaseManager.createDatabase( _config );
                        _conn = DatabaseManager.connect( _config );
                    catch( ULjException uex1) {
                        if( uex1.getErrorCode() !=
                            ULjException.SQLE_ULTRALITE_DATABASE_NOT_FOUND ) {
                            System.out.println( "Exception: " +
                                                uex1.toString() );
                            Dialog.alert( "Exception: " + uex1.toString() +
                                          ". Recreating database..." );
                        _conn = DatabaseManager.createDatabase( _config );
            return _da;        
        private static Connection _conn;
        private static DataAccess _da;

    This class imports the ianywhere.ultralitej package from the UltraLiteJ.jar file. The steps to create or connect to a database are:

    1. Define a configuration. In this example, it is a ConfigObjectStore configuration object, meaning that the UltraLiteJ database is persisted in the BlackBerry object store.
    2. Attempt to connect to the database.

      If the connection attempt fails, create the database. The createDatabase method then returns an open connection.

  4. Press F5 to build and deploy the application to the device simulator.
  5. From the File menu, choose Load Java Program.
  6. Browse to the J2meRim11 folder of your UltraLiteJ installation and open the UltraLiteJ.cod file.
  7. Run the program from the Simulator.

    You should see a status message indicating that the application successfully connected to the database.