0

I am trying to perform a check on the database for a table using the below snippet: But the problem is, if the table or the view is empty the condition tables.next() does not evaluate to true, how to handle this scenario?

DatabaseMetaData dbm = con.getMetaData();
ResultSet tables = dbm.getTables(null, null, "your_table_name", null);
if (tables.next()) {
  // Table exists
}
else {
  // Table does not exist
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
D.Amudha
  • 1
  • 1
  • Does this answer your question? [Check if table exists](https://stackoverflow.com/questions/2942788/check-if-table-exists) – Player_Neo Feb 14 '20 at 07:42
  • 1
    These are very trivial questions please do a basic search before posting questions – Player_Neo Feb 14 '20 at 07:45
  • If `next()` returns `false`, then the table does not exist. If the table or view is empty, then `next()` will still return `true`, because the table exists. Could you be more specific as to what the problem is? What are you trying to do? – Mark Rotteveel Feb 16 '20 at 17:04
  • @MarkRotteveel , thanks for the inputs. Found out that the problem was with the SQL statement referring to the incorrect schema. The code is working as expected. – D.Amudha Feb 17 '20 at 06:16

0 Answers0