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.lotaris.maven.plugin.dbunit.OperationMojo.java
@Override @SuppressWarnings("unchecked") public void execute() throws MojoExecutionException, MojoFailureException { if (skip) {/*w w w. j a va 2 s.co m*/ this.getLog().info("Skip operation: " + type + " execution"); return; } super.execute(); List concatenatedSources = new ArrayList(); CollectionUtils.addIgnoreNull(concatenatedSources, src); if (sources != null) { concatenatedSources.addAll(Arrays.asList(sources)); } try { IDatabaseConnection connection = createConnection(); // Force the database table to be empty before importing data if (clearAllTables) { Connection con = connection.getConnection(); DatabaseMetaData meta = con.getMetaData(); try (ResultSet rs = meta.getTables(null, null, "%", new String[] { "TABLE" })) { while (rs.next()) { String tName = rs.getString("TABLE_NAME"); // Truncate the data con.createStatement().execute("TRUNCATE " + tName + ";"); } } } try { for (Iterator i = concatenatedSources.iterator(); i.hasNext();) { File source = (File) i.next(); Operation op = new Operation(); op.setFormat(format); op.setSrc(source); op.setTransaction(transaction); op.setType(type); op.execute(connection); } } finally { connection.close(); } } catch (Exception e) { throw new MojoExecutionException("Error executing database operation: " + type, e); } }
From source file:com.moss.blankslate.PostgresqlCatalogFactory.java
private void dropSchema(String schemaName, Connection connection) throws SQLException { ResultSet schemas = connection.getMetaData().getSchemas(); boolean hasSchema = false; while (schemas.next()) { String nextSchema = schemas.getString("TABLE_SCHEM"); log.debug("Found schema " + nextSchema); if (nextSchema.equals(schemaName)) hasSchema = true;/*from ww w. j av a 2 s. c o m*/ } if (hasSchema) { log.debug("Dropping schema \"" + schemaName + "\""); connection.createStatement().execute("DROP SCHEMA " + schemaName); connection.commit(); } else { log.debug("Could not find schema to drop: \"" + schemaName + "\""); } }
From source file:com.migratebird.database.IdentifierProcessorFactory.java
/** * Determines the string used to quote identifiers to make them case-sensitive. This will use the connections * database metadata to determine the quote string. * * @param customIdentifierQuoteString If not null, it specifies a custom identifier quote string that replaces the one * specified by the JDBC DatabaseMetaData object * @param dataSource The datas ource, not null * @return The quote string, null if quoting is not supported *//* w w w . j a v a 2s . co m*/ protected String determineIdentifierQuoteString(String customIdentifierQuoteString, DataSource dataSource) { if (customIdentifierQuoteString != null) { return trimToNull(customIdentifierQuoteString); } Connection connection = null; try { connection = dataSource.getConnection(); DatabaseMetaData databaseMetaData = connection.getMetaData(); String quoteString = databaseMetaData.getIdentifierQuoteString(); return trimToNull(quoteString); } catch (SQLException e) { throw new DatabaseException("Unable to determine identifier quote string.", e); } finally { closeQuietly(connection, null, null); } }
From source file:de.iritgo.aktario.jdbc.GetDatabaseVersion.java
/** * Perform the command.//w w w .ja v a 2 s .c o m * * @return The database name and version. */ public Object performWithResult() { JDBCManager jdbcManager = (JDBCManager) Engine.instance().getManager("persist.JDBCManager"); DataSource dataSource = jdbcManager.getDefaultDataSource(); Connection connection = null; try { if (version == null) { connection = dataSource.getConnection(); DatabaseMetaData meta = connection.getMetaData(); version = meta.getDatabaseProductName() + " " + meta.getDatabaseProductVersion(); } return version; } catch (SQLException x) { Log.logError("persist", "Insert", "Unable to get database meta data: " + x); } finally { DbUtils.closeQuietly(connection); } return null; }
From source file:edu.northwestern.bioinformatics.studycalendar.dao.auditing.AuditEventDao.java
@SuppressWarnings("unchecked") private String determineDatabaseType() { String databaseType = (String) jdbcTemplate.execute(new ConnectionCallback() { public Object doInConnection(Connection connection) throws SQLException, DataAccessException { return connection.getMetaData().getDatabaseProductName(); }/* w w w . java2 s. c o m*/ }); return databaseType; }
From source file:com.emergya.persistenceGeo.dbutils.DatabaseUtilsImpl.java
public String getColumnType(String tableName, String columnName) { ResultSet rs = null;//from w w w. j av a2 s. c om String result = null; try { Connection con = ds.getConnection(); DatabaseMetaData md = con.getMetaData(); rs = md.getColumns(null, null, tableName, columnName); int i = 0; while (rs.next()) { i++; result = rs.getString("TYPE_NAME"); if (i > 1) { throw new DbUtilsException( "Found more than one column when looking for column's type. [tableName=" + tableName + ", columnName=" + columnName + "]"); } } con.close(); } catch (SQLException e) { throw new DbUtilsException("Error getting type of column", e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { throw new DbUtilsException("Error accessing database", e); } } } return result; }
From source file:com.migratebird.database.IdentifierProcessorFactory.java
/** * Determines the case the database uses to store non-quoted identifiers. This will use the connections * database metadata to determine the correct case. * * @param customStoredIdentifierCase The stored case: possible values 'lower_case', 'upper_case', 'mixed_case' and 'auto' * @param dataSource The datas ource, not null * @return The stored case, not null//from w w w .j a v a 2 s. c o m */ protected StoredIdentifierCase determineStoredIdentifierCase(StoredIdentifierCase customStoredIdentifierCase, DataSource dataSource) { if (customStoredIdentifierCase != null) { return customStoredIdentifierCase; } Connection connection = null; try { connection = dataSource.getConnection(); DatabaseMetaData databaseMetaData = connection.getMetaData(); if (databaseMetaData.storesUpperCaseIdentifiers()) { return UPPER_CASE; } else if (databaseMetaData.storesLowerCaseIdentifiers()) { return LOWER_CASE; } else { return MIXED_CASE; } } catch (SQLException e) { throw new DatabaseException("Unable to determine stored identifier case.", e); } finally { closeQuietly(connection, null, null); } }
From source file:com.snowstore.mercury.indicator.DataSourceHealthIndicator.java
private String getProduct() { return this.jdbcTemplate.execute(new ConnectionCallback<String>() { @Override/*from w w w . j a va2 s .c o m*/ public String doInConnection(Connection connection) throws SQLException, DataAccessException { return connection.getMetaData().getDatabaseProductName() + ";" + connection.getMetaData().getURL(); } }); }
From source file:com.thoughtworks.go.server.database.H2DatabaseTest.java
@Test void shouldUpgradeDatabase() throws SQLException { h2Database.shutdown();// w w w . ja v a 2s .c om h2Database.startDatabase(); BasicDataSource dataSource = h2Database.createDataSource(); h2Database.upgrade(); h2Database.startDatabase(); dataSource = h2Database.createDataSource(); Connection connection = dataSource.getConnection(); ResultSet set = connection.getMetaData().getTables(null, null, null, null); assertThat(set.next(), is(true)); }
From source file:mondrian.spi.impl.JdbcDialectImpl.java
/** * Helper method to determine if a connection would work with * a given database product. This can be used to differenciate * between databases which use the same driver as others. * * <p>It will first try to use/*from w ww .ja v a 2 s. c o m*/ * {@link DatabaseMetaData#getDatabaseProductName()} and match the * name of {@link DatabaseProduct} passed as an argument. * * <p>If that fails, it will try to execute <code>select version();</code> * and obtains some information directly from the server. * * @param databaseProduct Database product instance * @param connection SQL connection * @return true if a match was found. false otherwise. */ protected static boolean isDatabase(DatabaseProduct databaseProduct, Connection connection) { Statement statement = null; ResultSet resultSet = null; String dbProduct = databaseProduct.name().toLowerCase(); try { // Quick and dirty check first. if (connection.getMetaData().getDatabaseProductName().toLowerCase().contains(dbProduct)) { LOGGER.debug("Using " + databaseProduct.name() + " dialect"); return true; } // Let's try using version(). statement = connection.createStatement(); resultSet = statement.executeQuery("select version()"); if (resultSet.next()) { String version = resultSet.getString(1); LOGGER.debug("Version=" + version); if (version != null) { if (version.toLowerCase().contains(dbProduct)) { LOGGER.info("Using " + databaseProduct.name() + " dialect"); return true; } } } LOGGER.debug("NOT Using " + databaseProduct.name() + " dialect"); return false; } catch (SQLException e) { LOGGER.debug("NOT Using " + databaseProduct.name() + " dialect.", e); return false; } finally { Util.close(resultSet, statement, null); } }