Example Listener Implementation

In this listener implementation of the EventCallBack interface, the listener checks the event type and, for AFTER_INSERT events, multiplies each column by a factor and then inserts into a foreign table.

SYS.ADD_LISTENER shows example code for deploying this listener to a table.

package testpackage;

public class EventCallBackListenerImpl implements EventCallback {

  private String foreigntablename;
  private int factor;
  private Connection conn;
  private PreparedStatement ps;

  public EventCallBackListenerImpl() {
    
  }
  
  public void close() throws SQLException {
    
  }

  // The init method expects the initStr to contain the name of the 
  // foreign table and the factor. Eg: “emp.ftable:3�?
  public void init(String initStr) throws SQLException {
    String[] arr = initStr.split(":");
    this.foreigntablename = arr[0];
    this.factor = Integer.parseInt(arr[1]);
    this.conn = getConnection();
    this.ps = this.conn.prepareStatement(
       "insert into "+this.foreigntablename+" values(?, ?, ?)");
  }

  public void onEvent(Event event) throws SQLException {
    if (if ((event.getType() == Type.AFTER_INSERT)) {
      int size = event.getNewRow().size();
      assert size == 3 : “expected the size to be 3�?;
      List<Objects> colValues = event.getNewRow();
      int one = ((Integer)colValues.get(0)).intValue() * this.factor;
      int two = ((Integer)colValues.get(1)).intValue() * this.factor;
      int three = ((Integer)colValues .get(2)).intValue() * this.factor;
      this.ps.setInt(1, one);
      this.ps.setInt(2, two);
      this.ps.setInt(3, three);
      this.ps.executeUpdate();
  }
}