Skip to content

Java.sql.DatabaseMetaData

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;
}