在本课中,您将使用直接行处理来处理客户端数据库的 OrderComments 表中的行。为直接行处理添加以下方法:
GetUpload 将此方法用于 handle_UploadData 事件。GetUpload 将上载的注释写入 Excel 工作表 order_central.xls。
SetDownload 将此方法用于 handle_DownloadData 事件。SetDownload 检索存储在 Excel 工作表 order_central.xls 中的数据并将其发送到远程客户端。
以下过程介绍如何创建 Java 类,其中包括用于处理的方法。有关完整列表,请参见完整的 MobiLinkOrders 代码列表 (Java)。
创建名为 MobiLinkOrders 的类。
在文本编辑器或开发环境中键入以下代码。
import ianywhere.ml.script.*; import java.io.*; import java.sql.*; public class MobiLinkOrders { // ... } |
声明一个类级别 DBConnectionContext 实例。
DBConnectionContext _cc; |
MobiLink 服务器会将 DBConnectionContext 实例传递到类构造函数。DBConnectionContext 封装有关与 MobiLink 统一数据库的当前连接的信息。
创建类构造函数。
类构造函数设置类级别 DBConnectionContext 实例。
在文本编辑器或开发环境中键入以下代码。
public MobiLinkOrders( DBConnectionContext cc ) { _cc = cc; } |
编写 GetUpload() 方法。
GetUpload 方法获得表示 OrderComments 表的 UploadedTableData 类实例。OrderComments 表包含由远程销售雇员进行的特殊注释。您将在第 6 课:建立 MobiLink 客户端中创建该表。UploadedTableData getInserts 方法返回新订单注释的结果集。
在文本编辑器或开发环境中键入以下代码。
// 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(); try { // connect to the excel worksheet through ODBC Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection( "jdbc:odbc:excel_datasource" ); while( insertResultSet.next() ) { // get order comments int _commentID = insertResultSet.getInt("comment_id"); int _orderID = insertResultSet.getInt("order_id"); String _specialComments = insertResultSet.getString("order_comment"); // execute an insert statement to add the order comment to the worksheet Statement st = con.createStatement(); st.executeQuery( "insert into [order_sheet$]" + "(order_id, comment_id, order_comment) VALUES (" + Integer.toString(_orderID) + ", " + Integer.toString(_commentID) + ", '" + _specialComments + "')"); st.close(); } con.close(); } catch(Exception ex) { System.err.print("Exception: "); System.err.println(ex.getMessage()); } insertResultSet.close(); } |
编写 SetDownload 方法:
获得表示 OrderComments 表的类实例。
使用 DBConnectionContext getDownloadData 方法可获得 DownloadData 实例。使用 DownloadData getDownloadTableByName 方法可为 OrderComments 表返回 DownloadTableData 实例。
在文本编辑器或开发环境中键入以下代码。
DownloadData download_d = _cc.getDownloadData(); DownloadTableData download_td = download_d.getDownloadTableByName( "OrderComments" ); |
您将在第 6 课:建立 MobiLink 客户端中于远程数据库上创建此表。
获得允许您将插入或更新操作添加到下载的准备好的语句或者 IDBCommand。
使用 DownloadTableData getUpsertPreparedStatement 方法返回一个 java.sql.PreparedStatement 实例。
在文本编辑器或开发环境中键入以下代码。
PreparedStatement download_upserts = download_td.getUpsertPreparedStatement(); |
设置每行的下载数据。
以下示例遍历 order_central.xls 工作表并将数据添加到 MobiLink 下载。
在文本编辑器或开发环境中键入以下代码。
try { // connect to the excel worksheet through ODBC Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection( "jdbc:odbc:excel_datasource" ); // retrieve all the rows in the worksheet Statement st = con.createStatement(); ResultSet Excel_rs = st.executeQuery( "select * from [order_sheet$]" ); while (Excel_rs.next()) { // retrieve the row data int Excel_comment_id = Excel_rs.getInt(1); int Excel_order_id = Excel_rs.getInt(2); String Excel_comment = Excel_rs.getString(3); // add the Excel data to the MobiLink download. download_upserts.setInt( 1, Excel_comment_id ); download_upserts.setInt( 2, Excel_order_id ); download_upserts.setString( 3, Excel_comment ); download_upserts.executeUpdate(); } // close the excel result set, statement, and connection. Excel_rs.close(); st.close(); con.close(); } catch(Exception ex) { System.err.print("Exception: "); System.err.println(ex.getMessage()); } |
关闭用来将插入操作或更新操作添加到下载的准备好的语句。
在文本编辑器或开发环境中键入以下代码。
download_upserts.close(); |
将 Java 代码在工作目录 c:\MLobjexcel 中另存为 MobiLinkOrders.java。
编译您的类文件。
浏览到包含 Java 源文件的目录。
编译 MobiLinkOrders 时引用面向 Java 的 MobiLink 服务器 API 库。
需要引用位于 install-dir\Java 中的 mlscript.jar。运行以下命令来编译 Java 类,用 SQL Anywhere 11 目录替换 c:\Program Files\SQL Anywhere 11\:
javac -classpath "c:\Program Files\SQL Anywhere 11\java\mlscript.jar" MobiLinkOrders.java |
有关同步逻辑的详细信息,请参见使用 Java 语言编写同步脚本。
有关直接行处理的详细信息,请参见直接行处理。
完整的 MobiLinkOrders 代码列表 (Java)
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |