Accessing an UltraLiteJ database store

Applications must connect to an UltraLiteJ database before operations can be performed on the data. This section explains how to create or connect to a database with a specified password.

Implementations of the Configuration object

A Configuration is used to create and connect to a database. There are several different implementations of a Configuration provided in the API. A unique implementation exists for every type of database store supported by UltraLiteJ. Each implementation provides a different set of methods used to access the database store.

  • RIM object stores   Supported with a ConfigObjectStore.

  • Record stores   Supported with a ConfigRecordStore.

  • File system stores   Supported with a ConfigFile.

  • Non-persistent stores   Supported with a ConfigNonPersistent.

Properties of the Connection object
  • Transactions   Transactions must be committed to the database using the commit method of the Connection. They can be rolled back using the rollback method.

  • Prepared SQL statements   Methods are provided by the PreparedStatement interface to handle SQL statements. A PreparedStatement can be created using the prepareStatement method of the Connection.

  • Synchronization   A set of objects governing MobiLink synchronization is accessed from the Connection.

  • Table operations   UltraLiteJ database tables are accessed and maintained using methods provided by the Connection interface.

Creating a new UltraLiteJ database

An UltraLiteJ database can only be created using the API. You can not create a new database using Sybase Central or UltraLite command line utilities.

To create a database
  1. Create a new Configuration that references the database name.

    The proper syntax depends on the Java platform and the client device. In the following examples, config is the name of the Configuration object and DBname.ulj is the name of the new database.

    For J2ME BlackBerry devices:

    ConfigObjectStore config = 
        DatabaseManager.createConfigurationObjectStore("DBname.ulj");

    For all other J2ME devices:

    ConfigRecordStore config = 
        DatabaseManager.createConfigurationRecordStore("DBname.ulj");

    For J2SE devices:

    ConfigFile config = 
        DatabaseManager.createConfigurationFile("DBname.ulj");

    Alternatively, you can create a non-persistent database Configuration, which is supported by all platforms:

    ConfigNonPersistent config = 
        DatabaseManager.createConfigurationNonPersistent("DBname.ulj");
  2. Set a new database password using the setPassword method:

    config.setPassword("my_password");
  3. Create a new Connection:

    Connection conn = DatabaseManager.createDatabase(config);

    The createDatabase method finalizes the database creation process and connects to the database. After this method is called, you can perform schema and data operations but you can no longer change the name, password, or page size of the database.

Connecting to an existing database

An UltraLiteJ database must already exist on the client device before you can connect to it.

To connect to an existing database
  1. Create a new Configuration that references the name of the database.

    The proper syntax depends on the Java platform and the client device. In the following examples, config is the name of the Configuration object and DBname.ulj is the name of the database.

    For J2ME BlackBerry devices:

    ConfigObjectStore config = 
        DatabaseManager.createConfigurationObjectStore("DBname.ulj");

    For all other J2ME devices:

    ConfigRecordStore config = 
        DatabaseManager.createConfigurationRecordStore("DBname.ulj");

    For J2SE devices:

    ConfigFile config = 
        DatabaseManager.createConfigurationFile("DBname.ulj");

    Alternatively, you can connect to a non-persistent database Configuration, which is supported by all platforms:

    ConfigNonPersistent config = 
        DatabaseManager.createConfigurationNonPersistent("DBname.ulj");
  2. Specify the database password using the setPassword method:

    config.setPassword("my_password");
  3. Create a new Connection:

    Connection conn = DatabaseManager.connect(config);

    The connect method finalizes the database connection process. If the database does not exist, an error is thrown.

Disconnecting from a database

Use the release method of the DatabaseManager class to disconnect from the UltraLiteJ database. The release method closes the Connection and all properties associated with it.

See also