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.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;// w w w.ja va 2 s. c o 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); } } }
From source file:ca.nrc.cadc.db.DBUtil.java
private static void testDS(DataSource ds, boolean keepOpen) throws SQLException { Connection con = null; try {//from ww w . j av a 2 s .co m con = ds.getConnection(); DatabaseMetaData meta = con.getMetaData(); if (!log.getEffectiveLevel().equals(Level.DEBUG)) return; log.debug("connected to server: " + meta.getDatabaseProductName() + " " + meta.getDatabaseMajorVersion() + "." + meta.getDatabaseMinorVersion() + " driver: " + meta.getDriverName() + " " + meta.getDriverMajorVersion() + "." + meta.getDriverMinorVersion()); } finally { if (!keepOpen && con != null) try { con.close(); } catch (Exception ignore) { } } }
From source file:com.example.querybuilder.server.Jdbc.java
public static DatabaseMetaData getMetaData(Connection connection) { try {/* w ww . ja v a 2s .c o m*/ return connection.getMetaData(); } catch (SQLException e) { throw new SqlRuntimeException(e); } }
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 ww.j a v a 2 s . c o 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); } } }
From source file:com.jaspersoft.jasperserver.api.engine.common.virtualdatasourcequery.VirtualSQLDataSource.java
public static Set<String> discoverNonEmptySchemas(Connection conn) throws SQLException { DatabaseMetaData dbMetaData = conn.getMetaData(); try {/*from w w w . j a v a2s .c om*/ Set<String> set = new LinkedHashSet<String>(); Set<String> schemaList = getResult(dbMetaData.getSchemas(), TABLE_SCHEM); String[] types = { "TABLE" }; for (String schema : schemaList) { ResultSet rs2 = null; try { rs2 = conn.getMetaData().getTables(null, schema, null, types); if (rs2.next()) set.add(schema); else log.debug(schema + " schema contains no table. Ignore in VDS"); /*** take too long to check empty tables Set<String> tableList = getResult(conn.getMetaData().getTables(null, schema, null, types), TABLE_NAME); Set<String> tableWithColsList = getResult(conn.getMetaData().getColumns(null, schema, null, null), TABLE_NAME); if (tableList.size() == 0) { log.debug(schema + " schema contains no table. Ignore in VDS"); continue; } else { set.add(schema); // does all tables contain columns boolean doesAllTablesContainCols = true; for (String tableName : tableList) { if (!tableWithColsList.contains(tableName)) { log.debug(schema + "." + tableName + " table contains table with no column. Ignore in VDS"); doesAllTablesContainCols = false; break; } } if (doesAllTablesContainCols) set.add(schema); } ****/ } catch (SQLException ex2) { log.debug("Fail to read schema, " + schema + ". Ignore in VDS"); ex2.printStackTrace(); } finally { if (rs2 != null) rs2.close(); } } return set; } catch (SQLException ex) { log.error("Cannot get schemas", ex); throw ex; } }
From source file:com.aurel.track.admin.server.status.ServerStatusBL.java
private static void loadDatabaseInfo(ServerStatusTO serverStatusTO) { Connection conn = null; try {//from w ww .ja v a 2s . c o m conn = Torque.getConnection(BaseTSitePeer.DATABASE_NAME); DatabaseMetaData dbm = conn.getMetaData(); serverStatusTO.setDatabase(dbm.getDatabaseProductName() + " " + dbm.getDatabaseProductVersion()); serverStatusTO.setJdbcDriver(dbm.getDriverName() + " " + dbm.getDriverVersion()); serverStatusTO.setJdbcUrl(dbm.getURL()); } catch (Exception e) { LOGGER.error("Problem retrieving database meta data: " + e.getMessage()); } finally { if (conn != null) { Torque.closeConnection(conn); } } Double ping = loadPing(); serverStatusTO.setPingTime(ping.toString() + " ms"); }
From source file:com.glaf.core.jdbc.DBConnectionFactory.java
public static String getDatabaseType(Connection connection) { if (connection != null) { String databaseProductName = null; try {/*from www. j av a2 s . co m*/ DatabaseMetaData databaseMetaData = connection.getMetaData(); databaseProductName = databaseMetaData.getDatabaseProductName(); } catch (SQLException ex) { ex.printStackTrace(); throw new RuntimeException(ex); } String dbType = databaseTypeMappings.getProperty(databaseProductName); if (dbType == null) { throw new RuntimeException( "couldn't deduct database type from database product name '" + databaseProductName + "'"); } return dbType; } return null; }
From source file:com.anyi.gp.license.RegisterTools.java
public static String getDBServerURL() { Connection conn = null; try {/*from ww w . j av a2s . co m*/ conn = DAOFactory.getInstance().getConnection(); if (conn != null) { DatabaseMetaData meta = conn.getMetaData(); return (meta.getURL() + ":" + meta.getUserName()).toUpperCase(); } } catch (SQLException e) { e.printStackTrace(); } finally { DBHelper.closeConnection(conn); } return ""; }
From source file:net.antidot.sql.model.core.SQLConnector.java
/** * Drop all tables from database with connection c. Specific to MySQL * databases./*from w w w.j a va2s. 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.mirth.connect.server.util.DatabaseUtil.java
/** * Tell whether or not the given index exists in the database */// w ww . jav a2 s. c o m public static boolean indexExists(Connection connection, String tableName, String indexName) { if (!tableExists(connection, tableName)) { return false; } ResultSet resultSet = null; try { DatabaseMetaData metaData = connection.getMetaData(); resultSet = metaData.getIndexInfo(null, null, tableName.toUpperCase(), false, false); while (resultSet.next()) { if (indexName.equalsIgnoreCase(resultSet.getString("INDEX_NAME"))) { return true; } } resultSet = metaData.getIndexInfo(null, null, tableName.toLowerCase(), false, false); while (resultSet.next()) { if (indexName.equalsIgnoreCase(resultSet.getString("INDEX_NAME"))) { return true; } } return false; } catch (SQLException e) { throw new DonkeyDaoException(e); } finally { DbUtils.closeQuietly(resultSet); } }