Example usage for java.sql Connection getMetaData

List of usage examples for java.sql Connection getMetaData

Introduction

In this page you can find the example usage for java.sql Connection getMetaData.

Prototype

DatabaseMetaData getMetaData() throws SQLException;

Source Link

Document

Retrieves a DatabaseMetaData object that contains metadata about the database to which this Connection object represents a connection.

Usage

From source file:com.oracle2hsqldb.spring.MetaDataJdbcTemplate.java

public void query(RowCallbackHandler rowHandler) {
    Connection connection = getConnection();

    ResultSet results = null;/*from  w w  w  .  j ava 2 s  . co m*/
    try {
        results = getResults(connection.getMetaData());
        while (results.next()) {
            rowHandler.processRow(results);
        }
    } catch (SQLException e) {
        throw new MetaDataAccessException(e);
    } finally {
        try {
            if (results != null)
                results.close();
        } catch (SQLException e) {
            log.warn("could not close the ResultSet", e);
        }
        try {
            if (connection != null)
                connection.close();
        } catch (SQLException e) {
            throw new CleanupFailureDataAccessException("could not close Connection", e);
        }
    }
}

From source file:com.tera.common.database.factory.AbstractDatabaseFactory.java

/**
 * @param dbConfig/*from  www  .  j av  a 2 s .c  om*/
 * @throws Error
 */
void createConnectionPool(DatabaseConfiguration dbConfig) throws Error {
    log.info("Creating DB pool");
    connectionPool = new GenericObjectPool();
    connectionPool.setMaxIdle(dbConfig.getConnectionsIdelMax());
    connectionPool.setMinIdle(dbConfig.getConnectionsIdleMin());
    connectionPool.setMaxActive(dbConfig.getConnectionsActiveMax());
    connectionPool.setTestOnBorrow(true);

    try {
        dataSource = setupDataSource(dbConfig);
        Connection c = getConnection();
        DatabaseMetaData dmd = c.getMetaData();
        databaseName = dmd.getDatabaseProductName();
        databaseMajorVersion = dmd.getDatabaseMajorVersion();
        databaseMinorVersion = dmd.getDatabaseMinorVersion();
        c.close();
    } catch (Exception e) {
        log.error("Error with connection string: {}", dbConfig, e);
        throw new Error("DatabaseFactory not initialized!");
    }
    log.info("Successfully connected to the database");
}

From source file:com.netspective.axiom.DatabasePolicies.java

public DatabasePolicy getDatabasePolicy(Connection conn) throws SQLException {
    String databaseProductName = conn.getMetaData().getDatabaseProductName();
    DatabasePolicy policy = getDatabasePolicy(databaseProductName);
    if (policy == null) {
        log.error("Database policy not found for database '" + databaseProductName
                + "', using 'ansi'. Available: " + policiesById.keySet());
        return getDatabasePolicy(DBMSID_DEFAULT);
    } else//from w  ww.j a  v  a  2  s . c  om
        return policy;
}

From source file:it.emacro.extractor.db.ConnectionPool.java

public void inspect() throws SQLException {
    Connection conn = getConnection();
    try {//from w ww.j a va  2 s .  c  o m
        DatabaseMetaData dmd = conn.getMetaData();
        sqlStateType = dmd.getSQLStateType();
        // dumpDBMDInfo();
    } finally {
        closeConnection(conn);
    }
}

From source file:com.googlecode.psiprobe.controllers.sql.ConnectionTestController.java

protected ModelAndView handleContext(String contextName, Context context, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    String resourceName = ServletRequestUtils.getStringParameter(request, "resource");
    DataSource dataSource = null;

    try {/*from w  w w  .j a va 2s .  c o m*/
        dataSource = getContainerWrapper().getResourceResolver().lookupDataSource(context, resourceName,
                getContainerWrapper());
    } catch (NamingException e) {
        request.setAttribute("errorMessage", getMessageSourceAccessor()
                .getMessage("probe.src.dataSourceTest.resource.lookup.failure", new Object[] { resourceName }));
    }

    if (dataSource == null) {
        request.setAttribute("errorMessage", getMessageSourceAccessor()
                .getMessage("probe.src.dataSourceTest.resource.lookup.failure", new Object[] { resourceName }));
    } else {
        try {
            // TODO: use Spring's jdbc template?
            Connection conn = dataSource.getConnection();
            try {
                DatabaseMetaData md = conn.getMetaData();

                List dbMetaData = new ArrayList();

                addDbMetaDataEntry(dbMetaData, "probe.jsp.dataSourceTest.dbMetaData.dbProdName",
                        md.getDatabaseProductName());
                addDbMetaDataEntry(dbMetaData, "probe.jsp.dataSourceTest.dbMetaData.dbProdVersion",
                        md.getDatabaseProductVersion());
                addDbMetaDataEntry(dbMetaData, "probe.jsp.dataSourceTest.dbMetaData.jdbcDriverName",
                        md.getDriverName());
                addDbMetaDataEntry(dbMetaData, "probe.jsp.dataSourceTest.dbMetaData.jdbcDriverVersion",
                        md.getDriverVersion());
                //                    addDbMetaDataEntry(dbMetaData, "probe.jsp.dataSourceTest.dbMetaData.jdbcVersion", String.valueOf(md.getJDBCMajorVersion()));

                return new ModelAndView(getViewName(), "dbMetaData", dbMetaData);
            } finally {
                conn.close();
            }
        } catch (SQLException e) {
            String message = getMessageSourceAccessor()
                    .getMessage("probe.src.dataSourceTest.connection.failure", new Object[] { e.getMessage() });
            logger.error(message, e);
            request.setAttribute("errorMessage", message);
        }
    }

    return new ModelAndView(getViewName());
}

