List of usage examples for java.sql DatabaseMetaData getTables
ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String types[])
throws SQLException;
From source file:edu.mayo.cts2.uriresolver.dao.DAOUtiltities.java
public static boolean tableExists(DatabaseMetaData metaData, String tablename) { //ResultSet tables = metaData.getTables(null, "public", "%" ,new String[] {"TABLE"} ); ResultSet tables = null;// ww w .ja va 2s . co m try { tables = metaData.getTables(null, null, tablename, null); if (tables.next()) { tables.close(); return true; } } catch (SQLException e) { return false; } finally { if (tables != null) { try { tables.close(); } catch (SQLException e1) { logger.error("Error while colsing result set: " + e1.getMessage()); } } } return false; }
From source file:las.DBConnector.java
/** * Create Table with(example): tableName: ITEMS <All in capital letters> * details: ITEM_ID,NAME,NUMBER_AVAILABLE * <All in capital letters and put a dash instead of space> *///from w ww . j a va 2s .c om public static void createTable(String tableName, String details) throws SQLException { DatabaseMetaData dbmd = conn.getMetaData(); ResultSet rs = dbmd.getTables(null, "LAS", tableName, null); if (!rs.next()) { String data = "CREATE TABLE " + tableName + "(" + details + ")"; PreparedStatement pt = conn.prepareStatement(data); pt.executeUpdate(); System.out.println(tableName + " has been created"); } else { System.out.println(tableName + " already exists in LAS Database"); } }
From source file:org.eclipse.dirigible.repository.ext.db.DBUtils.java
public static ResultSet getAllTables(Connection connection) throws SQLException { DatabaseMetaData meta = connection.getMetaData(); ResultSet tableNames = meta.getTables(null, null, TABLE_NAME_PATTERN_ALL, null); return tableNames; }
From source file:net.antidot.sql.model.core.SQLConnector.java
/** * Drop all tables from database with connection c. Specific to MySQL * databases./*from ww w. ja v a 2 s. co m*/ * * @param c * @param driver * @throws SQLException */ public static void resetMySQLDatabase(Connection c, DriverType driver) throws SQLException { // Get tables of database DatabaseMetaData meta = c.getMetaData(); ResultSet tablesSet = meta.getTables(c.getCatalog(), null, "%", null); while (tablesSet.next()) { // Extract table name String tableName = new String(tablesSet.getString("TABLE_NAME")); String tableType = tablesSet.getString("TABLE_TYPE"); // Get a statement from the connection Statement stmt = c.createStatement(); // Execute the query if (driver == DriverType.MysqlDriver) { // MySQL compatibility stmt.execute("SET FOREIGN_KEY_CHECKS = 0"); stmt.execute("DROP TABLE \"" + tableName + "\""); } else { if (tableType != null && tableType.equals("TABLE")) stmt.execute("DROP TABLE \"" + tableName + "\" CASCADE"); } stmt.close(); } }
From source file:com.example.querybuilder.server.Jdbc.java
public static ResultSet getTables(DatabaseMetaData databaseMetaData, String catalog, String schemaPattern, String tableNamePattern, String[] types) { try {/*from w w w . java 2 s. co m*/ return databaseMetaData.getTables(catalog, schemaPattern, tableNamePattern, types); } catch (SQLException e) { throw new SqlRuntimeException(e); } }
From source file:org.nuxeo.ecm.core.storage.sql.DatabaseHelper.java
/** * Executes one statement on all the tables in a database. */// ww w .j ava2 s .c o 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 (tableName.toLowerCase().startsWith("trace_xe_")) { // Skip mssql 2012 system table 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; } if ("ACLR_MODIFIED".equals(tableName) && DATABASE instanceof DatabaseOracle) { // skip temporary table on Oracle, cannot be dropped 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"); } // needed for Azure if (tableNames.remove("NXP_LOGS")) { tableNames.add("NXP_LOGS"); } if (tableNames.remove("NXP_LOGS_EXTINFO")) { tableNames.add("NXP_LOGS_EXTINFO"); } // PostgreSQL is lowercase if (tableNames.remove("hierarchy")) { tableNames.add("hierarchy"); } Statement st = connection.createStatement(); for (String tableName : tableNames) { String sql = String.format(statement, tableName); executeSql(st, sql); } st.close(); }
From source file:uk.ac.kcl.texthunter.utils.Utils.java
public static boolean checkForTable(Connection con, String projectName) { DatabaseMetaData meta; try {//from w w w . j a v a 2 s . c om meta = con.getMetaData(); ResultSet res = meta.getTables(null, null, null, new String[] { "TABLE" }); while (res.next()) { System.out.println(" " + res.getString("TABLE_CAT") + ", " + res.getString("TABLE_SCHEM") + ", " + res.getString("TABLE_NAME") + ", " + res.getString("TABLE_TYPE") + ", " + res.getString("REMARKS")); if (res.getString("TABLE_NAME").equalsIgnoreCase(projectName)) { return true; } } } catch (SQLException ex) { Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex); } return false; }
From source file:at.molindo.dbcopy.Database.java
private static Map<String, Table> readTables(Connection connection) throws SQLException { Map<String, Table> tables = new HashMap<String, Table>(); DatabaseMetaData meta = connection.getMetaData(); String catalog = connection.getCatalog(); // for each table in current catalog ResultSet rs = meta.getTables(catalog, null, null, null); try {/* www. j av a 2 s . c o m*/ while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); Table.Builder table = Table.builder(tableName); // columns String columnsQuery = "select COLUMN_NAME,COLLATION_NAME from information_schema.COLUMNS where TABLE_SCHEMA=? and TABLE_NAME=? order by ORDINAL_POSITION"; Map<String, Column> columns = Utils.executePrepared(connection, columnsQuery, new ColumnHandler(), catalog, tableName); if (columns.isEmpty()) { throw new IllegalStateException("table (" + tableName + ") without columns?"); } table.addColumns(columns.values()); // unique keys String uniqueKeysQuery = "show keys from `" + tableName + "` in `" + catalog + "` where `Non_unique` = 0 and `Null` = ''"; List<Map<String, Object>> uniqueKeyColumns = Utils.executePrepared(connection, uniqueKeysQuery, new MapListHandler()); ListMap<String, Column> uniqeKeys = new ListMap<String, Column>(); for (Map<String, Object> keyColumn : uniqueKeyColumns) { String name = (String) keyColumn.get("INDEX_NAME"); String columnName = (String) keyColumn.get("COLUMN_NAME"); if (name == null) { throw new IllegalStateException("KEY_NAME must not be null"); } if (columnName == null) { throw new IllegalStateException("COLUMN_NAME must not be null"); } Column column = columns.get(columnName); if (column == null) { throw new IllegalStateException("COLUMN_NAME unknown: " + columnName); } uniqeKeys.add(name, column); } for (Map.Entry<String, List<Column>> e : uniqeKeys.entrySet()) { table.addUniqueKey(e.getKey(), e.getValue()); } if (uniqeKeys.isEmpty()) { log.warn("table without primary key not supported: " + tableName); } else { tables.put(tableName, table.build()); } } } finally { Utils.close(rs); } return tables; }
From source file:org.artifactory.storage.db.util.DbUtils.java
public static boolean tableExists(DatabaseMetaData metaData, String tableName) throws SQLException { boolean schemaExists; if (metaData.storesLowerCaseIdentifiers()) { tableName = tableName.toLowerCase(); } else if (metaData.storesUpperCaseIdentifiers()) { tableName = tableName.toUpperCase(); }/* w w w . j a v a2 s. c om*/ try (ResultSet rs = metaData.getTables(null, null, tableName, new String[] { "TABLE" })) { schemaExists = rs.next(); } return schemaExists; }
From source file:org.ralasafe.db.DBView.java
private static String[] getObjectNames(String dsName, String schema, String[] objectTypes) { Connection conn = null;/*from w w w .ja va2s . c o m*/ ResultSet tables = null; try { conn = DBPower.getConnection(dsName); DatabaseMetaData metaData = conn.getMetaData(); String databaseProductName = DBUtil.getDatabaseProductName(conn); if (databaseProductName == DBUtil.MYSQL || databaseProductName == DBUtil.SQLSERVER) { tables = metaData.getTables(schema, null, null, objectTypes); } else if (databaseProductName == DBUtil.ORACLE) { tables = metaData.getTables(null, null, null, objectTypes); } else { tables = metaData.getTables(null, schema, null, objectTypes); } List result = new LinkedList(); while (tables.next()) { String tableName = tables.getString("TABLE_NAME"); if (databaseProductName == DBUtil.ORACLE) { String s = tables.getString("TABLE_SCHEM"); if (s.equalsIgnoreCase(schema)) { result.add(tableName); } } else if (databaseProductName == DBUtil.SQLSERVER) { String s = tables.getString("TABLE_SCHEM"); tableName = s + "." + tableName; result.add(tableName); } else { result.add(tableName); } } String[] names = new String[result.size()]; Iterator itr = result.iterator(); for (int i = 0; i < names.length; i++) { names[i] = (String) itr.next(); } return names; } catch (SQLException e) { log.error("", e); throw new DBLevelException(e); } finally { DBUtil.close(tables); DBUtil.close(conn); } }