Thursday, 26 November 2015

To connect to External database below is the sample code:


public static void renameCustAccountsinRetailDB(CustAccount _oldName = "",
                                                CustAccount _newName = "" )
{
    #OCCRetryCount
    LoginProperty                 loginProperty;
    OdbcConnection                odbcConnection;
    Statement                     statement;
    ResultSet                     resultSet;
    str                           sql;
    SqlStatementExecutePermission perm;
    Map           mapRetailTables  =  new Map(Types::String,Types::String);
    MapEnumerator enumerator;

    mapRetailTables.insert('POSTRANSACTION','CUSTACCOUNT');
    mapRetailTables.insert('RETAILPUBRETAILCHANNELTABLE','DEFAULTCUSTACCOUNT');
    mapRetailTables.insert('RETAILCOUPONISSUERTABLE','CUSTACCOUNT');
    mapRetailTables.insert('RETAILCHANNELTABLE','DEFAULTCUSTACCOUNT');
    mapRetailTables.insert('RETAILPUBRETAILSTORETABLE','USEDEFAULTCUSTACCOUNT');
    mapRetailTables.insert('RETAILTRANSACTIONLOYALTYREWARDPOINTTRANS','CUSTACCOUNT');
    mapRetailTables.insert('RETAILTRANSACTIONTABLE','CUSTACCOUNT');
    mapRetailTables.insert('RETAILSTORETABLE','USEDEFAULTCUSTACCOUNT');
    mapRetailTables.insert('RETAILTRANSACTIONSALESTRANS','CUSTACCOUNT');
    mapRetailTables.insert('RETAILCUSTAFFILIATION','CUSTACCOUNTNUM');

    //ODBC Conection
    //Set information on the ODBC
    loginProperty = new LoginProperty();
    loginProperty.setDSN("RetailDSN");
    loginProperty.setDatabase("SICCSTOREDB2");
    try
    {
        //Create connection to external DB
        odbcConnection = new OdbcConnection(loginProperty);

        if (odbcConnection)
        {

            while (enumerator.moveNext())
            {
                sql = "update "+ enumerator.currentKey() +" set "+ enumerator.currentValue()+" ="+ _newName+" where "+enumerator.currentValue()+" ="+ _oldName;

                //assert permission for sql string
                perm = new SqlStatementExecutePermission(sql);

                perm.assert();

                //Prepare statement
                statement = odbcConnection.createStatement();
                resultSet = statement.executeQuery(sql);
            }
            resultSet.close();
            statement.close();
        }
        else
        {
            error("Failed to log on to the database");
        }
    }
    catch
    {
        throw error(" Please try again");
    }
}

No comments:

Post a Comment