I have a desktop application with a database embedded in it. When I execute my program I need to check that specific table exists, or create it if not.
Given a Connection object named conn for my database, how could I check this?
I have a desktop application with a database embedded in it. When I execute my program I need to check that specific table exists, or create it if not.
Given a Connection object named conn for my database, how could I check this?
You can use the available meta data:
DatabaseMetaData meta = con.getMetaData();
ResultSet res = meta.getTables(null, null, null,
new String[] {"TABLE"});
while (res.next()) {
System.out.println(
" "+res.getString("TABLE_CAT")
+ ", "+res.getString("TABLE_SCHEM")
+ ", "+res.getString("TABLE_NAME")
+ ", "+res.getString("TABLE_TYPE")
+ ", "+res.getString("REMARKS"));
}
See here for more details.
DatabaseMetaData dbm = con.getMetaData();
// check if "employee" table is there
ResultSet tables = dbm.getTables(null, null, "employee", null);
if (rs.next()) {
// Table exists
}
else {
// Table does not exist
}