在本课中,您将使用直接行处理来处理客户端数据库的 OrderComments 表中的行。为直接行处理添加以下方法:
GetUpload 将此方法用于 handle_UploadData 事件。GetUpload 将上载的注释写入名为 orderComments.txt 的文件中。
SetDownload 将此方法用于 handle_DownloadData 事件。SetDownload 使用 orderResponses.txt 文件来下载对远程客户端的响应。
还可以添加 EndDownload 方法来处理 end_download 事件。
以下过程介绍如何创建 Java 或 .NET 类,其中包括用于处理的方法。有关完整列表,请参见完整的 MobiLinkOrders 列表 (Java)或完整的 MobiLinkOrders 列表 (.NET)。
使用 Java 或 .NET 创建名为 MobiLinkOrders 的类。
对于 Java,在文本编辑器或开发环境中键入以下代码。
import ianywhere.ml.script.*; import java.io.*; import java.sql.*; public class MobiLinkOrders{ // to do... |
对于 .NET,使用以下代码:
using iAnywhere.MobiLink.Script; using System.IO; using System.Data; using System.Text; public class MobiLinkOrders // to do... |
声明一个类级别 DBConnectionContext 实例。
在 Java 中:
// class level DBConnectionContext DBConnectionContext _cc; |
在 .NET 中:
// class level DBConnectionContext private DBConnectionContext _cc = null; |
MobiLink 服务器会将 DBConnectionContext 实例传递到类构造函数。DBConnectionContext 封装有关与 MobiLink 统一数据库的当前连接的信息。
创建类构造函数。
类构造函数设置类级别 DBConnectionContext 实例。
对于 Java:
public MobiLinkOrders( DBConnectionContext cc ) { // set your class-level DBConnectionContext _cc = cc; } |
对于 .NET:
public MobiLinkOrders( DBConnectionContext cc ) { _cc = cc; } |
声明用于文件输入和输出的对象。
对于 Java,声明 java.io.FileWriter 和 java.io.BufferedReader:
// java objects for file i/o FileWriter my_writer; BufferedReader my_reader; |
对于 .NET,声明流写入器和流读取器:
// instances for file I/O private static StreamWriter my_writer = null; private static StreamReader my_reader = null; |
编写 EndDownload 方法。
此方法处理 end_download 连接事件并提供释放资源的机会。
对于 Java:
public void EndDownload() throws IOException { // free i/o resources if (my_reader!=null) my_reader.close(); if (my_writer!=null) my_writer.close(); } |
对于 .NET:
public void EndDownload() { if( my_writer != null ) { my_writer.Close(); my_writer = null; } |
编写 GetUpload 方法
GetUpload 方法获得表示 OrderComments 表的 UploadedTableData 类实例。OrderComments 表包含由远程销售雇员进行的特殊注释。您将在第 5 课:建立 MobiLink 客户端中创建该表。UploadedTableData getInserts 方法返回新订单注释的结果集。writeOrderComment 方法将结果集中的每行写出至文本文件。
对于 Java:
//method for the handle_UploadData synchronization event public void GetUpload( UploadData ut ) throws SQLException, IOException { // get an UploadedTableData for OrderComments UploadedTableData orderCommentsTbl = ut.getUploadedTableByName("OrderComments"); // get inserts uploaded by the MobiLink client ResultSet insertResultSet = orderCommentsTbl.getInserts(); while( insertResultSet.next() ) { // get order comments int _commentID = insertResultSet.getInt("comment_id"); int _orderID = insertResultSet.getInt("order_id"); String _specialComments = insertResultSet.getString("order_comment"); if (_specialComments != null) { writeOrderComment(_commentID,_orderID,_specialComments); } } insertResultSet.close(); } // writes out comment details to file public void writeOrderComment( int _commentID, int _orderID, String _comments ) throws IOException { // a FileWriter for writing order comments if(my_writer == null) { my_writer = new FileWriter( "C:\\MLdirect\\orderComments.txt",true); } // write out the order comments to remoteOrderComments.txt my_writer.write(_commentID + "\t" + _orderID + "\t" + _comments); my_writer.write( "\n" ); my_writer.flush(); } |
在 .NET 中:
// method for the handle_UploadData synchronization event. public void GetUpload( UploadData ut ) { // get UploadedTableData for remote table called OrderComments UploadedTableData order_comments_table_data = ut.GetUploadedTableByName( "OrderComments" ); // get inserts upload by the MobiLink client IDataReader new_comment_reader = order_comments_table_data.GetInserts(); while( new_comment_reader.Read() ) { // columns are // 0 - "order_comment" // 1 - "comment_id" // 2 - "order_id" // you can look up these values using the DataTable returned by: // order_comments_table_data.GetSchemaTable() if the send column names // option is turned on the remote. // in this example you just use the known column order to determine the column // indexes // only process this insert if the order_comment is not null if( !new_comment_reader.IsDBNull( 2 ) ) { int comment_id = new_comment_reader.GetInt32( 0 ); int order_id = new_comment_reader.GetInt32( 1 ); string comments= new_comment_reader.GetString( 2 ); WriteOrderComment( comment_id, order_id, comments ); } } // always close the reader when you are done with it! new_comment_reader.Close(); } |
编写 SetDownload 方法:
获得表示 OrderComments 表的类实例。
使用 DBConnectionContext getDownloadData 方法可获得 DownloadData 实例。使用 DownloadData getDownloadTableByName 方法可为 OrderComments 表返回 DownloadTableData 实例。
对于 Java:
DownloadData download_d = _cc.getDownloadData(); DownloadTableData download_td = download_d.getDownloadTableByName( "OrderComments" ); |
对于 .NET:
DownloadTableData comments_for_download = _cc.GetDownloadData().GetDownloadTableByName( "OrderComments" ); |
您将在第 5 课:建立 MobiLink 客户端中于远程数据库上创建此表。
获得允许您将插入或更新操作添加到下载的准备好的语句或者 IDBCommand。
对于 Java,使用 DownloadTableData getUpsertPreparedStatement 方法来返回一个 java.sql.PreparedStatement 实例。
PreparedStatement update_ps = download_td.getUpsertPreparedStatement(); |
对于 .NET,使用 DownloadTableData GetUpsertCommand 方法:
// add upserts to the set of operation that are going to be applied at the // remote database IDbCommand comments_upsert = comments_for_download.GetUpsertCommand(); |
将下载响应发送到远程客户端。
在 c:\MLdirect 中创建名为 orderResponses.txt 的文本文件。此文件包含对注释的响应。例如,orderResponses.txt 可以包含以下含有表示 comment_id、order_id 和 order_comment 的制表符分隔的值的条目。
... 786 34 OK, we will ship promotional material. 787 35 Yes, the product is going out of production. 788 36 No, we can't increase your commission... ... |
设置每行的下载数据。
对于 Java,以下示例遍历 orderResponses.txt 并将数据添加到 MobiLink 下载。
对于 Java:
// a BufferedReader for reading in responses if (my_reader==null) my_reader = new BufferedReader(new FileReader( "c:\\MLdirect\\orderResponses.txt")); // send updated comments down to clients String commentLine; // read the first line from orderResponses.txt commentLine = my_reader.readLine(); while(commentLine != null) { // get the next line from orderResponses.txt String[] response_details = commentLine.split("\t"); if (response_details.length != 3) { System.err.println("Error reading from orderResponses.txt"); System.err.println("Error setting direct row handling download"); return; } int comment_id = Integer.parseInt(response_details[0]); int order_id = Integer.parseInt(response_details[0]); String updated_comment = response_details[2]; // set an order comment response in the MobiLink download update_ps.setInt(1, comment_id); update_ps.setInt(2, order_id); update_ps.setString(3, updated_comment); update_ps.executeUpdate(); // get next line from orderResponses.txt commentLine = my_reader.readLine(); } |
对于 .NET:
string comment_line; while( (comment_line = my_reader.ReadLine()) != null) { // three values are on each line separated by '\t' string[] response_details = comment_line.Split( '\t' ); if( response_details.Length != 3 ) { throw( new SynchronizationException( "Error reading from orderResponses.txt" ) ); } int comment_id = System.Int32.Parse( response_details[0] ); int order_id = System.Int32.Parse( response_details[1] ); string comments= response_details[2]; // Parameters of the correct number and type have already been added // so you just need to set the values of the IDataParameters ((IDataParameter)(comments_upsert.Parameters[0])).Value = comment_id; ((IDataParameter)(comments_upsert.Parameters[1])).Value = order_id; ((IDataParameter)(comments_upsert.Parameters[2])).Value = comments; // add the upsert operation comments_upsert.ExecuteNonQuery(); } |
关闭用来将插入操作或更新操作添加到下载的预准备语句。
对于 Java:
update_ps.close(); |
对于 .NET,不必关闭 IDBCommand。MobiLink 会在下载结束时将其取消。
编译您的类文件。
浏览到包含 Java 或 .NET 源文件的目录。
编译 MobiLinkOrders 时引用面向 Java 或 .NET 的 MobiLink 服务器 API 库。
对于 Java,需要引用位于 install-dir\java 中的 mlscript.jar。运行以下命令来编译 Java 类:
javac -classpath "%SQLANY11%\java\mlscript.jar" MobiLinkOrders.java |
对于 .NET,运行以下命令:
csc /out:MobiLinkServerCode.dll /target:library /reference:"%SQLANY11%\Assembly\v2\iAnywhere.MobiLink.Script.dll" MobiLinkOrders.cs |
此处显示的示例并未确保主键值是唯一的。请参见维护唯一主键。
有关类构造函数和 DBConnectionContext 的详细信息,请参见:
有关 Java 同步逻辑的详细信息,请参见使用 Java 语言编写同步脚本。
有关 .NET 同步逻辑的详细信息,请参见使用 .NET 编写同步脚本。
有关直接行处理的详细信息,请参见直接行处理。
完整的 MobiLinkOrders 列表 (Java)
完整的 MobiLinkOrders 列表 (.NET)
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |