Java.sql.DatabaseMetaData
lastmod 2026-11-10created 2014-06-153 edits
DatabaseMetaData is a JDBC interface for querying metadata about the connected database and driver. It is obtained with Connection.getMetaData(), and methods such as getTables(), getColumns(), getPrimaryKeys(), and getIndexInfo() take a catalog, schema, and name pattern as arguments and return the results as a ResultSet. It is used to check whether a table or column exists, or to build schema tools, without directly querying system tables that differ for each type of database, and it can also report driver characteristics such as supported SQL features and identifier case rules.
exist Table & Column
public static boolean existTable(Connection connection, String tableName) {
try {
DatabaseMetaData meta = connection.getMetaData();
ResultSet tables = meta.getTables(null, null, tableName, null);
if (tables.next() == true) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public static boolean existColumn(Connection connection, String tableName, String column) {
try {
DatabaseMetaData meta = connection.getMetaData();
ResultSet tables = meta.getColumns(null, null, tableName, column);
if (tables.next() == true) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}