Reputation: 11
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?
Upvotes: 1
Views: 90
Reputation: 26647
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.
Upvotes: 1