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

SQL Anywhere 12.0.0 (中文) » MobiLink - 入门 » MobiLink 教程 » 教程:直接行处理简介 » 第 4 课:创建 Java 或 .NET 类进行 MobiLink 直接行处理

 

完整的 MobiLinkOrders 代码列表 (Java)

以下是适用于 Java 直接行处理的完整的 MobiLinkOrders 列表。有关分步说明,请参见第 4 课:创建 Java 或 .NET 类进行 MobiLink 直接行处理



import ianywhere.ml.script.*;
import java.io.*;
import java.sql.*;
    
public class MobiLinkOrders {
    
    // Class level DBConnectionContext
    DBConnectionContext _cc;
        
    // Java objects for file i/o
    FileWriter my_writer;
    BufferedReader my_reader;

    public MobiLinkOrders( DBConnectionContext cc )
        throws IOException, FileNotFoundException
    {
        // Declare a class-level DBConnectionContext
        _cc = cc; 
    }
    
    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();
    }
        
    public void SetDownload() 
        throws SQLException, IOException
    {
        DownloadData download_d = _cc.getDownloadData();
         
        DownloadTableData download_td = download_d.getDownloadTableByName( "OrderComments" );
          
        PreparedStatement update_ps = download_td.getUpsertPreparedStatement();
        // 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();
        }
        update_ps.close();
    }
    
    public void EndDownload() 
        throws IOException
    {
        // Close i/o resources
        if (my_reader != null) my_reader.close();
        if (my_writer != null) my_writer.close();
    }
}