DataAccess.java



// *****************************************************
// Copyright (c) 2006-2011 iAnywhere Solutions, Inc.
// Portions copyright (c) 2006-2011 Sybase, Inc.
// All rights reserved. All unpublished rights reserved.
// *****************************************************
// This sample code is provided AS IS, without warranty or liability
// of any kind.
//
// You may use, reproduce, modify and distribute this sample code
// without limitation, on the condition that you retain the foregoing
// copyright notice and disclaimer as to the original iAnywhere code.
//
// *********************************************************************
package myapp;

import com.ianywhere.ultralitej12.*;

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 void createDatabaseSchema() {
        try {
            String sql = "CREATE TABLE Names (ID UNIQUEIDENTIFIER DEFAULT NEWID(), Name VARCHAR(254), " +
                "PRIMARY KEY (ID))";
            PreparedStatement ps = _conn.prepareStatement(sql);
            ps.execute();
            ps.close();
        }
        catch (ULjException uex1) {
            Dialog.alert("ULjException: " + uex1.toString());
        }
        catch (Exception ex1) {
            Dialog.alert("Exception: " + ex1.toString());
        }
    }
    public void insertName(String name){
        try {
            UUIDValue nameID = _conn.createUUIDValue();
            String sql = "INSERT INTO Names(ID, Name) VALUES(?, ?)";           
            PreparedStatement ps = _conn.prepareStatement(sql);
            ps.set(1, nameID);
            ps.set(2, name);
            ps.execute();
            _conn.commit();
            ps.close();
        }
        catch(ULjException uex) {
            Dialog.alert("ULjException: " + uex.toString());
        }
        catch( Exception ex ){
            Dialog.alert("Exception: " + ex.toString());
        }  
    }
    public Vector getNameVector(){
        Vector nameVector = new Vector();
        try {
            String sql = "SELECT ID, Name FROM Names";
            PreparedStatement ps = _conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while ( rs.next() ){
                String nameID = rs.getString(1);
                String name = rs.getString(2);
                NameRow nr = new NameRow( nameID, name);
                nameVector.addElement(nr);
            }
        }
        catch( ULjException uex ){
            Dialog.alert("ULjException: " + uex.toString());
        }
        catch( Exception ex ){
            Dialog.alert("Exception: " + ex.toString());
        }
        return nameVector;
    }
    
    public boolean sync() {
        try {
            if(_syncParms == null){
                _syncParms = _conn.createSyncParms(SyncParms.HTTP_STREAM, 
                				"mluser", 
                				"HelloBlackBerrySyncModel");
                _syncParms.setPassword("mlpassword");
                _syncParms.getStreamParms().setHost("your-host-name"); // USE YOUR OWN
                _syncParms.getStreamParms().setPort(8081); // USE YOUR OWN
            }
            _conn.synchronize(_syncParms);
            return true;
        }
        catch(ULjException uex) {
            Dialog.alert("Exception: " + uex.toString());
            return false;
        }
    }
    
    private static Connection _conn;
    private static DataAccess _da;
    private static SyncParms _syncParms;
}