private static boolean doTableExists(String table, Connection connection) { String schema = null; try { // Using H2 with a JDBC TCP connection is throwing an exception // See org.h2.engine.SessionRemote#getCurrentSchemaName() if (!"H2 JDBC Driver".equals(connection.getMetaData().getDriverName())) { schema = connection.getSchema(); } } catch (SQLException e) { Loggers.get(DatabaseUtils.class).warn("Fail to determine schema. Keeping it null for searching tables", e); } // table type is used to speed-up Oracle by removing introspection of system tables and aliases. try (ResultSet rs = connection.getMetaData().getTables(connection.getCatalog(), schema, table, TABLE_TYPE)) { while (rs.next()) { String name = rs.getString("TABLE_NAME"); if (table.equalsIgnoreCase(name)) { return true; } } return false; } catch (SQLException e) { throw wrapSqlException(e, "Can not check that table %s exists", table); } }
/** * @param table case-insensitive name of table * @return true if a table exists with this name, otherwise false * @throws SQLException */ public static boolean tableExists(String table, Connection connection) { // table type is used to speed-up Oracle by removing introspection of system tables and aliases. try (ResultSet rs = connection.getMetaData().getTables(null, null, null, TABLE_TYPE)) { while (rs.next()) { String name = rs.getString("TABLE_NAME"); if (table.equalsIgnoreCase(name)) { return true; } } return false; } catch (SQLException e) { throw wrapSqlException(e, "Can not check that table %s exists", table); } }
private static boolean doTableExists(String table, Connection connection) { String schema = null; try { // Using H2 with a JDBC TCP connection is throwing an exception // See org.h2.engine.SessionRemote#getCurrentSchemaName() if (!"H2 JDBC Driver".equals(connection.getMetaData().getDriverName())) { schema = connection.getSchema(); } } catch (SQLException e) { Loggers.get(DatabaseUtils.class).warn("Fail to determine schema. Keeping it null for searching tables", e); } // table type is used to speed-up Oracle by removing introspection of system tables and aliases. try (ResultSet rs = connection.getMetaData().getTables(connection.getCatalog(), schema, table, TABLE_TYPE)) { while (rs.next()) { String name = rs.getString("TABLE_NAME"); if (table.equalsIgnoreCase(name)) { return true; } } return false; } catch (SQLException e) { throw wrapSqlException(e, "Can not check that table %s exists", table); } }