List of usage examples for java.sql DatabaseMetaData getTables
ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String types[])
throws SQLException;
From source file:jp.co.tis.gsp.tools.dba.s2jdbc.gen.DbTableMetaReaderWithView.java
protected List<DbTableMeta> getDbTableMetaList(DatabaseMetaData metaData, String schemaName) { List<DbTableMeta> result = new ArrayList<DbTableMeta>(); try {//from w w w .j av a 2s.c om ResultSet rs = metaData.getTables(null, schemaName, null, new String[] { "TABLE", "VIEW" }); try { while (rs.next()) { DbTableMeta dbTableMeta = new DbTableMeta(); dbTableMeta.setCatalogName(rs.getString("TABLE_CAT")); dbTableMeta.setSchemaName(rs.getString("TABLE_SCHEM")); dbTableMeta.setName(rs.getString("TABLE_NAME")); if (readComment) { dbTableMeta.setComment(rs.getString("REMARKS")); } if (isTargetTable(dbTableMeta)) { result.add(dbTableMeta); } } return result; } finally { ResultSetUtil.close(rs); } } catch (SQLException e) { throw new SQLRuntimeException(e); } }
From source file:com.emr.schemas.DestinationTables.java
/** * Method for getting the MPI database's tables *//*from w w w .j a v a2 s . co m*/ public final void getDatabaseMetaData() { try { DatabaseMetaData dbmd = mpiConn.getMetaData(); String[] types = { "TABLE" }; ResultSet rs = dbmd.getTables(null, null, "%", types); while (rs.next()) { //Add table name to Jlist listModel.addElement(rs.getString("TABLE_NAME")); } } catch (SQLException e) { String stacktrace = org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace(e); JOptionPane.showMessageDialog(this, "Could not fetch Tables for the KenyaEMR Database. Error Details: " + stacktrace, "Table Names Error", JOptionPane.ERROR_MESSAGE); } }
From source file:org.apache.kylin.jdbc.ITJDBCDriverTest.java
@Test public void testMetadata2() throws Exception { Connection conn = getConnection(); List<String> tableList = Lists.newArrayList(); DatabaseMetaData dbMetadata = conn.getMetaData(); ResultSet resultSet = dbMetadata.getTables(null, "%", "%", new String[] { "TABLE" }); while (resultSet.next()) { String schema = resultSet.getString("TABLE_SCHEM"); String name = resultSet.getString("TABLE_NAME"); System.out.println("Get table: schema=" + schema + ", name=" + name); tableList.add(schema + "." + name); }// w ww .j ava2 s. co m resultSet.close(); Assert.assertTrue(tableList.contains("DEFAULT.TEST_KYLIN_FACT")); resultSet = dbMetadata.getColumns(null, "%", "TEST_KYLIN_FACT", "%"); List<String> columns = Lists.newArrayList(); while (resultSet.next()) { String name = resultSet.getString("COLUMN_NAME"); String type = resultSet.getString("TYPE_NAME"); System.out.println("Get column: name=" + name + ", data_type=" + type); columns.add(name); } Assert.assertTrue(columns.size() > 0 && columns.contains("CAL_DT")); resultSet.close(); conn.close(); }
From source file:joachimeichborn.geotag.io.database.DerbyDatabase.java
private Set<String> getAllTables() { final Set<String> tableNames = new HashSet<String>(); try {//from w ww. j av a 2s. c om synchronized (readConnection) { final DatabaseMetaData metaData = readConnection.getMetaData(); try (final ResultSet rs = metaData.getTables(null, null, null, new String[] { "TABLE" })) { while (rs.next()) { tableNames.add(rs.getString("TABLE_NAME").toLowerCase()); } } } } catch (final SQLException aEx) { logger.log(Level.SEVERE, "Could not obtain names of existing tables", aEx); throw new IllegalStateException(aEx); } return tableNames; }
From source file:org.apache.hive.beeline.QFileBeeLineClient.java
private Set<String> getViews() throws SQLException { Set<String> views = new HashSet<String>(); DatabaseMetaData metaData = beeLine.getDatabaseMetaData(); // Get the tables in the default database String[] types = new String[] { "VIEW" }; try (ResultSet tablesResultSet = metaData.getTables(null, "default", "%", types)) { while (tablesResultSet.next()) { views.add(tablesResultSet.getString("TABLE_NAME")); }/*from w w w .j ava 2 s . c o m*/ } return views; }
From source file:jmdbtools.JMdbTools.java
private void createTables(DbSchema schema, Connection conn) throws SQLException { Statement stmt = conn.createStatement(); for (DbTable dbTable : schema.getTables()) { String createTableSql = new CreateTableQuery(dbTable, true).validate().toString(); //TODO: DANGEROUS: auto override tables. Add explicit option to enable if (dbOverwrite) { log("Dropping existing table", "warn"); stmt.executeUpdate("DROP TABLE IF EXISTS `" + dbTable.getName() + "`"); log("creating table:" + dbTable.getName(), "info"); stmt.executeUpdate(createTableSql); } else {/* ww w. ja va2 s.com*/ DatabaseMetaData meta = conn.getMetaData(); ResultSet res = meta.getTables(null, null, dbTable.getName(), new String[] { "TABLE" }); if (res.last()) { //there are entries for "TABLE" with this name don't try to create table log("Table already exists:" + dbTable.getName(), "info"); } else { log("creating table:" + dbTable.getName(), "info"); stmt.executeUpdate(createTableSql); } } } }
From source file:org.apache.hive.beeline.QFileBeeLineClient.java
private Set<String> getTables() throws SQLException { Set<String> tables = new HashSet<String>(); DatabaseMetaData metaData = beeLine.getDatabaseMetaData(); // Get the tables in the default database String[] types = new String[] { "TABLE" }; try (ResultSet tablesResultSet = metaData.getTables(null, "default", "%", types)) { while (tablesResultSet.next()) { tables.add(tablesResultSet.getString("TABLE_NAME")); }// www. j a v a 2s .c om } return tables; }
From source file:com.googlecode.fascinator.sequences.SequenceService.java
/** * Check if the given table exists in the database. * /*from www. ja va2 s . c o m*/ * @param table * The table to look for * @return boolean flag if the table was found or not * @throws SQLException * if there was an error accessing the database */ private boolean findTable(String table) throws SQLException { boolean tableFound = false; DatabaseMetaData meta = connection().getMetaData(); ResultSet result = (ResultSet) meta.getTables(null, null, null, null); while (result.next() && !tableFound) { if (result.getString("TABLE_NAME").equalsIgnoreCase(table)) { tableFound = true; } } close(result); return tableFound; }
From source file:com.emr.schemas.ForeignDataMover.java
private List getDatabaseTables(Connection con) { List tables = new ArrayList(); try {/*from w w w .ja v a 2 s .c o m*/ DatabaseMetaData dbmd = con.getMetaData(); String[] types = { "TABLE" }; ResultSet rs = dbmd.getTables(null, null, "%", types); while (rs.next()) { //Add table name to Jlist tables.add(rs.getString("TABLE_NAME")); } } catch (SQLException e) { String stacktrace = org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace(e); JOptionPane.showMessageDialog(this, "Could not fetch Tables for the KenyaEMR Database. Error Details: " + stacktrace, "Table Names Error", JOptionPane.ERROR_MESSAGE); } return tables; }
From source file:com.adito.jdbc.DBDumper.java
/** * Dump table creation SQL. It is up to the caller to close the stream and connections when * finished with./*from w w w. ja v a2 s.c o m*/ * * @param writer write SQL to this writer. * @param conx connection to get data from * @param quoteChar character to use to quote strings * @param tables array of table names or <code>null</code> to dump all in * database * @throws Exception on any error */ public void dumpTable(PrintWriter writer, JDBCConnectionImpl conx, char quoteChar, String[] tables) throws Exception { Connection jdbcConnection = conx.getConnection(); DatabaseMetaData dbMetaData = jdbcConnection.getMetaData(); if (tables == null) { ResultSet rs = dbMetaData.getTables(null, null, null, null); try { while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); String tableType = rs.getString("TABLE_TYPE"); if (tableType.equalsIgnoreCase("TABLE")) { dumpTable(writer, conx, quoteChar, new String[] { tableName }); } } } finally { rs.close(); } } else { for (int i = 0; i < tables.length; i++) { String tableName = tables[i]; log.info("Dumping table creation for " + tableName); writer.println("CREATE TABLE " + tableName + " ("); boolean first = true; // Columns ResultSet rs2 = dbMetaData.getColumns(null, null, tableName, "%"); try { while (rs2.next()) { if (first) { first = false; } else { writer.println(","); } String columnName = rs2.getString("COLUMN_NAME"); String columnType = rs2.getString("TYPE_NAME"); int columnSize = rs2.getInt("COLUMN_SIZE"); String nullable = rs2.getString("IS_NULLABLE"); String nullString = "NULL"; if ("NO".equalsIgnoreCase(nullable)) { nullString = "NOT NULL"; } writer.print(" " + columnName + " " + columnType); if (columnSize != 0) { if (columnType.equalsIgnoreCase("varchar") && columnSize > 255) { columnSize = 255; } writer.print(" (" + columnSize + ")"); } writer.print(" " + nullString); } } finally { rs2.close(); } // Keys try { rs2 = dbMetaData.getPrimaryKeys(null, null, tableName); String primaryKeyName = null; StringBuffer primaryKeyColumns = new StringBuffer(); while (rs2.next()) { String thisKeyName = rs2.getString("PK_NAME"); if ((thisKeyName != null && primaryKeyName == null) || (thisKeyName == null && primaryKeyName != null) || (thisKeyName != null && !thisKeyName.equals(primaryKeyName)) || (primaryKeyName != null && !primaryKeyName.equals(thisKeyName))) { if (primaryKeyColumns.length() > 0) { writer.print(",\n PRIMARY KEY "); if (primaryKeyName != null) { writer.print(primaryKeyName); } writer.print("(" + primaryKeyColumns.toString() + ")"); } primaryKeyColumns = new StringBuffer(); primaryKeyName = thisKeyName; } if (primaryKeyColumns.length() > 0) { primaryKeyColumns.append(", "); } primaryKeyColumns.append(rs2.getString("COLUMN_NAME")); } if (primaryKeyColumns.length() > 0) { writer.print(",\n PRIMARY KEY "); if (primaryKeyName != null) { writer.print(primaryKeyName); } writer.print(" (" + primaryKeyColumns.toString() + ")"); } } finally { rs2.close(); } writer.println("\n);"); writer.println(); } } }