[英]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.