在本课中,您将使用直接行处理来处理客户端数据库的 OrderComments 表中的行。
上下文和注释
在本课中,您将为直接行处理添加以下方法:
GetUpload 将此方法用于 handle_UploadData 事件。GetUpload 将上载的注释写入 excel 工作表 order_central.xlsx。
SetDownload 将此方法用于 handle_DownloadData 事件。SetDownload 检索存储在 Excel 工作表 order_central.xlsx 中的数据并将其发送到远程客户端。
以下过程介绍如何创建 Java 类,其中包括用于处理的方法。 有关完整列表,请参见完整的 MobiLinkOrders 代码列表 (Java)。
开始编写名为 MobiLinkOrders 的新类。
编写以下代码:
import ianywhere.ml.script.*; import java.io.*; import java.sql.*; public class MobiLinkOrders { |
声明一个类级别 DBConnectionContext 实例。
附加以下代码:
// Class level DBConnectionContext DBConnectionContext _cc; |
MobiLink 服务器会将 DBConnectionContext 实例传递到类构造函数。DBConnectionContext 封装有关与 MobiLink 统一数据库的当前连接的信息。
创建类构造函数。
类构造函数设置类级别 DBConnectionContext 实例。
附加以下代码:
public MobiLinkOrders( DBConnectionContext cc ) throws IOException, FileNotFoundException { // Declare a class-level DBConnectionContext _cc = cc; } |
编写 GetUpload 方法。
GetUpload 方法获得表示 OrderComments 表的 UploadedTableData 类实例。OrderComments 表包含由远程销售雇员进行的特殊注释。您将在下一课中创建此表。
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 PreparedStatement st = con.prepareStatement("INSERT INTO [order_sheet$]" + "(order_id, comment_id, order_comment) VALUES (?,?,?)" ); st.setString( 1, Integer.toString(_orderID) ); st.setString( 2, Integer.toString(_commentID) ); st.setString( 3, _specialComments ); st.executeUpdate(); st.close(); } con.close(); } catch(Exception ex) { System.err.print("Exception: "); System.err.println(ex.getMessage()); } finally { insertResultSet.close(); } } |
编写 SetDownload 方法:
获得表示 OrderComments 表的类实例。
使用 DBConnectionContext getDownloadData 方法可获得 DownloadData 实例。使用 DownloadData getDownloadTableByName 方法可为 OrderComments 表返回 DownloadTableData 实例。
附加以下代码:
public void SetDownload() throws SQLException, IOException { DownloadData download_d = _cc.getDownloadData(); DownloadTableData download_td = download_d.getDownloadTableByName( "OrderComments" ); |
您将在第 7 课:建立 MobiLink 客户端数据库中于远程数据库上创建此表。
获得允许您将插入或更新操作添加到下载的准备好的语句或者 IDbCommand。
使用 DownloadTableData getUpsertPreparedStatement 方法返回一个 java.sql.PreparedStatement 实例。
附加以下代码:
// Prepared statement to compile upserts (inserts or updates). PreparedStatement download_upserts = download_td.getUpsertPreparedStatement(); |
设置每行的下载数据。
以下代码遍历 order_central.xlsx 工作表并将数据添加到 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()); } |
关闭用来将插入操作或更新操作添加到下载中的预准备语句,结束方法和类。
附加以下代码:
finally { download_upserts.close(); } } } |
将 Java 代码在工作目录 c:\MLobjexcel 中另存为 MobiLinkOrders.java。
要验证 MobiLinkOrders.java 中的代码,请参见完整的 MobiLinkOrders 代码列表 (Java)。
编译您的类文件。
浏览到包含 Java 源文件的目录。
编译 MobiLinkOrders,它将引用面向 Java 的 MobiLink 服务器 API 库。
您需要引用位于 %SQLANY16%\Java 中的 mlscript.jar。
运行以下命令,将 C:\Program Files\SQL Anywhere 16\ 替换为 SQL Anywhere 16 目录:
javac -classpath "C:\Program Files\SQL Anywhere 16\java\mlscript.jar" MobiLinkOrders.java |
完整的 MobiLinkOrders 代码列表 (Java)
![]() |
使用DocCommentXchange讨论此页。
|
版权 © 2013, SAP 股份公司或其关联公司. - SAP Sybase SQL Anywhere 16.0 |