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

SAP Sybase SQL Anywhere 16.0 » UltraLite - Java Programming » Tutorial: Building a BlackBerry application » Part 1: Creating a new BlackBerry application

 

Lesson 3: Creating an UltraLite Java edition database

In this lesson, you write code to create and connect to an UltraLite Java edition database.

Prérequis

This lesson assumes you have completed all preceding lessons. See Lesson 1: Setting up a new BlackBerry project.

Contexte et remarques

The code that creates a new database is defined in a singleton class named DataAccess, and is invoked from the HomeScreen constructor. Using a singleton class ensures that only one database connection is open at a time. While the UltraLiteJ API supports multiple connections, it is a common design pattern to use a single connection.

 Task
  1. Modify the HomeScreen class to instantiate a DataAccess object.

    The following is the complete and updated HomeScreen class code listing:



    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
    import java.util.*;
    
    class HomeScreen extends MainScreen {
    
        HomeScreen() {
            
            // Set the window title
            LabelField applicationTitle = new LabelField("Hello BlackBerry");
            setTitle(applicationTitle);
            
            // Add a label to show application status
            _statusLabel = new LabelField("Status: Started");
            add(_statusLabel);
            
            // Create database and connect
            try {
                _da = DataAccess.getDataAccess(true);
                _statusLabel.setText("Status: Connected");
            }
            catch(Exception ex)
            {
                _statusLabel.setText("Exception: " + ex.toString());
            }        
        }
        private LabelField _statusLabel;
        private DataAccess _da;
    }

    Eclipse may report warnings indicating that DataAccess cannot be resolved. The DataAccess object is held as a class-level variable so that it can be accessed from other parts of the code. You create the DataAccess class in the next step.

  2. Add a DataAccess class to your project.

    1. In the Package Explorer window, expand HelloBlackBerry and click src.

    2. Click File » New » Class.

      The New Java Class window appears.

    3. In the Name field, type DataAccess.

    4. Click Finish.

      The DataAccess.java file appears under your project in the Package Explorer window.

  3. Modify the DataAccess class so that it contains a getDataAccess method that ensures a single database connection.

    Double-click DataAccess.java in the Package Explorer window, and then replace the code with the following snippet:



    import com.ianywhere.ultralitej16.*;
    import net.rim.device.api.ui.component.*;
    import java.util.*;
    
    class DataAccess {
        DataAccess() {
        }
        
        public static synchronized DataAccess getDataAccess(boolean reset) 
            throws Exception
        {   
            if (_da == null) {
                _da = new DataAccess();
                ConfigObjectStore config = DatabaseManager.createConfigurationObjectStore("HelloDB");
                if (reset) {
                    _conn = DatabaseManager.createDatabase(config);
                    // _da.createDatabaseSchema();
                } 
                else {
                    try {
                        _conn = DatabaseManager.connect(config);
                    }
                    catch (ULjException uex1) {
                        if (uex1.getErrorCode() != ULjException.SQLE_ULTRALITE_DATABASE_NOT_FOUND) {
                            Dialog.alert("Exception: " + uex1.toString() + ". Recreating database...");
                        }
                        _conn = DatabaseManager.createDatabase(config);
                        // _da.createDatabaseSchema();
                    }
                }
            }
            return _da;        
        }
        private static Connection _conn;
        private static DataAccess _da;
    }

    This class imports the com.ianywhere.ultralitej16 package from the UltraLiteJ16.jar file. The following steps are needed to create or connect to an UltraLite Java edition database:

    1. Define a configuration. In this tutorial, the configuration object is defined by the ConfigObjectStore interface, which allows you to configure a persistent database that resides in the BlackBerry object store.

    2. Attempt to connect to the database. In this tutorial, the database is created using the createDatabase method when the connection attempt fails. This method then returns an open connection.

  4. Click File » Save.

  5. Run the simulator.

    In the Package Explorer window, click Application.java, and then click Run » Run As » BlackBerry Simulator.

    Note

    If multiple projects are open in your workspace, click Run » Run Configurations, select HelloBlackBerry, and then click Run.

    The HelloBlackBerry project compiles and then the simulator window appears.

    Ensure that the project compiles without errors by selecting the Problems tab in Eclipse.

  6. From the simulator menu, click File » Load Java Program.

  7. Browse to the \UltraLite\UltraLiteJ\BlackBerry4.2\ directory of your SQL Anywhere installation and open the UltraLiteJ16.cod file.

    Note

    You may need to copy UltraLiteJ16.cod and the DBG files to the working simulator directory (for example, C:\Eclipse\plugins\net.rim.ejde.componentpack6.0.0_6.0.0.0.26\components\simulator\) to run the application. When copied, you do not need to load the Java program from the simulator menu.

  8. From the simulator menu, click Simulate » Set IT Policy.

    The Set IT Policy window appears.

  9. In the Policy field, click Allow Third Party Apps to Use Persistent Store and then click >>.

  10. Click Set and then click Close.

  11. Launch your application.

    In the simulator window, navigate to Downloads and then run the HelloBlackBerry application.

    A screen appears that displays the Hello BlackBerry title bar and the Status: Connected text, which indicates that the application has successfully connected to the UltraLite Java edition database.

  12. Stop the simulation.

    In the simulator window, click File » Exit.

Résultat

The application runs in the simulator, and the code creates an UltraLite Java Edition database.

 See also