gpt4 book ai didi

java - 无法使用 Oracle 数据库更改通知获取表名称

转载 作者:行者123 更新时间:2023-11-30 03:31:29 25 4
gpt4 key购买 nike

我必须监视多个表的更改。我的数据库是Oracle11g,我使用的是ojdbc6.jar jdbc驱动库。问题是我得到了“???”当数据库更改事件发生时,而不是表名。 DatabaseChangeEvent 中的所有其他数据均存在,仅缺少表名。

我正在使用在互联网上找到的以下代码,我根据自己的需要稍作修改。

这是程序的输出:

Connection information  : local=ORATEST01/10.10.60.17:43000, remote=ORATEST01/10
.10.60.17:52683
Registration ID : 63
Notification version : 1
Event type : OBJCHANGE
Database name : fmvroratest
Table Change Description (length=1)
operation=[UPDATE], tableName=???, objectNumber=73786
Row Change Description (length=2):
ROW: operation=UPDATE, ROWID=AAASA6AAHAAACEEAAA
ROW: operation=UPDATE, ROWID=AAASA6AAHAAACEEAAA

Table changed: ???




package hr.mspoljaric.dcn;

import java.sql.*;
import java.util.*;
import oracle.jdbc.*;
import oracle.jdbc.dcn.*;

public class OracleDCN {
String URL = "jdbc:oracle:thin:**********************************";
Properties prop;

public static void main(String[] argv) {

OracleDCN dcn = new OracleDCN();
try {
dcn.prop = new Properties();
dcn.run();
} catch (Exception e) {
e.printStackTrace();
}
}

void run() throws SQLException {
OracleConnection conn = (OracleConnection) DriverManager.getConnection(
URL, prop);

Properties prop = new Properties();
prop.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
prop.setProperty(OracleConnection.NTF_LOCAL_TCP_PORT, "43000");

DatabaseChangeRegistration dcr = conn
.registerDatabaseChangeNotification(prop);

try {
dcnListener list = new dcnListener(this);
dcr.addListener(list);

Statement stmt = conn.createStatement();
((OracleStatement) stmt).setDatabaseChangeRegistration(dcr);
ResultSet rs = stmt
.executeQuery("select 1 from klijent, osobe where 1=2");
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 {
OracleDCN dcn;

dcnListener(OracleDCN dem) {
dcn = dem;
}

public void onDatabaseChangeNotification(DatabaseChangeEvent e) {

System.out.println(e.toString());

TableChangeDescription[] tcds = e.getTableChangeDescription();

for (TableChangeDescription tcd: tcds)
System.out.println("Table changed: " + tcd.getTableName());

synchronized (dcn) {
dcn.notify();
}
}
}

最佳答案

已通过以下解决方法解决。由于我收到了 objectNumber 作为响应,因此我在此附加选择中使用此数字来获取表名称:

select object_name from user_objects where object_id=73786

关于java - 无法使用 Oracle 数据库更改通知获取表名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28894323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com