List of usage examples for java.sql DatabaseMetaData getTables
ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String types[])
throws SQLException;
From source file:org.eclipse.ecr.core.storage.sql.testlib.DatabaseHelper.java
/** * Executes one statement on all the tables in a database. *///from w ww .j ava 2 s . co m public static void doOnAllTables(Connection connection, String catalog, String schemaPattern, String statement) throws SQLException { DatabaseMetaData metadata = connection.getMetaData(); List<String> tableNames = new LinkedList<String>(); ResultSet rs = metadata.getTables(catalog, schemaPattern, "%", new String[] { "TABLE" }); while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); if (tableName.indexOf('$') != -1) { // skip Oracle 10g flashback/fulltext-index tables continue; } if ("ACLR_USER_USERS".equals(tableName)) { // skip nested table that is dropped by the main table continue; } if ("ANCESTORS_ANCESTORS".equals(tableName)) { // skip nested table that is dropped by the main table continue; } tableNames.add(tableName); } // not all databases can cascade on drop // remove hierarchy last because of foreign keys if (tableNames.remove("HIERARCHY")) { tableNames.add("HIERARCHY"); } // PostgreSQL is lowercase if (tableNames.remove("hierarchy")) { tableNames.add("hierarchy"); } Statement st = connection.createStatement(); for (String tableName : tableNames) { String sql = String.format(statement, tableName); log.trace("SQL: " + sql); st.execute(sql); } st.close(); }
From source file:com.mirth.connect.server.util.DatabaseUtil.java
/** * Tell whether or not the given table exists in the database */// w w w . j a v a 2s . c om public static boolean tableExists(Connection connection, String tableName) { ResultSet resultSet = null; try { DatabaseMetaData metaData = connection.getMetaData(); resultSet = metaData.getTables(null, null, tableName.toUpperCase(), new String[] { "TABLE" }); if (resultSet.next()) { return true; } resultSet = metaData.getTables(null, null, tableName.toLowerCase(), new String[] { "TABLE" }); return resultSet.next(); } catch (SQLException e) { throw new DonkeyDaoException(e); } finally { DbUtils.closeQuietly(resultSet); } }
From source file:org.apache.phoenix.hive.util.PhoenixUtil.java
public static boolean existTable(Connection conn, String tableName) throws SQLException { boolean exist = false; DatabaseMetaData dbMeta = conn.getMetaData(); String[] schemaInfo = getTableSchema(tableName.toUpperCase()); try (ResultSet rs = dbMeta.getTables(null, schemaInfo[0], schemaInfo[1], null)) { exist = rs.next();/* w w w . j av a 2 s .c o m*/ if (LOG.isDebugEnabled()) { if (exist) { LOG.debug(rs.getString("TABLE_NAME") + " table exist. "); } else { LOG.debug("table " + tableName + " doesn't exist."); } } } return exist; }
From source file:org.apache.tika.eval.reports.ResultsReporter.java
private static Path getDefaultReportsConfig(Connection c) throws IOException, SQLException { DatabaseMetaData md = c.getMetaData(); String internalPath = null;/*from ww w .ja v a2s . c o m*/ try (ResultSet rs = md.getTables(null, null, "%", null)) { while (rs.next()) { String tName = rs.getString(3); if (ExtractComparer.CONTENTS_TABLE_B.getName().equalsIgnoreCase(tName)) { internalPath = "/comparison-reports.xml"; break; } else if (ExtractProfiler.PROFILE_TABLE.getName().equalsIgnoreCase(tName)) { internalPath = "/profile-reports.xml"; break; } } } if (internalPath == null) { throw new RuntimeException("Couldn't determine if this database was a 'profiler' or 'comparison' db"); } Path tmp = Files.createTempFile("tmp-tika-reports", ".xml"); Files.copy(ResultsReporter.class.getResourceAsStream(internalPath), tmp, StandardCopyOption.REPLACE_EXISTING); return tmp; }
From source file:org.apache.falcon.util.HsqldbTestUtils.java
public static String[] listTables() { ResultSet results = null;/*from w w w . ja v a 2 s . com*/ String[] tableTypes = { "TABLE" }; try { try { DatabaseMetaData metaData = getConnection().getMetaData(); results = metaData.getTables(null, null, null, tableTypes); } catch (SQLException sqlException) { LOG.error("Error reading database metadata: " + sqlException.toString(), sqlException); return null; } if (null == results) { return null; } try { ArrayList<String> tables = new ArrayList<String>(); while (results.next()) { String tableName = results.getString("TABLE_NAME"); tables.add(tableName); } return tables.toArray(new String[0]); } catch (SQLException sqlException) { LOG.error("Error reading from database: " + sqlException.toString(), sqlException); return null; } } finally { if (null != results) { try { results.close(); getConnection().commit(); } catch (SQLException sqlE) { LOG.error("Exception closing ResultSet: " + sqlE.toString(), sqlE); } } } }
From source file:org.apache.hadoop.vertica.VerticaUtil.java
public static void checkOutputSpecs(Configuration conf) throws IOException { VerticaConfiguration vtconfig = new VerticaConfiguration(conf); String writerTable = vtconfig.getOutputTableName(); if (writerTable == null) throw new IOException("Vertica output requires a table name defined by " + VerticaConfiguration.OUTPUT_TABLE_NAME_PROP); String[] def = vtconfig.getOutputTableDef(); boolean dropTable = vtconfig.getDropTable(); String schema = null;//w w w. j a va2s . com String table = null; String[] schemaTable = writerTable.split("\\."); if (schemaTable.length == 2) { schema = schemaTable[0]; table = schemaTable[1]; } else table = schemaTable[0]; Statement stmt = null; try { Connection conn = vtconfig.getConnection(true); DatabaseMetaData dbmd = conn.getMetaData(); ResultSet rs = dbmd.getTables(null, schema, table, null); boolean tableExists = rs.next(); stmt = conn.createStatement(); if (tableExists && dropTable) { if (verticaVersion(conf, true) >= 305) { stmt = conn.createStatement(); stmt.execute("TRUNCATE TABLE " + writerTable); } else { // for version < 3.0 drop the table if it exists // if def is empty, grab the columns first to redfine the table if (def == null) { rs = dbmd.getColumns(null, schema, table, null); ArrayList<String> defs = new ArrayList<String>(); while (rs.next()) defs.add(rs.getString(4) + " " + rs.getString(5)); def = defs.toArray(new String[0]); } stmt = conn.createStatement(); stmt.execute("DROP TABLE " + writerTable + " CASCADE"); tableExists = false; // force create } } // create table if it doesn't exist if (!tableExists) { if (def == null) throw new RuntimeException( "Table " + writerTable + " does not exist and no table definition provided"); if (schema != null) { rs = dbmd.getSchemas(null, schema); if (!rs.next()) stmt.execute("CREATE SCHEMA " + schema); } StringBuffer tabledef = new StringBuffer("CREATE TABLE ").append(writerTable).append(" ("); for (String column : def) tabledef.append(column).append(","); tabledef.replace(tabledef.length() - 1, tabledef.length(), ")"); stmt.execute(tabledef.toString()); // TODO: create segmented projections stmt.execute("select implement_temp_design('" + writerTable + "')"); } } catch (Exception e) { throw new RuntimeException(e); } finally { if (stmt != null) try { stmt.close(); } catch (SQLException e) { throw new RuntimeException(e); } } }
From source file:com.predic8.membrane.core.interceptor.statistics.util.JDBCUtil.java
public static boolean tableExists(Connection con, String table) throws SQLException { DatabaseMetaData meta = con.getMetaData(); ResultSet rs = meta.getTables("", null, table.toUpperCase(), null); return rs.next(); }
From source file:com.vertica.hadoop.VerticaOutputFormat.java
public static void checkOutputSpecs(VerticaConfiguration vtconfig) throws IOException { Relation vTable = new Relation(vtconfig.getOutputTableName()); if (vTable.isNull()) throw new IOException("Vertica output requires a table name defined by " + VerticaConfiguration.OUTPUT_TABLE_NAME_PROP); String[] def = vtconfig.getOutputTableDef(); boolean dropTable = vtconfig.getDropTable(); Statement stmt = null;/*from w w w . j a va 2s . c om*/ try { Connection conn = vtconfig.getConnection(true); DatabaseMetaData dbmd = conn.getMetaData(); ResultSet rs = dbmd.getTables(null, vTable.getSchema(), vTable.getTable(), null); boolean tableExists = rs.next(); stmt = conn.createStatement(); if (tableExists && dropTable) { stmt = conn.createStatement(); stmt.execute("TRUNCATE TABLE " + vTable.getQualifiedName().toString()); } // create table if it doesn't exist if (!tableExists) { if (def == null) throw new RuntimeException("Table " + vTable.getQualifiedName().toString() + " does not exist and no table definition provided"); if (!vTable.isDefaultSchema()) { stmt.execute("CREATE SCHEMA IF NOT EXISTS " + vTable.getSchema()); } StringBuffer tabledef = new StringBuffer("CREATE TABLE ") .append(vTable.getQualifiedName().toString()).append(" ("); for (String column : def) tabledef.append(column).append(","); tabledef.replace(tabledef.length() - 1, tabledef.length(), ")"); stmt.execute(tabledef.toString()); } } catch (Exception e) { throw new RuntimeException(e); } finally { if (stmt != null) try { stmt.close(); } catch (SQLException e) { throw new RuntimeException(e); } } }
From source file:org.artificer.repository.hibernate.HibernateUtil.java
private static boolean hasTables(Connection connection) throws Exception { DatabaseMetaData metadata = connection.getMetaData(); // check if "ArtificerArtifact" table exists ResultSet tables = metadata.getTables(null, null, ArtificerArtifact.class.getSimpleName(), null); if (tables.next()) { return true; }/* ww w.ja v a 2 s. c o m*/ // also need to check all caps (thanks, Oracle) tables = metadata.getTables(null, null, ArtificerArtifact.class.getSimpleName().toUpperCase(Locale.ROOT), null); if (tables.next()) { return true; } // otherwise, nope return false; }
From source file:com.vertica.hivestoragehandler.VerticaOutputFormat.java
public static void checkOutputSpecs(VerticaConfiguration vtconfig) throws IOException { VerticaRelation vTable = new VerticaRelation(vtconfig.getOutputTableName()); if (vTable.isNull()) throw new IOException("Vertica output requires a table name defined by " + VerticaConfiguration.OUTPUT_TABLE_NAME_PROP); String[] def = vtconfig.getOutputTableDef(); boolean dropTable = vtconfig.getDropTable(); Statement stmt = null;//from w w w . j a va 2 s .co m try { Connection conn = vtconfig.getConnection(true); DatabaseMetaData dbmd = conn.getMetaData(); ResultSet rs = dbmd.getTables(null, vTable.getSchema(), vTable.getTable(), null); boolean tableExists = rs.next(); stmt = conn.createStatement(); if (tableExists && dropTable) { stmt = conn.createStatement(); stmt.execute("TRUNCATE TABLE " + vTable.getQualifiedName().toString()); } // create table if it doesn't exist if (!tableExists) { if (def == null) throw new RuntimeException("Table " + vTable.getQualifiedName().toString() + " does not exist and no table definition provided"); if (!vTable.isDefaultSchema()) { stmt.execute("CREATE SCHEMA IF NOT EXISTS " + vTable.getSchema()); } StringBuffer tabledef = new StringBuffer("CREATE TABLE ") .append(vTable.getQualifiedName().toString()).append(" ("); for (String column : def) tabledef.append(column).append(","); tabledef.replace(tabledef.length() - 1, tabledef.length(), ")"); stmt.execute(tabledef.toString()); } } catch (Exception e) { throw new RuntimeException(e); } finally { if (stmt != null) try { stmt.close(); } catch (SQLException e) { throw new RuntimeException(e); } } }