第一次尝试做这样的事情,我不确定我错过了什么?
我的代码:
package SQLOCpackage;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleStatement;
import oracle.jdbc.dcn.DatabaseChangeEvent;
import oracle.jdbc.dcn.DatabaseChangeListener;
import oracle.jdbc.dcn.DatabaseChangeRegistration;
import oracle.jdbc.dcn.RowChangeDescription;
import oracle.jdbc.dcn.TableChangeDescription;
@SuppressWarnings("serial")
public class SQLONframe extends JFrame {
String URL = "jdbc:oracle:thin:@xxxxx.xxxxx.xx.xxxx:1521:xxxxx";
Properties prop;
private JPanel contentPane;
static String PW = "xxxxxxxx";
static String UN = "xxxxxxxx";
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SQLONframe frame = new SQLONframe();
frame.setVisible(true);
SQLONframe dcn = new SQLONframe();
try {
dcn.prop = new Properties();
dcn.prop.setProperty("user", UN);
dcn.prop.setProperty("password", PW);
dcn.run();
}
catch(Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SQLONframe() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
void run() throws SQLException {
OracleConnection conn = (OracleConnection)DriverManager.getConnection(URL,prop);
DatabaseChangeRegistration dcr = null;
Properties prop = new Properties();
prop.setProperty(OracleConnection.DCN_QUERY_CHANGE_NOTIFICATION,"true");
try {
dcr = conn.registerDatabaseChangeNotification(prop);
dcnListener list = new dcnListener(this);
dcr.addListener(list);
Statement stmt = conn.createStatement();
((OracleStatement)stmt).setDatabaseChangeRegistration(dcr);
ResultSet rs = stmt.executeQuery("select script_name, current_status, Issues_found_during_run, Testers, tools from ALLDATA WHERE ID = 1");
while (rs.next())
{
}
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(Exception e) {
//clean up our registration
if(conn != null)
conn.unregisterDatabaseChangeNotification(dcr);
e.printStackTrace();
}
finally {
try {
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
}
try {
Thread.currentThread().join();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
OracleConnection conn3 = (OracleConnection)DriverManager.getConnection(URL,prop);
conn3.unregisterDatabaseChangeNotification(dcr);
conn3.close();
}
}
class dcnListener implements DatabaseChangeListener {
SQLONframe dcn;
dcnListener(SQLONframe dem) {
dcn = dem;
}
public void onDatabaseChangeNotification(DatabaseChangeEvent e) {
TableChangeDescription[] tc = e.getTableChangeDescription();
for (int i = 0; i < tc.length; i++) {
RowChangeDescription[] rcds = tc[i].getRowChangeDescription();
for (int j = 0; j < rcds.length; j++) {
System.out.println(rcds[j].getRowOperation() + " " + rcds[j].getRowid().stringValue());
}
}
synchronized( dcn ){
dcn.notify();
}
}
}
}
我可以看到它确实注册了更改通知,但是当我去更改该表中的某些内容并提交它时,我从来没有在代码方面得到任何东西说某些东西已经改变了?
任何帮助都会很棒!
更新
使用
SELECT * from USER_CHANGE_NOTIFICATION_REGS
在 SQL Developer 中执行该查询时,我确实得到了一个 reg:
ojdbc8.jar 版本信息是:
使用 javac 1.8.0_171 编译的 Oracle 18.3.0.0.0 JDBC 4.2
Tue_Jun_26_11:06:40_PDT_2018
默认连接属性资源
2019 年 3 月 12 日星期二 10:06:48 EDT
这是 Oracle SQL Developer 的状态:
Oracle Database 12c 企业版 12.2.0.1.0 - 64 位生产
PL/SQL 版本 12.2.0.1.0 - 生产
“核心 12.2.0.1.0 生产”
适用于 Linux 的 TNS:版本 12.2.0.1.0 - 生产
NLSRTL 版本 12.2.0.1.0 - 生产
在我的程序运行并且查询注册的情况下,这就是我看到的端口: