List of usage examples for java.sql Connection getMetaData
DatabaseMetaData getMetaData() throws SQLException;
DatabaseMetaData
object that contains metadata about the database to which this Connection
object represents a connection. From source file:com.pactera.edg.am.metamanager.extractor.util.GenSqlUtil.java
/** * ????Oracle?Teradata?SQL/*from ww w . j a v a 2 s . c om*/ * * @return * @throws IOException * ????Bean * @throws SQLException * ????? */ private static Map<String, String> findDatabaseSQL() { Connection conn = null; String databaseName = instance.databaseName; try { if (databaseName == null) { DataSource dataSource = ExtractorContextLoader.getDataSource(); conn = dataSource.getConnection(); DatabaseMetaData meta = conn.getMetaData(); instance.databaseName = meta.getDatabaseProductName(); databaseName = instance.databaseName; } String location = instance.getLocation(databaseName); if (location == null) { throw new SQLFileNotLoadException("???" + databaseName + "SQLspring/context-service.xmlGenSqlUtil"); } if (instance.sqlHolder.get(databaseName) == null) { Map<String, String> sqls = readSQL(location); instance.sqlHolder.put(databaseName, sqls); } return instance.sqlHolder.get(databaseName); } catch (SQLException e) { databaseName = (databaseName == null) ? "Unkown" : databaseName; String s = "??[" + databaseName + "]SQL"; log.error(s, e); throw new SQLFileNotLoadException(s, e); } catch (IOException e) { databaseName = (databaseName == null) ? "Unkown" : databaseName; String s = "??[" + databaseName + "]SQL"; log.error(s, e); throw new SQLFileNotLoadException(s, e); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
From source file:com.aurel.track.dbase.UpdateDbSchema.java
private static boolean hasTables(Connection conn) { boolean hasTables = false; try {// w ww .j av a2s .c o m DatabaseMetaData md = conn.getMetaData(); String userName = md.getUserName(); String url = md.getURL(); boolean isOracleOrDb2 = url.startsWith("jdbc:oracle") || url.startsWith("jdbc:db2"); ResultSet rsTables = md.getTables(conn.getCatalog(), isOracleOrDb2 ? userName : null, null, null); LOGGER.info("Getting the tables metadata"); if (rsTables != null && rsTables.next()) { LOGGER.info("Find TSITE table..."); while (rsTables.next()) { String tableName = rsTables.getString("TABLE_NAME"); String tablenameUpperCase = tableName.toUpperCase(); if ("TSITE".equals(tablenameUpperCase)) { LOGGER.info("TSITE table found"); hasTables = true; break; } else { if (tablenameUpperCase.endsWith("TSITE")) { LOGGER.info(tablenameUpperCase + " table found"); hasTables = true; break; } } } } } catch (Exception e) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } if (!hasTables) { Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT OBJECTID FROM TSITE"); if (rs.next()) { hasTables = true; } } catch (SQLException e) { LOGGER.info("Table TSITE does not exist"); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { } } } } return hasTables; }
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 {//from w w w .ja v 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:com.hangum.tadpole.erd.core.relation.CubridTableRelation.java
/** * ? relation ?/*from www . j a va 2 s . c om*/ * * @param userDB * @return */ public static List<ReferencedTableDAO> makeCubridRelation(UserDBDAO userDB, String tableName) throws Exception { List<ReferencedTableDAO> listRealRefTableDAO = new ArrayList<ReferencedTableDAO>(); Connection conn = null; ResultSet rs = null; String[] tableNames = StringUtils.split(tableName, ','); for (String table : tableNames) { table = StringUtils.replace(table, "'", ""); try { SqlMapClient sqlClient = TadpoleSQLManager.getInstance(userDB); conn = sqlClient.getDataSource().getConnection(); rs = conn.getMetaData().getImportedKeys("", "", table); while (rs.next()) { ReferencedTableDAO ref = new ReferencedTableDAO(); ref.setConstraint_name(rs.getString("FK_NAME")); // ? ref.setTable_name(rs.getString("FKTABLE_NAME")); ref.setColumn_name(rs.getString("FKCOLUMN_NAME")); ref.setReferenced_table_name(rs.getString("PKTABLE_NAME")); ref.setReferenced_column_name(rs.getString("PKCOLUMN_NAME")); if (logger.isDebugEnabled()) logger.debug(ref.toString()); listRealRefTableDAO.add(ref); } } catch (Exception e) { logger.error("cubrid relation", e); throw new Exception("Cubrid relation exception " + e.getMessage()); } finally { if (rs != null) try { rs.close(); } catch (Exception e) { } if (conn != null) try { conn.close(); } catch (Exception e) { } } } // end last for return listRealRefTableDAO; }
From source file:com.trackplus.ddl.DataReader.java
private static void logDatabaseMetaDataInfo(DatabaseInfo databaseInfo, Connection connection) throws DDLException { DatabaseMetaData databaseMetaData = null; try {/*w w w . ja va2s. co m*/ databaseMetaData = connection.getMetaData(); int majorVersion = databaseMetaData.getDatabaseMajorVersion(); int minorVersion = databaseMetaData.getDatabaseMinorVersion(); String productName = databaseMetaData.getDatabaseProductName(); String productVersion = databaseMetaData.getDatabaseProductVersion(); int driverMajorVersion = databaseMetaData.getDriverMajorVersion(); int driverMinorVersion = databaseMetaData.getDriverMinorVersion(); LOGGER.debug("DB DRIVER=" + databaseInfo.getDriver()); LOGGER.debug("DB URL=" + databaseInfo.getUrl()); LOGGER.debug("DB USER=" + databaseInfo.getUser()); String password = databaseInfo.getPassword() == null ? null : databaseInfo.getPassword().replaceAll(".", "*"); LOGGER.debug("DB PASSWORD=" + password + "\n"); LOGGER.debug("DB majorVersion=" + majorVersion); LOGGER.debug("DB minorVersion=" + minorVersion); LOGGER.debug("DB productName=" + productName); LOGGER.debug("DB productVersion=" + productVersion); LOGGER.debug("DB driverMajorVersion=" + driverMajorVersion); LOGGER.debug("DB driverMinorVersion=" + driverMinorVersion); } catch (SQLException e) { throw new DDLException(e.getMessage(), e); } }
From source file:com.tern.db.db.java
public static Database establish(DataSource ds, String name) throws SQLException { if (name == null || name.length() <= 0) { name = "default"; }/*from w w w.j ava 2 s .co m*/ if (Database.dbs.containsKey(name)) { throw new SQLException("Has duplicate database name:" + name); } Connection con = null; String driverName = null; try { con = ds.getConnection(); java.sql.DatabaseMetaData meta = con.getMetaData(); driverName = meta.getDriverName(); } catch (SQLException e) { Trace.write(Trace.Error, e, "Create database from DataSource"); } finally { if (con != null) { try { con.close(); } catch (java.sql.SQLException ex1) { } } } if (driverName == null) { return null; } Database d = null; if (driverName.indexOf("Oracle") >= 0) { d = new OracleDB(); } else if (driverName.indexOf("mysql") >= 0) { d = new MySqlDB(); } else { throw new SQLException("Unknown database driver:" + driverName); } d.ds = ds; if (Database.db == null) { Database.db = d; } Database.dbs.put(name, d); Trace.write(Trace.Running, "database added,name = %s,driver=%s", name, driverName); return d; }
From source file:com.hangum.tadpole.rdb.erd.core.relation.CubridTableRelation.java
/** * ? relation ?/*from w w w. jav a 2 s.c o m*/ * * @param userDB * @return */ public static List<ReferencedTableDAO> makeCubridRelation(UserDBDAO userDB, String tableName) throws Exception { List<ReferencedTableDAO> listRealRefTableDAO = new ArrayList<ReferencedTableDAO>(); Connection conn = null; ResultSet rs = null; String[] tableNames = StringUtils.split(tableName, ','); for (String table : tableNames) { table = StringUtils.replace(table, "'", ""); try { SqlMapClient sqlClient = TadpoleSQLManager.getInstance(userDB); conn = sqlClient.getDataSource().getConnection(); rs = conn.getMetaData().getImportedKeys("", "", table); while (rs.next()) { ReferencedTableDAO ref = new ReferencedTableDAO(); if (rs.getString("FK_NAME") == null || "".equals(rs.getString("FK_NAME"))) ref.setConstraint_name(rs.getString("FK_NAME")); else ref.setConstraint_name(rs.getString("fk_name")); // Table names if (rs.getString("FKTABLE_NAME") == null || "".equals(rs.getString("FKTABLE_NAME"))) ref.setTable_name(rs.getString("FKTABLE_NAME")); else ref.setTable_name(rs.getString("fktable_name")); if (rs.getString("FKCOLUMN_NAME") == null || "".equals(rs.getString("FKCOLUMN_NAME"))) ref.setColumn_name(rs.getString("FKCOLUMN_NAME")); else ref.setColumn_name(rs.getString("fkcolumn_name")); if (rs.getString("PKTABLE_NAME") == null || "".equals(rs.getString("PKTABLE_NAME"))) ref.setReferenced_table_name(rs.getString("PKTABLE_NAME")); else ref.setReferenced_table_name(rs.getString("pktable_name")); if (rs.getString("PKCOLUMN_NAME") == null || "".equals(rs.getString("PKCOLUMN_NAME"))) ref.setReferenced_column_name(rs.getString("PKCOLUMN_NAME")); else ref.setReferenced_column_name(rs.getString("pkcolumn_name")); if (logger.isDebugEnabled()) logger.debug(ref.toString()); listRealRefTableDAO.add(ref); } } catch (Exception e) { logger.error("cubrid relation", e); throw new Exception("Cubrid relation exception " + e.getMessage()); } finally { if (rs != null) try { rs.close(); } catch (Exception e) { } if (conn != null) try { conn.close(); } catch (Exception e) { } } } // end last for return listRealRefTableDAO; }
From source file:gridool.db.helpers.GridDbUtils.java
public static boolean hasParentTable(@Nonnull final Connection conn, @Nullable final String pkTableName) throws SQLException { DatabaseMetaData metadata = conn.getMetaData(); String catalog = conn.getCatalog(); final ResultSet rs = metadata.getExportedKeys(catalog, null, pkTableName); try {// w w w . ja va 2s .co m return rs.next(); } finally { rs.close(); } }
From source file:com.mg.framework.utils.DatabaseUtils.java
/** * ? ? //from w w w . j a va2 s.c om * * @return <code>DBMSType.UNKNOWN</> ? ? * ? * @see DBMSType */ public static DBMSType getDBMSType() { DBMSType result = DBMSType.UNKNOWN; java.sql.Connection conn = ServerUtils.getConnection(); String dbName; try { try { dbName = conn.getMetaData().getDatabaseProductName().toUpperCase(); } finally { conn.close(); } } catch (SQLException e) { logger.error("During get DBMS type database access error occurs", e); return result; } return getDBMSType(dbName); }
From source file:net.firejack.platform.core.utils.db.DBUtils.java
private static List<Table> getTables(Connection connection, OpenFlameDataSource dataSource) throws SQLException { List<Table> tableNames = new ArrayList<Table>(); DatabaseMetaData metaData = connection.getMetaData(); ResultSet rs;/*from w ww .j a v a2 s.c o m*/ if (dataSource.getName() == DatabaseName.Oracle) { rs = metaData.getTables(dataSource.getSid(), dataSource.getSchema(), null, new String[] { "TABLE" }); } else { rs = metaData.getTables(null, null, null, new String[] { "TABLE" }); } while (rs.next()) { String tableType = rs.getString("TABLE_TYPE"); if ("TABLE".equals(tableType)) { String tableName = rs.getString("TABLE_NAME"); Table table = new Table(); table.setName(tableName); List<Column> columns = getColumns(dataSource, metaData, table); table.setColumns(columns); tableNames.add(table); } } return tableNames; }