Native UltraLite for Java User's Guide
Tutorial: An Introductory Application
This lesson uses MobiLink synchronization, which is part of SQL Anywhere Studio. You must have the SQL Anywhere Studio installed to carry out this lesson.
The steps involved in this lesson are to add synchronization code to your application, to start the MobiLink synchronization server, and to run your application to synchronize.
The synchronization is carried out with the UltraLite 9.0 Sample database. This is an Adaptive Server Anywhere database that holds several tables. The ULCustomer table has a cust_id column and a cust_name column. During synchronization the data in that table is downloaded to your UltraLite application.
This lesson assumes some familiarity with MobiLink synchronization.
To add synchronization to your application
Add the following method to the Customer.java file:
private void sync() throws SQLException { conn.syncParms.setStream( StreamType.TCPIP ); conn.syncParms.setVersion( "ul_default" ); conn.syncParms.setUserName( "sample" ); conn.syncParms.setSendColumnNames( true ); conn.syncParms.setDownloadOnly( true ); conn.synchronize(); }
This code carries out the following tasks:
Sets the synchronization stream to TCP/IP. Synchronization can also be carried out over HTTP, ActiveSync, or HTTPS. HTTPS synchronization requires that you obtain the separately licensable SQL Anywhere Studio security option.
For more information, see ianywhere.native_ultralite.StreamType and ianywhere.native_ultralite.Connection in the API Reference.
The syncParms field of the Connection object provides convenient access to a SyncParms object. For more information, see ianywhere.native_ultralite.SyncParms in the API Reference.
MobiLink synchronization is controlled by scripts at the MobiLink synchronization server. The script version identifies which set of scripts to use. The setSendColumnNames method together with an option on the MobiLink synchronization server generates those scripts automatically.
For more information, see the MobiLink Synchronization User's Guide.
Sets the MobiLink user ID. This is used for authentication at the MobiLink synchronization server, and is different from the UltraLite database user ID, although in some applications you may wish to make them the same.
Sets the synchronization to only download data. By default, MobiLink synchronization is two-way. Here, we use download-only synchronization so that the rows in your table do not get uploaded to the sample database.
Add the following line to the main()
method, immediately after the call to the insert method and before the call to the select method:
cust.sync();
Compile your application, as in Lesson 1: Connect to the database. Do not run the application yet.
To synchronize your data
Start the MobiLink synchronization server.
From a command prompt, start the MobiLink synchronization server with the following command line:
dbmlsrv9 -c "dsn=ASA 9.0 Sample" -v+ -zu+ -za
The ASA 9.0 Sample database has a Customer table that matches the columns in the UltraLite database you have created. You can synchronize your UltraLite application with the ASA 9.0 Sample database.
The -zu+
and -za
command line options provide automatic addition of users and generation of synchronization scripts. For more information on these options, see the MobiLink Synchronization User's Guide.
Run your application, as in Lesson 2: Connect to the database
The MobiLink synchronization server window displays status messages indicating the synchronization progress. The final message displays Synchronization complete
:
The data downloaded from the sample database are listed at the command prompt window, confirming that the synchronization succeeded:
Connected to an existing database. The table has 128 rows. id= 1, name= Gene Poole id= 2, name= Penny Stamp id= 101, name= Michaels Devlin id= 102, name= Beth Reiser id= 103, name= Erin Niedringhaus id= 104, name= Meghan Mason ...
This completes the tutorial.
For more code samples, see Samples\NativeUltraLiteForJava\Simple\Simple.java