From source file:org.wte4j.examples.showcase.server.config.DatabaseConfigTest.java

@Test
public void databaseIsIntializedTest() {
    JdbcTemplate template = new JdbcTemplate(ds);
    Set<String> wte4jTables = template.execute(new ConnectionCallback<Set<String>>() {

        @Override/*from www.j av  a 2  s.c  om*/
        public Set<String> doInConnection(Connection con) throws SQLException, DataAccessException {
            Set<String> tableNames = new HashSet<String>();
            ResultSet tableRs = con.getMetaData().getTables(null, null, null, new String[] { "TABLE" });
            try {
                while (tableRs.next()) {
                    tableNames.add(tableRs.getString("TABLE_NAME").toLowerCase());
                }
            } finally {
                tableRs.close();
            }
            return tableNames;
        }
    });
    assertEquals(6, wte4jTables.size());
    assertTrue(wte4jTables.contains("person"));
    assertTrue(wte4jTables.contains("purchase_order"));
    assertTrue(wte4jTables.contains("wte4j_template"));
    assertTrue(wte4jTables.contains("wte4j_template_properties"));
    assertTrue(wte4jTables.contains("wte4j_gen"));
    assertTrue(wte4jTables.contains("wte4j_template_content_mapping"));
}

From source file:gov.nih.nci.cabig.caaers.datamigrator.CaaersDataMigratorTemplate.java

private String getDBName() {
    if (DB_PRODUCT_VERSION == null) {
        DB_PRODUCT_VERSION = (String) getJdbcTemplate().execute(new ConnectionCallback() {
            public Object doInConnection(Connection con) throws SQLException, DataAccessException {
                return con.getMetaData().getDatabaseProductVersion();
            }/*from w  w w . j ava  2s .c o m*/
        });
    }

    return DB_PRODUCT_VERSION;
}

From source file:jp.co.tis.gsp.tools.db.EntityDependencyParser.java

public void parse(Connection conn, String url, String normalizedSchemaName) {
    try {/*www.  j a  va  2 s  .  c o  m*/
        DatabaseMetaData metaData = conn.getMetaData();
        List<String> tableNames = getAllTableNames(metaData, normalizedSchemaName);
        for (String tableName : tableNames) {
            parseReference(metaData, normalizedSchemaName, tableName);
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.googlecode.flyway.core.dbsupport.db2.DB2DbSupport.java

public boolean tableExists(final String schema, final String table) {
    return (Boolean) jdbcTemplate.execute(new ConnectionCallback() {
        public Boolean doInConnection(Connection connection) throws SQLException, DataAccessException {
            ResultSet resultSet = connection.getMetaData().getTables(null, schema.toUpperCase(),
                    table.toUpperCase(), null);
            return resultSet.next();
        }//from  w  w w  .  ja  v  a  2 s . c  o m
    });
}

From source file:com.emergya.persistenceGeo.dbutils.DatabaseUtilsImpl.java

@Override
public List<String> getTableColumns(String tableName) {
    ResultSet rs = null;//from  ww w  .  ja  v  a 2 s .c  om
    List<String> result = new ArrayList<String>();
    try {
        Connection con = ds.getConnection();
        DatabaseMetaData md = con.getMetaData();
        rs = md.getColumns(null, null, tableName, null);
        while (rs.next()) {
            result.add(rs.getString("COLUMN_NAME"));
        }
        con.close();

    } catch (SQLException e) {
        throw new DbUtilsException("Error changing column names", e);
    } finally {
        if (rs != null) {
            try {
                rs.close();

            } catch (SQLException e) {
                throw new DbUtilsException("Error accessing database", e);
            }
        }
    }
    return result;
}