简体   繁体   English

如何使用 Java JDBC 从数据库中获取所有触发器名称?

[英]How to get all trigger names from a database using Java JDBC?

I'd like to retrieve all trigger names from an Oracle database schema.我想从 Oracle 数据库模式中检索所有触发器名称。

I use getFunctions to retrieve all functions but i can't find another one for the triggers.我使用 getFunctions 来检索所有函数,但我找不到触发器的另一个函数。

DatabaseMetaData dbmd;
ResultSet result = dbmd.getFunctions(null, Ousername, null);

You can do it using metadata.您可以使用元数据来完成。

DatabaseMetaData dbmd = dbConnection.getMetaData(); 
ResultSet result = dbmd.getTables("%", Ousername, "%", new String[]{ "TRIGGER" });
while (result.next()) {
     result.getString("TABLE_NAME")
}

The JDBC API does not provide a standard way to retrieve trigger information from the DatabaseMetaData. JDBC API 不提供从 DatabaseMetaData 中检索触发器信息的标准方法。 In fact, the word "trigger" does not even appear in the Javadoc.事实上,“触发器”这个词甚至没有出现在 Javadoc 中。 The accepted answer may work for Oracle, but it is not documented, and it certainly does not work for other databases like HSQL and PostgreSQL.接受的答案可能适用于 Oracle,但没有记录,它当然不适用于 HSQL 和 PostgreSQL 等其他数据库。

The only way, at this time, to retrieve trigger information without finding some undocumented backdoor hack in the JDBC driver is to issue database specific queries.此时,检索触发器信息而不在 JDBC 驱动程序中发现一些未记录的后门黑客的唯一方法是发出特定于数据库的查询。

I have found another way to get all trigger via PreparedStatement:我找到了另一种通过 PreparedStatement 获取所有触发器的方法:

try {
    System.out.println("\n******* Table Name: "+ tableName);    
    String selectQuery = "SELECT TRIGGER_NAME FROM ALL_TRIGGERS WHERE TABLE_NAME = ?";
    PreparedStatement statement = DataSource.getConnection().prepareStatement(selectQuery); 
    statement.setString(1, tableName.toUpperCase());
    ResultSet result = statement.executeQuery();

    System.out.println("Triggers: ");
    while (result.next()) {
        String triggerName = result.getString("TRIGGER_NAME");
        System.out.println(triggerName);
    }
} catch (SQLException e) {
    e.printStackTrace();
}

Just hint for MySQL users: if you want to retrieve all triggers from MySQL database there is table TRIGGERS in INFORMATION_SCHEMA with all info about database triggers:仅提示 MySQL 用户:如果您想从 MySQL 数据库中检索所有触发器,则 INFORMATION_SCHEMA 中的表 TRIGGERS 包含有关数据库触发器的所有信息:

SELECT * FROM INFORMATION_SCHEMA.TRIGGERS

Similar is for routines (Functions and Procedures)类似的是例程(函数和过程)

SELECT * FROM INFORMATION_SCHEMA.ROUTINES

Unfortunately triggers are not well supported in JDBC MetaData.不幸的是触发器在 JDBC MetaData 中没有得到很好的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JDBC-从OpenOffice数据库获取所有表名 - JDBC - Get all Table names from OpenOffice Database 如何使用java JDBC获取MySql的数据库“架构”名称列表 - how to get list of Databases "Schema" names of MySql using java JDBC 如何使用Java jdbc-odbc网桥从* .mdb文件获取表名? - How to get the table names from a *.mdb file using java jdbc-odbc bridge? 如何从数据库中获取所有表名? - How to get all table names from a database? 如何从java中的Sybase DB服务器获取所有数据库名称的列表 - How to get the list of all database names from a Sybase DB Server in java 如何使用Java和JDBC向数据库发送/接收HashMap? - How to send/receive HashMap to/from database using Java and JDBC? java-如何在不使用JDBC的情况下从数据库检索结果集? - java- how to retrieve resultset from database without using JDBC? 如何使用java从firebase数据库中的列表中获取所有值? - How to get all the values from the list in firebase database using java? 如何使用JDBC从Java连接到Access数据库? - How can I connect to an Access database from Java using JDBC? 如何使用Java中的集合从数据库中获取表的列名(动态地而不用硬编码列头) - how to get the column names(dynamically without hardcoding the columnheaders) of a table from database using collections in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM