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

SQL Anywhere 11.0.0 » UltraLiteJ » Using UltraLiteJ » Tutorial: Build a BlackBerry application » Code listing for tutorial

 

DataAccess.java

/*
 * DataAccess.java
 *
 * © <your company here>, 2003-2005
 * Confidential and proprietary.
 */

package myapp;


import ianywhere.ultralitej.*;
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 
    {   
    try{
        if( _da == null ){
            _da = new DataAccess();
            ConfigObjectStore _config = 
               DatabaseManager.createConfigurationObjectStore("HelloDB");
            if(reset)
            {
                _conn = DatabaseManager.createDatabase( _config );
            } 
            else
            {
                try{
                    _conn = DatabaseManager.connect( _config );
                }
                catch( ULjException uex1) {
                    if( uex1.getErrorCode() !=
                        ULjException.SQLE_ULTRALITE_DATABASE_NOT_FOUND ) {
                        System.out.println( "Exception: " + uex1.toString() );
                        Dialog.alert( "Exception: " + uex1.toString() +
                           ". Recreating database..." );
                    }
                    _conn = DatabaseManager.createDatabase( _config );
                }
            }
            _da.createDatabaseSchema();
        }
        return _da;        
    } catch ( ULjException ue)
    {
        System.out.println("Exception in getDataAccess" + ue.toString() );
        return null;
    }
    }
    
    /**
     * 
     * Create the table in the database. 
     * If the table already exists, a harmless exception is thrown
     */
    private void createDatabaseSchema()
    {
        try{
            _conn.schemaCreateBegin();
            ColumnSchema column_schema;
            TableSchema table_schema = _conn.createTable("Names");
            column_schema = table_schema.createColumn( "ID", Domain.UUID );
            column_schema.setDefault( ColumnSchema.COLUMN_DEFAULT_UNIQUE_ID);
            table_schema.createColumn( "Name", Domain.VARCHAR, 254 );
            IndexSchema index_schema = 
               table_schema.createPrimaryIndex("prime_keys");
            index_schema.addColumn("ID", IndexSchema.ASCENDING);           
            _conn.schemaCreateComplete();
        }
        catch( ULjException uex1){
            System.out.println( "ULjException: " + uex1.toString() );
        }
        catch( Exception ex1){
            System.out.println( "Exception: " + ex1.toString() );
        }
    }
    
    public void insertName( String name ){
        try{
            Value 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();
        }
        catch( ULjException uex ){
            System.out.println( "ULjException: " + uex.toString() );
        }
        catch( Exception ex ){
            System.out.println( "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 ){
            System.out.println( "ULjException: " + uex.toString() );
        }
        catch( Exception ex ){
            System.out.println( "Exception: " + ex.toString() );
        }
        finally{
            return nameVector;
        }
    }
    
    public boolean sync() {
        try {
            if( _syncParms == null ){
                String host = "ultralitej.sybase.com";
                _syncParms = _conn.createSyncParms( "mluser",
                             "HelloBlackBerrySyncModel" );
                _syncParms.setPassword("mlpassword");
                _streamParms = _syncParms.getStreamParms();
                _streamParms.setPort( 80 ); // use your own
                _streamParms.setHost( host ); // use your own
                if(host.equals("ultralitej.sybase.com"))
                {
                    _streamParms.setURLSuffix(
                       "scripts/iaredirect.dll/ml/HelloBlackBerry/");
                }

            }
            System.out.println( "Synchronizing" );
            _conn.synchronize( _syncParms ); 
            return true;
        }
        catch( ULjException uex){
            System.out.println(uex.toString());
            return false;
        }
    }
    
    public boolean complete() {
        try{
            _conn.checkpoint();
            _conn.release();
            _conn = null;
            _da = null;
            _config = null;
            return true;
        }
        catch(Exception e){
            return false;
        }
    }

    private static ConfigObjectStore _config;
    private static Connection _conn;
    private static DataAccess _da;
    private static SyncParms _syncParms;
    private static StreamHTTPParms _streamParms;
}