在本课中,您将使用直接行处理来处理客户端数据库的 OrderComments 表中的行。为直接行处理添加以下方法:
GetUpload 将此方法用于 handle_UploadData 事件。GetUpload 将上载的注释写入名为 orderComments.txt 的文件中。
SetDownload 将此方法用于 handle_DownloadData 事件。SetDownload 使用 orderResponses.txt 文件来下载对远程客户端的响应。
EndDownload 此方法可用于 end_download 事件。EndDownload 可释放内存资源。
以下过程介绍如何创建 Java 或 .NET 类,其中包括用于处理的方法。有关完整列表,请参见完整的 MobiLinkOrders 代码列表 (Java)或完整的 MobiLinkOrders 代码列表 (.NET)。
使用 Java 或 .NET 创建名为 MobiLinkOrders 的类。
对于 Java,使用以下代码:
import ianywhere.ml.script.*; import java.io.*; import java.sql.*; public class MobiLinkOrders { |
对于 .NET,使用以下代码:
using iAnywhere.MobiLink.Script; using System.IO; using System.Data; using System.Text; public class MobiLinkOrders { |
声明一个类级别 DBConnectionContext 实例。
对于 Java,使用以下代码:
// Class level DBConnectionContext DBConnectionContext _cc; |
对于 .NET,使用以下代码:
// Class level DBConnectionContext private DBConnectionContext _cc = null; |
MobiLink 服务器会将 DBConnectionContext 实例传递到类构造函数。DBConnectionContext 封装有关与 MobiLink 统一数据库的当前连接的信息。
声明用于文件输入和输出的对象。
对于 Java,声明 java.io.FileWriter 和 java.io.BufferedReader,如下所示:
// Java objects for file i/o FileWriter my_writer; BufferedReader my_reader; |
对于 .NET,声明 StreamWriter 和 StreamReader,如下所示:
// Instances for file I/O private static StreamWriter my_writer = null; private static StreamReader my_reader = null; |
创建类构造函数。
类构造函数设置类级别 DBConnectionContext 实例。
对于 Java,使用以下代码:
public MobiLinkOrders( DBConnectionContext cc ) throws IOException, FileNotFoundException { // Declare a class-level DBConnectionContext _cc = cc; } |
对于 .NET,使用以下代码:
public MobiLinkOrders(DBConnectionContext cc) { _cc = cc; } |
编写 GetUpload 方法
GetUpload 方法获得表示 OrderComments 表的 UploadedTableData 类实例。OrderComments 表包含由远程销售雇员进行的特殊注释。您将在第 6 课:建立 MobiLink 客户端数据库中创建该表。UploadedTableData getInserts 方法返回新订单注释的结果集。writeOrderComment 方法将结果集中的每行写出至文本文件。
对于 Java,使用以下代码:
public void writeOrderComment( int _commentID, int _orderID, String _comments ) throws IOException { if (my_writer == null) // A FileWriter for writing order comments 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(); } // 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(); } |
对于 .NET,使用以下代码:
public void WriteOrderComment(int comment_id, int order_id, string comments) { if (my_writer == null) { my_writer = new StreamWriter("c:\\MLdirect\\orderComments.txt"); } my_writer.WriteLine("{0}\t{1}\t{2}", comment_id, order_id, comments); my_writer.Flush(); } // 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 uploaded 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 at 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,使用以下代码:
public void SetDownload() throws SQLException, IOException { DownloadData download_d = _cc.getDownloadData(); DownloadTableData download_td = download_d.getDownloadTableByName( "OrderComments" ); |
对于 .NET,使用以下代码:
private const string read_file_path = "c:\\MLdirect\\orderResponses.txt"; // Method for the handle_DownloadData synchronization event public void SetDownload() { if ((my_reader == null) && !File.Exists(read_file_path)) { System.Console.Out.Write("There is no file to read."); return; } DownloadTableData comments_for_download = _cc.GetDownloadData().GetDownloadTableByName("OrderComments"); |
您将在第 6 课:建立 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(); |
设置每行的下载数据。
此代码遍历 orderResponses.txt 并将数据添加到 MobiLink 下载。
对于 Java,使用以下代码:
// A BufferedReader for reading in responses if (my_reader == null) my_reader = new BufferedReader(new FileReader( "c:\\MLdirect\\orderResponses.txt")); // Get the next line from orderResponses String commentLine; commentLine = my_reader.readLine(); // Send comment responses down to clients 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[1]); 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 commentLine = my_reader.readLine(); } |
对于 .NET,使用以下代码:
if (my_reader == null) { my_reader = new StreamReader(read_file_path); } 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 IDataParameter ((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。此对象会在下载结束时自动取消。
编写 EndDownload 方法。
此方法处理 end_download 连接事件并提供释放资源的机会。
对于 Java,使用以下代码:
public void EndDownload() throws IOException { // Close 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; } if (my_reader != null) { my_reader.Close(); my_reader = null; } } } |
保存您的代码。
对于 Java,在工作目录 c:\MLdirect 中将代码另存为 MobiLinkOrders.java。
对于 .NET,在工作目录 c:\MLdirect 中将代码另存为 MobiLinkOrders.cs。
请参见完整的 MobiLinkOrders 代码列表 (Java)或完整的 MobiLinkOrders 代码列表 (.NET),以此验证代码。
编译您的类文件。
浏览到包含 Java 或 .NET 源文件的目录。
编译 MobiLinkOrders 并引用面向 Java 或 .NET 的 MobiLink 服务器 API 库。
对于 Java,需要引用位于 install-dir\java 中的 mlscript.jar。
对于 Java,运行以下命令,将 C:\Program Files\SQL Anywhere 12\ 替换为 SQL Anywhere 12 目录:
javac -classpath "C:\Program Files\SQL Anywhere 12\java\mlscript.jar" MobiLinkOrders.java |
对于 .NET,运行以下命令,将 C:\Program Files\SQL Anywhere 12\ 替换为 SQL Anywhere 12 目录:
csc /out:MobiLinkServerCode.dll /target:library /reference:"C:\Program Files\SQL Anywhere 12\Assembly\v2\iAnywhere.MobiLink.Script.dll" MobiLinkOrders.cs |
此处显示的示例并未确保主键值是唯一的。请参见维护唯一主键。
完整的 MobiLinkOrders 代码列表 (Java)
完整的 MobiLinkOrders 代码列表 (.NET)
![]() |
使用DocCommentXchange 讨论此页。
|
版权 © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |