|
/*
* DataAccess.java
*
* © <your company here>, 2003-2005
* Confidential and proprietary.
*/
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;
}
/**
*
* Create the table in the database.
* If the table already exists, a harmless exception is thrown
*/
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();
}
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() );
}
finally{
return nameVector;
}
}
public boolean sync() {
try {
if( _syncParms == null ){
String host = "relayserver.sybase.com/ias_relay_server/client/rs_client.dll/tomslee.HelloBlackBerry";
_syncParms = _conn.createSyncParms( "mluser", "HelloBlackBerrySyncModel" );
_syncParms.setPassword("mlpassword");
_streamParms = _syncParms.getStreamParms();
_streamParms.setPort( 8081 ); // use your own
_streamParms.setHost( host ); // use your own
if(host.equals("relayserver.sybase.com/ias_relay_server/client/rs_client.dll/tomslee.HelloBlackBerry"))
{
_streamParms.setURLSuffix("/ias_relay_server/client/rs_client.dll/tomslee.HelloBlackBerry");
}
}
_conn.synchronize( _syncParms );
return true;
}
catch( ULjException uex){
Dialog.alert(uex.toString());
return false;
}
}
public boolean complete() {
try{
_conn.checkpoint();
_conn.release();
_conn = null;
_da = null;
return true;
}
catch(Exception e){
return false;
}
}
private static Connection _conn;
private static DataAccess _da;
private static SyncParms _syncParms;
private static StreamHTTPParms _streamParms;
} |