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

SQL Anywhere 12.0.1 (Deutsch) » MobiLink - Erste Orientierung » Praktische Einführungen in MobiLink » Praktische Einführung: Synchronisation mit Microsoft Excel » Lektion 5: Erstellen einer Java-Klasse mit direkter Zeilenbehandlung in MobiLink

 

Vollständige MobiLinkOrders-Auflistung (Java)

Die folgende Auflistung zeigt den vollständigen Quellcode der Java MobiLinkOrders-Klasse, die für diese praktische Einführung verwendet wird. Eine schrittweise Erklärung finden Sie unter Lektion 5: Erstellen einer Java-Klasse mit direkter Zeilenbehandlung in MobiLink.



import ianywhere.ml.script.*;
    import java.io.*;
    import java.sql.*;
    
    public class MobiLinkOrders {
    
    // Class level DBConnectionContext
    DBConnectionContext _cc;
    
    public MobiLinkOrders( DBConnectionContext cc )
        throws IOException, FileNotFoundException {
        // Declare a class-level DBConnectionContext
        _cc = cc;
    }

    //  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();
	}
    }
   
    public void SetDownload() throws SQLException, IOException {
        DownloadData download_d = _cc.getDownloadData();
        DownloadTableData download_td = download_d.getDownloadTableByName( "OrderComments" );
  
        // Prepared statement to compile upserts (inserts or updates).
        PreparedStatement download_upserts = download_td.getUpsertPreparedStatement();
  
        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();
	}
    }
}