このレッスンは、受講者がこれまでのすべてのレッスンを終了していることを前提としています。 レッスン 1:Excel ワークシートの設定を参照してください。
このレッスンでは、ダイレクトローハンドリングを使用して、クライアントデータベース内の OrderComments テーブルのローを処理します。ダイレクトローハンドリング用に次のメソッドを追加します。
GetUpload このメソッドは handle_UploadData イベントに使用します。GetUpload では、アップロードされたコメントを order_central.xlsx という Excel ワークシートに書き込みます。
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; |
Mobile Link サーバーによって DBConnectionContext のインスタンスがクラスコンストラクターに渡されます。DBConnectionContext には、Mobile Link 統合データベースとの現在の接続に関する情報がカプセル化されます。
クラスコンストラクターを作成します。
クラスコンストラクターが、クラスレベルの 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:Mobile Link クライアントデータベースの設定でリモートデータベースに作成します。
準備文または IDbCommand を取得します。これを使用すると、ダウンロードに挿入操作や更新操作を追加できます。
DownloadTableData の getUpsertPreparedStatement メソッドを使用して java.sql.PreparedStatement のインスタンスを返します。
次のコードを追加します。
// Prepared statement to compile upserts (inserts or updates). PreparedStatement download_upserts = download_td.getUpsertPreparedStatement(); |
各ローのダウンロードデータを設定します。
次のコードでは、order_central.xlsx ワークシートを参照して、Mobile Link ダウンロードにデータを追加しています。
次のコードを追加します。
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 コードを MobiLinkOrders.java という名前で作業ディレクトリ c:\MLobjexcel に保存します。
MobiLinkOrders.java のコードを検証する場合は、MobiLinkOrders コードの全リスト (Java)を参照してください。
クラスファイルをコンパイルします
Java のソースファイルが含まれるディレクトリに移動します。
Java 用の Mobile Link サーバー API ライブラリを参照する MobiLinkOrders をコンパイルします。
%SQLANY12%\Java にある mlscript.jar を参照する必要があります。
次のコマンドを実行して、C:\Program Files\SQL Anywhere 12\ を SQL Anywhere 12 ディレクトリに置き換えます。
javac -classpath "C:\Program Files\SQL Anywhere 12\java\mlscript.jar" MobiLinkOrders.java |
MobiLinkOrders コードの全リスト (Java)
![]() |
DocCommentXchange で意見交換できます
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |