简体   繁体   中英

Obtain a table name used after executing JDBC Insert/Update/Delete

Our application executes Insert/Update/Delete SQL's provided to us by our users. We do not know what the SQL looks like before hand. Does there exist a way to obtain which table was affected after executing the Insert/Update/Delete without parsing the tablename out of the SQL itself?

Note: Obviously, since we are calling stmt.executeUpdate() no ResultSet / ResultSetMetaData will be available to us.

Question: Does there exist a way to determine which table was affected after executing an Insert/Update/Delete SQL?

It might depend on your specific DB system but here are some ideas:

If you can get the table for a column, then you could iterate over all the columns and store the table used.

If you have a connection object you an inspect all tables:

DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
  System.out.println(rs.getString(3));
}

If you have access to the SQL query then you can scan for the FROM SQL keyword to identify the table, and/or scan the statement further or use an SQL analyzer on the statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM