List of usage examples for java.sql DatabaseMetaData getTables
ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String types[])
throws SQLException;
From source file:com.swordlord.jalapeno.DBGenerator.java
private boolean isDBAvailableAndConfigured(DataContext context, DataMap dataMap) { DataNode dataNode = context.getParentDataDomain().lookupDataNode(dataMap); boolean bResult = true; try {/*from ww w. j av a 2 s . c om*/ Connection connection = dataNode.getDataSource().getConnection(); DatabaseMetaData metadata = connection.getMetaData(); String[] types = { "TABLE" }; ResultSet resultSet = metadata.getTables(null, null, "%", types); // If there is at least one, OK if (!resultSet.next()) { LOG.info("Database exists but no structure defined"); bResult = false; } resultSet.close(); connection.close(); } catch (SQLException e) { LOG.info("SQL exception {}", e); bResult = false; } return bResult; }
From source file:org.jtalks.poulpe.util.databasebackup.persistence.DbTableLister.java
/** * Returns the list of all database table names which database contains from given Data source via JDBC. * //w w w . j a v a 2 s . c o m * @return a List of Strings where every String instance represents a table name from the database. * @throws SQLException * is thrown if there is an error during collaborating with the database. */ @SuppressWarnings("unchecked") private List<String> getTableNames() throws MetaDataAccessException { Validate.notNull(dataSource, "dataSource must not be null"); return Collections.unmodifiableList( (List<String>) JdbcUtils.extractDatabaseMetaData(dataSource, new DatabaseMetaDataCallback() { @Override public Object processMetaData(DatabaseMetaData dmd) throws SQLException, MetaDataAccessException { List<String> tableList = new ArrayList<String>(); ResultSet rs = dmd.getTables(null, null, null, new String[] { "TABLE" }); while (rs.next()) { tableList.add(rs.getString("TABLE_NAME")); } return tableList; } })); }
From source file:nl.nn.adapterframework.util.JdbcUtil.java
/** * @return true if tableName exists in database in this connection *//*from w ww. j ava2 s .c o m*/ public static boolean tableExists(Connection conn, String tableName) throws SQLException { PreparedStatement stmt = null; if (useMetaDataForTableExists) { DatabaseMetaData dbmeta = conn.getMetaData(); ResultSet tableset = dbmeta.getTables(null, null, tableName, null); return !tableset.isAfterLast(); } String query = null; try { query = "select count(*) from " + tableName; log.debug("create statement to check for existence of [" + tableName + "] using query [" + query + "]"); stmt = conn.prepareStatement(query); log.debug("execute statement"); ResultSet rs = stmt.executeQuery(); log.debug("statement executed"); rs.close(); return true; } catch (SQLException e) { if (log.isDebugEnabled()) log.debug("exception checking for existence of [" + tableName + "] using query [" + query + "]", e); return false; } finally { if (stmt != null) { stmt.close(); } } }
From source file:com.oracle2hsqldb.dialect.GenericDialect.java
public Iterator getTables(DataSource dataSource, final String schemaName) throws SQLException { final List result = new LinkedList(); MetaDataJdbcTemplate template = new MetaDataJdbcTemplate(dataSource) { protected ResultSet getResults(DatabaseMetaData metaData) throws SQLException { return metaData.getTables(null, schemaName, null, Table.Type.getSupportedNames()); }/*w w w. j av a 2 s . co m*/ }; template.query(new RowCallbackHandler() { public void processRow(ResultSet tables) throws SQLException { result.add(new Table.Spec(tables.getString("TABLE_NAME"), tables.getString("TABLE_TYPE"))); } }); return result.iterator(); }
From source file:org.wso2.carbon.reporting.template.core.client.DatasourceClient.java
/** * get the tables exists in the data source name provided * * @param dsName name of the data source * @return names of the table available in the given data source * @throws ReportingException will occurred if error occurred while getting connection for data source * @throws SQLException will occurred if any error in the SQL syntax *//*from ww w. j a va 2 s.com*/ public String[] getTableNames(String dsName) throws ReportingException, SQLException { ArrayList<String> tableNames = new ArrayList<String>(); Connection connection = getConnection(dsName); DatabaseMetaData metaData = connection.getMetaData(); String[] types = { "TABLE" }; ResultSet resultSet = metaData.getTables(null, null, "%", types); while (resultSet.next()) { tableNames.add(resultSet.getString(3)); } connection.close(); return tableNames.toArray(new String[tableNames.size()]); }
From source file:com.abixen.platform.module.chart.service.impl.AbstractDatabaseService.java
protected ResultSet getTablesAsResultSet(Connection connection) throws SQLException { DatabaseMetaData md = connection.getMetaData(); return md.getTables(null, null, "%", null); }
From source file:com.sample.team5.WhatsYourTechController.java
public void createTablePlayers() throws SQLException { Connection conn;//from ww w.j ava2 s . c om conn = dataSource.getConnection(); DatabaseMetaData meta = conn.getMetaData(); ResultSet res = meta.getTables(null, null, "players", null); if (res.next()) { System.out.println("Table 'players' already exists."); logger.info("Table 'players' already exists."); } else { Statement stmt = conn.createStatement(); String query = "CREATE TABLE IF NOT EXISTS players ( " + "game_id int(11) NOT NULL AUTO_INCREMENT, " + "player varchar(45) DEFAULT NULL, " + "total_score int(11) NOT NULL DEFAULT 0, " + "PRIMARY KEY (game_id), " + "UNIQUE KEY game_id_UNIQUE (game_id), " + "KEY player_fk_idx (player), " + "CONSTRAINT player_fk FOREIGN KEY (player) REFERENCES users (username) ON DELETE NO ACTION ON UPDATE NO ACTION )"; stmt.executeUpdate(query); logger.info("Table 'players' created."); System.out.println("Table 'players' created."); } conn.close(); }
From source file:DatabaseBrowser.java
protected void populateTableBox() { try {/*from w w w.j av a2 s . c om*/ String[] types = { "TABLE" }; String catalog = connection.getCatalog(); String schema = (String) (schemaBox.getSelectedItem()); DatabaseMetaData dmd = connection.getMetaData(); ResultSet rset = dmd.getTables(catalog, schema, null, types); Vector values = new Vector(); while (rset.next()) { values.addElement(rset.getString(3)); } rset.close(); tableBox.setModel(new DefaultComboBoxModel(values)); tableBox.setEnabled(values.size() > 0); } catch (Exception e) { tableBox.setEnabled(false); } }
From source file:com.sample.team5.WhatsYourTechController.java
public void createTableHints() throws SQLException { Connection conn;/*from w ww. j a va 2 s. c o m*/ conn = dataSource.getConnection(); DatabaseMetaData meta = conn.getMetaData(); ResultSet res = meta.getTables(null, null, "hints", null); if (res.next()) { System.out.println("Table 'hints' already exists."); logger.info("Table 'hints' already exists."); } else { Statement stmt = conn.createStatement(); String query = " CREATE TABLE IF NOT EXISTS hints ( " + "hints_id int(11) NOT NULL AUTO_INCREMENT, " + "answer varchar(45) DEFAULT NULL, " + "hint1 varchar(300) DEFAULT NULL, " + "hint2 varchar(300) DEFAULT NULL, " + "hint3 varchar(300) DEFAULT NULL, " + "difficulty varchar(10) DEFAULT NULL, " + "PRIMARY KEY (hints_id), " + "UNIQUE KEY hints_id_UNIQUE (hints_id) )"; stmt.executeUpdate(query); logger.info("Table 'hints' created."); System.out.println("Table 'hints' created."); populateTable(); } conn.close(); }
From source file:com.netflix.metacat.connector.snowflake.SnowflakeConnectorTableService.java
/** * {@inheritDoc}/*w ww. j a va2 s. co m*/ */ @Override protected ResultSet getTables(@Nonnull @NonNull final Connection connection, @Nonnull @NonNull final QualifiedName name, @Nullable final QualifiedName prefix) throws SQLException { final String schema = name.getDatabaseName(); final DatabaseMetaData metaData = connection.getMetaData(); return prefix == null || StringUtils.isEmpty(prefix.getTableName()) ? metaData.getTables(connection.getCatalog(), schema, null, TABLE_TYPES) : metaData.getTables(connection.getCatalog(), schema, prefix.getTableName() + JdbcConnectorUtils.MULTI_CHARACTER_SEARCH, TABLE_TYPES); }