简体   繁体   English

获取执行JDBC插入/更新/删除后使用的表名

[英]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. 我们的应用程序执行用户提供给我们的插入/更新/删除SQL。 We do not know what the SQL looks like before hand. 我们不知道SQL是什么样子。 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? 是否存在一种方法,可以在执行插入/更新/删除操作后不从SQL本身解析表名的情况下获取受影响的表?

Note: Obviously, since we are calling stmt.executeUpdate() no ResultSet / ResultSetMetaData will be available to us. 注意:显然,由于我们正在调用stmt.executeUpdate()没有ResultSet / ResultSetMetaData可用于我们。

Question: Does there exist a way to determine which table was affected after executing an Insert/Update/Delete SQL? 问题:执行插入/更新/删除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. 如果您有权访问SQL查询,则可以扫描FROM SQL关键字以识别表,和/或进一步扫描该语句,或者在该语句上使用SQL分析器。

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

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