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.
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"); 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; } |
Create a file named myapp\DataAccess.java in the HelloBlackBerry project.
Provide a getDataAccess method that ensures a single database connection.
package myapp; import com.ianywhere.ultralitej12.*; 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 = 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.ultralitej12 package from the UltraLiteJ12.jar file. The steps to create or connect to a database are:
Define a configuration. In this example, it is a ConfigObjectStore configuration object, meaning that the UltraLiteJ database is persisted in the BlackBerry object store.
Attempt to connect to the database.
If the connection attempt fails, create the database. The createDatabase method then returns an open connection.
Press F5 to build and deploy the application to the device simulator.
From the File menu, choose Load Java Program.
Browse to the BlackBerry4.2 folder of your UltraLiteJ installation and open the UltraLiteJ12.cod file.
Run the program from the Simulator.
You should see a status message indicating that the application successfully connected to the database.
Discuss this page in DocCommentXchange.
|
Copyright © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |