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

SQL Anywhere 12.0.0 (中文) » UltraLiteJ » 使用 UltraLiteJ » 开发 UltraLiteJ 应用程序 » 代码示例

 

示例:同步数据库

本示例演示与 MobiLink 服务器连接,以便同步客户端数据库和统一(在本例中是 SQL Anywhere)数据库。

 ♦  运行 Sync.java 示例
  1. 转到以下目录:samples-dir\UltraLiteJ

    有关 samples-dir 缺省位置的信息,请参见示例目录

  2. 运行 CreateSales 示例:

    rundemo CreateSales

    请参见示例:创建销售数据库

  3. 运行以下命令启动 MobiLink 服务器:

    start_ml
  4. 运行以下命令(此命令区分大小写):

    rundemo Sync
  5. 完成示例后,关闭 MobiLink 服务器。请参见停止 MobiLink 服务器



// *****************************************************
// Copyright (c) 2006-2010 iAnywhere Solutions, Inc.
// Portions copyright (c) 2006-2010 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 com.ianywhere.ultralitej.demo;

import com.ianywhere.ultralitej12.*;

/**
 * Sync: sample program to demonstrate Database synchronization.
 *
 * Requires starting the MobiLink Server Sample using start_ml.bat
 */
public class Sync
{
    /**
     * mainline for program.
     *
     * @param args command-line arguments
     *
     */
    public static void main
	( String[] args )
    {
	try {
	    Configuration config = DatabaseManager.createConfigurationFile( "Demo1.ulj" );
	    Connection conn = DatabaseManager.createDatabase( config );

	    PreparedStatement ps = conn.prepareStatement(
	    	"CREATE TABLE ULCustomer" +
		"( cust_id int NOT NULL PRIMARY KEY" +
		", cust_name VARCHAR(30) NOT NULL" +
		")"
		);
	    ps.execute();
	    ps.close();

	    //
	    // Synchronization
	    //

	    // Version set for MobiLink 12.0.x
	    SyncParms syncParms = conn.createSyncParms( SyncParms.HTTP_STREAM, "50", "custdb 12.0" );
	    syncParms.getStreamParms().setPort( 9393 );
	    conn.synchronize( syncParms );
	    SyncResult result = syncParms.getSyncResult();
	    Demo.display(
		    "*** Synchronized *** bytes sent=" + result.getSentByteCount()
		    + ", bytes received=" + result.getReceivedByteCount()
		    + ", rows received=" + result.getReceivedRowCount()
		);

	    conn.release();

	} catch( ULjException exc ) {
	    Demo.displayException( exc );
	}
    }
}