UltraLite User's Guide
Tutorial: Build an Application Using Java
Once you have tested that your program is functioning properly, you can remove the lines of code that manually insert data into the ULProduct table. Replace these statements with a call to the JdbcConnection.synchronize() function to synchronize the remote database with the consolidated database. This process will fill the tables with data and you can subsequently execute a select query.
Adding synchronization actually simplifies the code. Your initial version of Sample.java uses the following lines to insert data into your UltraLite database.
PreparedStatement pstmt1 = conn.prepareStatement( ADD_PRODUCT_1 ); pstmt1.setInt(1, 1); pstmt1.setInt(2, 400); pstmt1.setString(3, "4x8 Drywall x100"); int rows1=pstmt1.executeUpdate(); pstmt1.setInt(1, 2); pstmt1.setInt(2, 3000); pstmt1.setString(3, "8' 2x4 Studs x1000"); int rows2=pstmt1.executeUpdate();
This code is included to provide an initial set of data for your application. In a production application, you would not insert an initial copy of your data from source code, but would carry out a synchronization.
To add synchronization to your application
Replace the hard-coded inserts with a synchronization call.
Delete the instructions listed above, which insert code.
Add the following line in their place:
UlSynchOptions synch_opts = new UlSynchOptions(); synch_opts.setUserName( "50" ); synch_opts.setPassword( "pwd50" ); synch_opts.setScriptVersion( "custdb" ); synch_opts.setStream( new UlSocketStream() ); synch_opts.setStreamParms( "host=localhost" ); ( (JdbcConnection)conn ).synchronize( synch_opts );
The ULSocketStream argument instructs the application to synchronize over TCP/IP, to a MobiLink synchronization server on the current machine (localhost), using a MobiLink user name of 50.
Compile and link your application.
Enter the following command, with a CLASSPATH that includes the current directory, the UltraLite runtime classes, and the Java runtime classes:
javac *.java
Start the MobiLink synchronization server running against the sample database.
From a command prompt in your JavaTutorial directory, enter the following command:
start dbmlsrv9 -c "dsn=UltraLite 9.0 Sample"
Run your application.
From a command prompt in your JavaTutorial directory, enter the following command:
java Sample
The application connects, synchronizes to receive data, and writes out information to the command line. The output is as follows:
Connecting to server:port = localhost(a.b.c.d):2439 4x8 Drywall x100 Id=1 Price=400 8' 2x4 Studs x1000 Id=2 Price=3000 Drywall Screws 10lb Id=3 Price=40 Joint Compound 100lb Id=4 Price=75 Joint Tape x25x500 Id=5 Price=100 Putty Knife x25 Id=6 Price=400 8' 2x10 Supports x 200 Id=7 Price=3000 400 Grit Sandpaper Id=8 Price=75 Screwmaster Drill Id=9 Price=40 200 Grit Sandpaper Id=10 Price=100
In this lesson, you have added synchronization to a simple UltraLite application.
For more information on the JdbcConnection.synchronize() function, see Adding synchronization to your application.