Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 OracleDCN {
- static final String USERNAME = "scott";
- static final String PASSWORD = "tiger";
- static String URL = "jdbc:oracle:thin:@localhost:1521:stingdev";
- public static void main(String[] args) {
- OracleDCN oracleDCN = new OracleDCN();
- 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("Changed row id : "+dce.getTableChangeDescription()[0].getRowChangeDescription()[0].getRowid().stringValue());
- }
- });
- Statement stmt = conn.createStatement();
- ((OracleStatement) stmt).setDatabaseChangeRegistration(dcr);
- ResultSet rs = stmt.executeQuery("select * from EXAMPLE where ID=1");
- 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", OracleDCN.USERNAME);
- prop.setProperty("password", OracleDCN.PASSWORD);
- return (OracleConnection) dr.connect(OracleDCN.URL, prop);
- }
- }
- Changed row id : AAAFSzAAAAAAAG8AAA
- Changed row id : AAAFSzAAAAAAAG8AAA
- Changed row id : AAAFSzAAAAAAAG8AAA
- Changed row id : AAAFSzAAAAAAAG8AAA
- select TABLE_NAME
- from USER_CHANGE_NOTIFICATION_REGS
- SELECT REGID, CALLBACK FROM USER_CHANGE_NOTIFICATION_REGS
- System.out.println("DCE : regId="+dce.getRegristrationId()+"; transactionId="+dce.getTransactionId());
- public void onDatabaseChangeNotification(DatabaseChangeEvent dce) {
- if (e.getRegId() == dcr.getRegId())
- System.out.println("Changed row id : "+dce.getTableChangeDescription()[0].getRowChangeDescription()[0].getRowid().stringValue());
- }
- public void onDatabaseChangeNotification(DatabaseChangeEvent dce) {
- if (dce.getRegId() == dcr.getRegId())
- System.out.println("Changed row id : "+dce.getTableChangeDescription()[0].getRowChangeDescription()[0].getRowid().stringValue());
- }
Add Comment
Please, Sign In to add comment