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.

  • Object stores   Supported with a ConfigObjectStore.

  • Record stores   Supported with a ConfigRecordStore.

  • Persistent file stores   Supported with a ConfigFile.

  • Non-persistent stores   Supported with a ConfigNonPersistent.

Properties of the Connection object
  • Transactions   In the UltraLiteJ API, there is no AutoCommit mode. Each transaction must be followed by the commit method of the Connection. They can be rolled back with a rollback statement.

  • Prepared SQL statements   Methods are provided to handle the SQL statement, which are executed from the Connection.

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

  • Table operations   UltraLiteJ database tables are accessed and maintained using methods of the Connection.

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 the 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 supported by all platforms:

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

    The following example sets the password to my_password:

    config.setPassword("my_password");
  3. Create a new Connection using the following syntax:

    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

The 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 supported by all platforms with the following syntax:

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

    The following example assumes that the password is my_password:

    config.setPassword("my_password");
  3. Create a new Connection using the following code:

    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 object and all properties associated with it.

See also