java - 如何在 Java 中实现一个数据库监听器

标签 java database listener

我有一个要求,如果在数据库表中插入一条记录,则需要自动执行一个 java 进程。实现数据库监听器的最简单方法是什么?

最佳答案

我有一个适用于 Oracle 的解决方案。你不需要创建你自己的,因为现在 Oracle 购买了 Java,它为它发布了一个监听器。据我所知,这在内部不使用轮询,而是将通知推送到 Java 端(可能基于某些触发器):

public interface oracle.jdbc.dcn.DatabaseChangeListener 
extends java.util.EventListener {
    void onDatabaseChangeNotification(oracle.jdbc.dcn.DatabaseChangeEvent arg0);
}

你可以像这样实现它(这只是一个示例):

public class DBListener implements DatabaseChangeListener {
    private DbChangeNotification toNotify;

    public BNSDBListener(DbChangeNotification toNotify) {
        this.toNotify = toNotify;
    }

    @Override
    public void onDatabaseChangeNotification(oracle.jdbc.dcn.DatabaseChangeEvent e) {
        synchronized( toNotify ) {
            try {
                toNotify.notifyDBChangeEvent(e); //do sth
            } catch (Exception ex) {
                Util.logMessage(CLASSNAME, "onDatabaseChangeNotification", 
                    "Errors on the notifying object.", true);
                Util.printStackTrace(ex);
                Util.systemExit();                                       
            }
        }       
    }
}

编辑:
您可以使用以下类进行注册:oracle.jdbc.OracleConnectionWrapper

public class oracle.jdbc.OracleConnectionWrapper implements oracle.jdbc.OracleConnection {...}

假设你在某处创建了一个方法:

public void registerPushNotification(String sql) {
    oracle.jdbc.driver.OracleConnection oracleConnection = ...;//connect to db

    dbProperties.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
    dbProperties.setProperty(OracleConnection.DCN_QUERY_CHANGE_NOTIFICATION, "true");

    //this is what does the actual registering on the db end
    oracle.jdbc.dcn.DatabaseChangeRegistration dbChangeRegistration= oracleConnection.registerDatabaseChangeNotification(dbProperties);

    //now you can add the listener created before my EDIT
    listener = new DBListener(this);
    dbChangeRegistration.addListener(listener);

    //now you need to add whatever tables you want to monitor
    Statement stmt = oracleConnection.createStatement();
    //associate the statement with the registration:
    ((OracleStatement) stmt).setDatabaseChangeRegistration(dbChangeRegistration); //look up the documentation to this method [http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleStatement.html#setDatabaseChangeRegistration_oracle_jdbc_dcn_DatabaseChangeRegistration_]

    ResultSet rs = stmt.executeQuery(sql); //you have to execute the query to link it to the statement for it to be monitored
    while (rs.next()) { ...do sth with the results if interested... }

    //see what tables are being monitored
    String[] tableNames = dbChangeRegistration.getTables();
    for (int i = 0; i < tableNames.length; i++) {
        System.out.println(tableNames[i]    + " has been registered.");
    }
    rs.close();
    stmt.close();
}

此示例不包括 try-catch 子句或任何异常处理。

关于java - 如何在 Java 中实现一个数据库监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12618915/

相关文章:

android - 将 onClick 添加到对话框布局中没有类的按钮

java - 设置 Actor 监听 LibGDX 中的按钮点击

java - 使用 selenium,我需要动态迭代所有 iframe 及其子 iframe

database - 使用映射表保持一致性的最佳方法

mysql - 用户文件数据库设计

java - 在自动触发默认监听器之前触发 Hibernate 自定义事件监听器

java - 如何为 JList 和 DefaultListModel 创建 setter 和 getter? ( java )

java - Android Assets 文件夹为空

java - 优化 : Is the order of cases in a switch statement important?

mysql - 误删用户账号和数据库,如何恢复?