title: Java - Oracle Database Change Notification
body: <p>I am trying to implement a event listener which can identify <strong>DATABASE CHANGE NOTIFICATION</strong> (Oracle). According to the <strong><a href="http://jirawong.wordpress.com/2013/03/27/oracle-database-change-notification-jdbc-style-example/" rel="nofollow">reference website</a></strong>, it said that event will triggle and print ROW_ID when something change in EXAMPLE table. I want this project running, and it should give me a message "give me something!" if I manually insert/update data in Database. However, it is my understanding that this code will terminate no matter what since there is no infinite loop that can be interrupted by the event. Please correct me if I am wrong. </p>
  
  <p><strong>Additional Question]</strong><br/>
  By setting <strong>OracleConnection.DCN_NOTIFY_ROWIDS</strong> as true, it will notify every event including inserting, updating, deleting. Am I correct? I was confused with the meaning of <strong>"Database change events will include row-level details, such as operation type and ROWID"</strong> </p>
  
  <p>my code:</p>
  
  <pre><code>import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  import java.util.Properties;
  import oracle.jdbc.OracleConnection;
  import oracle.jdbc.OracleDriver;
  import oracle.jdbc.OracleStatement;
  import oracle.jdbc.dcn.DatabaseChangeEvent;
  import oracle.jdbc.dcn.DatabaseChangeListener;
  import oracle.jdbc.dcn.DatabaseChangeRegistration;
  
  public class DBTest {
      static final String USERNAME = "username";
      static final String PASSWORD = "password";
      static String URL = "jdbc:oracle:thin:@url:port/name";
  
      public static void main(String[] args) {
          DBTest oracleDCN = new DBTest();
          try {
              oracleDCN.run();
          } catch (Exception ex) {
              ex.printStackTrace();
          }
      }
  
      private void run() throws Exception {
          OracleConnection conn = connect();
          Properties prop = new Properties();
          prop.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
          DatabaseChangeRegistration dcr = conn.registerDatabaseChangeNotification(prop);
  
          try {
              dcr.addListener(new DatabaseChangeListener() {
                  public void onDatabaseChangeNotification(DatabaseChangeEvent dce) {
                      System.out.println("GIVE ME SOMETHING!");
                  }
              });
              //conn.unregisterDatabaseChangeNotification(dcr);
              Statement stmt = conn.createStatement();
              ((OracleStatement) stmt).setDatabaseChangeRegistration(dcr);
              ResultSet rs = stmt.executeQuery("select * from Schema.T_TEST");
              while (rs.next()) {
              }
              rs.close();
              stmt.close();
          } catch (SQLException ex) {
              if (conn != null) {
                  conn.unregisterDatabaseChangeNotification(dcr);
                  conn.close();
              }
              throw ex;
          }
      }
  
      OracleConnection connect() throws SQLException {
          OracleDriver dr = new OracleDriver();
          Properties prop = new Properties();
          prop.setProperty("user", DBTest.USERNAME);
          prop.setProperty("password", DBTest.PASSWORD);
          return (OracleConnection) dr.connect(DBTest.URL, prop);
      }
  }
  </code></pre>
  
  <p>More details can be found in the <a href="http://jirawong.wordpress.com/2013/03/27/oracle-database-change-notification-jdbc-style-example/" rel="nofollow">reference website</a></p>