4

I'd like to retrieve all trigger names from an Oracle database schema.

I use getFunctions to retrieve all functions but i can't find another one for the triggers.

DatabaseMetaData dbmd;
ResultSet result = dbmd.getFunctions(null, Ousername, null);
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
gtzinos
  • 1,205
  • 15
  • 27
  • 2
    Maybe method described here will help you http://stackoverflow.com/questions/4305691/need-to-list-all-triggers-in-sql-server-database-with-table-name-and-tables-sch – Kuba May 06 '15 at 11:05

4 Answers4

4

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")
}
gtzinos
  • 1,205
  • 15
  • 27
2

The JDBC API does not provide a standard way to retrieve trigger information from the DatabaseMetaData. In fact, the word "trigger" does not even appear in the 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.

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.

Keith
  • 143
  • 8
0

I have found another way to get all trigger via 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();
}
Ercan
  • 2,601
  • 22
  • 23
0

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:

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.

sbrbot
  • 6,169
  • 6
  • 43
  • 74
  • You can also simply use `SHOW TRIGGERS`. An example: `ResultSet triggerResultSet = con.prepareStatement("SHOW TRIGGERS").executeQuery(); triggersResultSet.next(); triggersResultSet.getString("Trigger"); ` – Maurice Jan 09 '21 at 10:39