Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.1 » Mobile Link クイックスタート » Mobile Link チュートリアル » チュートリアル:Microsoft Excel との同期

 

レッスン 5:Mobile Link のダイレクトローハンドリングのための Java クラスの作成

このレッスンは、受講者がこれまでのすべてのレッスンを終了していることを前提としています。 レッスン 1:Excel ワークシートの設定を参照してください。

このレッスンでは、ダイレクトローハンドリングを使用して、クライアントデータベース内の OrderComments テーブルのローを処理します。ダイレクトローハンドリング用に次のメソッドを追加します。

  • GetUpload   このメソッドは handle_UploadData イベントに使用します。GetUpload では、アップロードされたコメントを order_central.xlsx という Excel ワークシートに書き込みます。

  • SetDownload   このメソッドは handle_DownloadData イベントに使用します。SetDownload は、Excel ワークシート order_central.xlsx に格納されたデータを取り出し、リモートクライアントに送信します。

次の手順では、処理用メソッドを含む Java のクラスを作成する方法を示します。 完全なリストについては、MobiLinkOrders コードの全リスト (Java)を参照してください。

 ♦ ダウンロード専用のダイレクトローハンドリング用の Java のクラスの作成
  1. MobiLinkOrders という新しいクラスの作成を開始します。

    次のコードを作成します。

    import ianywhere.ml.script.*;
    import java.io.*;
    import java.sql.*;
    
    public class MobiLinkOrders {
    
  2. クラスレベルの DBConnectionContext インスタンスを宣言します。

    次のコードを追加します。

        // Class level DBConnectionContext
        DBConnectionContext _cc;

    Mobile Link サーバーによって DBConnectionContext のインスタンスがクラスコンストラクターに渡されます。DBConnectionContext には、Mobile Link 統合データベースとの現在の接続に関する情報がカプセル化されます。

  3. クラスコンストラクターを作成します。

    クラスコンストラクターが、クラスレベルの DBConnectionContext インスタンスを設定します。

    次のコードを追加します。

        public MobiLinkOrders( DBConnectionContext cc )
            throws IOException, FileNotFoundException {
            // Declare a class-level DBConnectionContext
            _cc = cc;
        }
    
  4. 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();
    	}
        }
  5. SetDownload メソッドを作成します。

    1. 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 クライアントデータベースの設定でリモートデータベースに作成します。

    2. 準備文または IDbCommand を取得します。これを使用すると、ダウンロードに挿入操作や更新操作を追加できます。

      DownloadTableData の getUpsertPreparedStatement メソッドを使用して java.sql.PreparedStatement のインスタンスを返します。

      次のコードを追加します。

              // Prepared statement to compile upserts (inserts or updates).
              PreparedStatement download_upserts = download_td.getUpsertPreparedStatement();
    3. 各ローのダウンロードデータを設定します。

      次のコードでは、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());
              } 
      
    4. ダウンロードに挿入操作または更新操作を追加する準備文を終了し、メソッドとクラスを終了します。

      次のコードを追加します。

              finally {
      	    download_upserts.close();
      	}
          }
      }
  6. Java コードを MobiLinkOrders.java という名前で作業ディレクトリ c:\MLobjexcel に保存します。

    MobiLinkOrders.java のコードを検証する場合は、MobiLinkOrders コードの全リスト (Java)を参照してください。

  7. クラスファイルをコンパイルします

    1. Java のソースファイルが含まれるディレクトリに移動します。

    2. 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
  8. レッスン 6:Mobile Link サーバーの起動に進みます。

 参照

MobiLinkOrders コードの全リスト (Java)