赞
踩
- create table nj_tmp_tmp(id number, val number);
-
-
- insert into nj_tmp_tmp values(1,100);
- insert into nj_tmp_tmp values(2,200);
- insert into nj_tmp_tmp values(3,300);
- commit;
grant change notification to scott;
DatabaseChangeRegistration dcr = conn.registerDatabaseChangeNotification(prop);
statement.executeQuery("select * from nj_tmp_tmp");
- dcr.addListener(new DatabaseChangeListener(){
-
-
-
-
- @Override
- public void onDatabaseChangeNotification(DatabaseChangeEvent arg0) {
- TableChangeDescription[] td=arg0.getTableChangeDescription();
- for(TableChangeDescription t:td){
- for(RowChangeDescription r:t.getRowChangeDescription()){
- ROWID rowid = r.getRowid();
- System.out.println("--------"+new String(rowid.getBytes()));
- }
- }
- }
-
- });
- public class DBChangeNotification
- {
- static final String USERNAME= "*****";//数据库用户名
- static final String PASSWORD= "*****";//数据库密码
- static String URL;
-
- public static void main(String[] argv)
- {
- URL = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)))";
- try
- {
- OracleConnection conn = connect();
- Properties prop = new Properties();
- prop.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS,"true");
- prop.setProperty(OracleConnection.NTF_TIMEOUT, "0");
- prop.setProperty(OracleConnection.DCN_IGNORE_DELETEOP, "true");
- prop.setProperty(OracleConnection.DCN_IGNORE_UPDATEOP,"true");
- DatabaseChangeRegistration dcr = conn.registerDatabaseChangeNotification(prop);
-
- try
- {
- dcr.addListener(new DatabaseChangeListener(){
-
-
- @Override
- public void onDatabaseChangeNotification(DatabaseChangeEvent arg0) {
- TableChangeDescription[] td=arg0.getTableChangeDescription();
- for(TableChangeDescription t:td){
- for(RowChangeDescription r:t.getRowChangeDescription()){
- ROWID rowid = r.getRowid();
- System.out.println("--------"+new String(rowid.getBytes()));
- }
- }
- }
-
- });
- Statement stmt = conn.createStatement();
- ((OracleStatement)stmt).setDatabaseChangeRegistration(dcr);
- ResultSet rs = stmt.executeQuery("select * from nj_tmp_tmp");
- String[] tableNames = dcr.getTables();
- for(int i=0;i<tableNames.length;i++)
- System.out.println(tableNames[i]+" is part of the registration.");
- rs.close();
- stmt.close();
- }
- catch(SQLException ex)
- {
- if(conn != null)
- conn.unregisterDatabaseChangeNotification(dcr);
- throw ex;
- }
- finally
- {
- try
- {
- conn.close();
- }
- catch(Exception innerex){ innerex.printStackTrace(); }
- }
- }
- catch(SQLException mainSQLException )
- {
- mainSQLException.printStackTrace();
- }
- }
-
- /**
- * Creates a connection the database.
- */
- static OracleConnection connect() throws SQLException
- {
- OracleDriver dr = new OracleDriver();
- Properties prop = new Properties();
- prop.setProperty("user",DBChangeNotification.USERNAME);
- prop.setProperty("password",DBChangeNotification.PASSWORD);
- return (OracleConnection)dr.connect(DBChangeNotification.URL,prop);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。