1. Home
  2. Knowledge Base
  3. Sample External Object (Java) Source Code

Sample External Object (Java) Source Code

Sample Source Code

package MRCWORKLIB;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

import com.mrc.dbo.MrcConnection;
/**
 * Delete Order # from MRCWORKLIB.ORDDET
 * 2011-09-22
 * @author RH
 *
 * 2. Define external object
 *
 * Class Name: MRCWORKLIB.DELDETAIL
 * Method Name: run
 * Then define one object parm to pass Order number
 * 3. Use this object in your servlet application
 *
 */
public class DELDETAIL {

  private Logger log = Logger.getLogger(this.getClass());

  /****************************************************
   * Delete from MRCWORKLIB.ORDDET
   ****************************************************/
  public void run(String[] str) {

    if (str.length < 1) {
      log.info("Must pass Order #.");
      return;
    }
    int ordno = 0;
    try {
      ordno = Integer.parseInt(str[0]);
    } catch (Exception e) {
      log.info("Invalid Order # passed.");
      return;
    }
    String sql = "DELETE FROM MRCWORKLIB.ORDERDET where ONUM=" + ordno;
    Connection conn = null;
    try {
      //Get connection configured in m-power/mrcjava/web-inf/classes/spring-config.xml                         

      conn = MrcConnection.getConnection("as400_remote1");
      Statement stmt = conn.createStatement();
      int deleted = stmt.executeUpdate(sql);
      log.info(deleted + " records deleted.");
      stmt.close();
    } catch (SQLException sqle) {
      log.error("sql = " + sql, sqle);
    } finally {
      try {
        conn.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  } /****************************************************          * Test          ****************************************************/
  public static void main(String[] str) {
    DELDETAIL delete = new DELDETAIL();
    String[] str0 = {
      "2"
    };
    delete.run(str0);
  }
}
Updated on March 28, 2023

Was this article helpful?

Need Support?
Can’t find the answer you’re looking for? Don’t worry we’re here to help!
Contact Support