Guest User

Untitled

a guest
Nov 20th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. import java.sql.ResultSet;
  2. import java.sql.SQLException;
  3. import java.sql.Statement;
  4. import java.util.Properties;
  5. import oracle.jdbc.OracleConnection;
  6. import oracle.jdbc.OracleDriver;
  7. import oracle.jdbc.OracleStatement;
  8. import oracle.jdbc.dcn.DatabaseChangeEvent;
  9. import oracle.jdbc.dcn.DatabaseChangeListener;
  10. import oracle.jdbc.dcn.DatabaseChangeRegistration;
  11.  
  12. public class OracleDCN {
  13. static final String USERNAME = "scott";
  14. static final String PASSWORD = "tiger";
  15. static String URL = "jdbc:oracle:thin:@localhost:1521:stingdev";
  16.  
  17. public static void main(String[] args) {
  18. OracleDCN oracleDCN = new OracleDCN();
  19. try {
  20. oracleDCN.run();
  21. } catch (Exception ex) {
  22. ex.printStackTrace();
  23. }
  24. }
  25.  
  26. private void run() throws Exception{
  27. OracleConnection conn = connect();
  28. Properties prop = new Properties();
  29. prop.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
  30. DatabaseChangeRegistration dcr = conn.registerDatabaseChangeNotification(prop);
  31.  
  32. try{
  33. dcr.addListener(new DatabaseChangeListener() {
  34.  
  35. public void onDatabaseChangeNotification(DatabaseChangeEvent dce) {
  36. System.out.println("Changed row id : "+dce.getTableChangeDescription()[0].getRowChangeDescription()[0].getRowid().stringValue());
  37. }
  38. });
  39.  
  40. Statement stmt = conn.createStatement();
  41. ((OracleStatement) stmt).setDatabaseChangeRegistration(dcr);
  42. ResultSet rs = stmt.executeQuery("select * from EXAMPLE where ID=1");
  43. while (rs.next()) {
  44. }
  45. rs.close();
  46. stmt.close();
  47. }catch(SQLException ex){
  48. if (conn != null)
  49. {
  50. conn.unregisterDatabaseChangeNotification(dcr);
  51. conn.close();
  52. }
  53. throw ex;
  54. }
  55. }
  56.  
  57. OracleConnection connect() throws SQLException {
  58. OracleDriver dr = new OracleDriver();
  59. Properties prop = new Properties();
  60. prop.setProperty("user", OracleDCN.USERNAME);
  61. prop.setProperty("password", OracleDCN.PASSWORD);
  62. return (OracleConnection) dr.connect(OracleDCN.URL, prop);
  63. }
  64. }
  65.  
  66. Changed row id : AAAFSzAAAAAAAG8AAA
  67. Changed row id : AAAFSzAAAAAAAG8AAA
  68. Changed row id : AAAFSzAAAAAAAG8AAA
  69. Changed row id : AAAFSzAAAAAAAG8AAA
  70.  
  71. select TABLE_NAME
  72. from USER_CHANGE_NOTIFICATION_REGS
  73.  
  74. SELECT REGID, CALLBACK FROM USER_CHANGE_NOTIFICATION_REGS
  75.  
  76. System.out.println("DCE : regId="+dce.getRegristrationId()+"; transactionId="+dce.getTransactionId());
  77.  
  78. public void onDatabaseChangeNotification(DatabaseChangeEvent dce) {
  79. if (e.getRegId() == dcr.getRegId())
  80. System.out.println("Changed row id : "+dce.getTableChangeDescription()[0].getRowChangeDescription()[0].getRowid().stringValue());
  81. }
  82.  
  83. public void onDatabaseChangeNotification(DatabaseChangeEvent dce) {
  84. if (dce.getRegId() == dcr.getRegId())
  85. System.out.println("Changed row id : "+dce.getTableChangeDescription()[0].getRowChangeDescription()[0].getRowid().stringValue());
  86. }
Add Comment
Please, Sign In to add comment