Example usage for java.sql DatabaseMetaData getDatabaseMajorVersion

List of usage examples for java.sql DatabaseMetaData getDatabaseMajorVersion

Introduction

In this page you can find the example usage for java.sql DatabaseMetaData getDatabaseMajorVersion.

Prototype

int getDatabaseMajorVersion() throws SQLException;

Source Link

Document

Retrieves the major version number of the underlying database.

Usage

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

/**
 * @param dbConfig/*from w w w  .  ja v 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:org.alfresco.hibernate.DialectFactoryBean.java

@SuppressWarnings("deprecation")
@Override//w  w  w  .  j a  v  a  2s.co m
public Dialect getObject() throws SQLException {
    Session session = ((SessionFactory) this.localSessionFactory.getObject()).openSession();
    Configuration cfg = this.localSessionFactory.getConfiguration();
    Connection con = null;
    try {
        // make sure that we AUTO-COMMIT
        con = session.connection();
        con.setAutoCommit(true);
        DatabaseMetaData meta = con.getMetaData();
        Dialect dialect = DialectFactory.buildDialect(cfg.getProperties(), meta.getDatabaseProductName(),
                meta.getDatabaseMajorVersion());
        dialect = changeDialect(cfg, dialect);
        return dialect;
    } finally {
        try {
            con.close();
        } catch (Exception e) {
        }
    }
}

From source file:annis.administration.AbstractAdminstrationDao.java

/**
 * Closes all open idle connections. The current data source
 * must have superuser rights.//from www  . j a  v a 2  s .c o  m
 * 
 * This can be used if a another database action needs full access to a database,
 * e.g. when deleting and then creating it
 * @param databasename
 */
protected void closeAllConnections(String databasename) {
    String sql = "SELECT pg_terminate_backend(pg_stat_activity.pid)\n" + "FROM pg_stat_activity\n"
            + "WHERE pg_stat_activity.datname = ?\n" + "  AND pid <> pg_backend_pid();";
    try (Connection conn = getDataSource().getConnection()) {
        DatabaseMetaData meta = conn.getMetaData();

        if (meta.getDatabaseMajorVersion() == 9 && meta.getDatabaseMinorVersion() <= 1) {
            sql = "SELECT pg_terminate_backend(pg_stat_activity.procpid)\n" + "FROM pg_stat_activity\n"
                    + "WHERE pg_stat_activity.datname = ?\n" + "  AND procpid <> pg_backend_pid();";
        }
    } catch (SQLException ex) {
        log.warn("Could not get the PostgreSQL version", ex);
    }

    getJdbcTemplate().queryForRowSet(sql, databasename);

}

From source file:com.mmnaseri.dragonfly.dialect.impl.Mysql5Dialect.java

@Override
public boolean accepts(DatabaseMetaData databaseMetaData) {
    try {// w  w w  . ja  v  a  2 s  .  co  m
        return databaseMetaData.getDatabaseProductName().toLowerCase().matches("mysql")
                && databaseMetaData.getDatabaseMajorVersion() == 5;
    } catch (SQLException e) {
        throw new DatabaseMetadataAccessError(e);
    }
}

From source file:it.greenvulcano.gvesb.j2ee.db.connections.impl.DriverPoolConnectionBuilder.java

public Connection getConnection() throws GVDBException {
    try {/*from  w  w w .j  a va  2s.co m*/
        Connection conn = dataSource.getConnection();
        if (debugJDBCConn) {
            logger.debug("Created JDBC Connection [" + name + "]: [" + conn + "]");
            if (isFirst) {
                isFirst = false;
                DatabaseMetaData dbmd = conn.getMetaData();

                logger.debug("=====  Database info =====");
                logger.debug("DatabaseProductName: " + dbmd.getDatabaseProductName());
                logger.debug("DatabaseProductVersion: " + dbmd.getDatabaseProductVersion());
                logger.debug("DatabaseMajorVersion: " + dbmd.getDatabaseMajorVersion());
                logger.debug("DatabaseMinorVersion: " + dbmd.getDatabaseMinorVersion());
                logger.debug("=====  Driver info =====");
                logger.debug("DriverName: " + dbmd.getDriverName());
                logger.debug("DriverVersion: " + dbmd.getDriverVersion());
                logger.debug("DriverMajorVersion: " + dbmd.getDriverMajorVersion());
                logger.debug("DriverMinorVersion: " + dbmd.getDriverMinorVersion());
                logger.debug("=====  JDBC/DB attributes =====");
                if (dbmd.supportsGetGeneratedKeys())
                    logger.debug("Supports getGeneratedKeys(): true");
                else
                    logger.debug("Supports getGeneratedKeys(): false");
            }
        }

        return conn;
    } catch (Exception exc) {
        throw new GVDBException("DriverPoolConnectionBuilder - Error while creating Connection[" + name + "]",
                exc);
    }
}

From source file:net.risesoft.soa.asf.web.controller.SystemController.java

private List<SysInfo> getDBInfo() {
    List list = new ArrayList();
    String group = "4. ??";
    try {//from  w w  w.  ja va  2 s  .com
        Connection conn = this.basicDataSource.getConnection();
        try {
            DatabaseMetaData dbmd = conn.getMetaData();
            list.add(new SysInfo("DatabaseProductName", dbmd.getDatabaseProductName(), group));
            list.add(new SysInfo("DatabaseProductVersion", dbmd.getDatabaseProductVersion(), group));
            list.add(new SysInfo("DatabaseMajorVersion", Integer.valueOf(dbmd.getDatabaseMajorVersion()),
                    group));
            list.add(new SysInfo("DatabaseMinorVersion", Integer.valueOf(dbmd.getDatabaseMinorVersion()),
                    group));
            list.add(new SysInfo("DriverName", dbmd.getDriverName(), group));
            list.add(new SysInfo("DriverVersion", dbmd.getDriverVersion(), group));
            list.add(new SysInfo("DriverMajorVersion", Integer.valueOf(dbmd.getDriverMajorVersion()), group));
            list.add(new SysInfo("DriverMinorVersion", Integer.valueOf(dbmd.getDriverMinorVersion()), group));
        } finally {
            conn.close();
        }
        group = "5. ?";
        BasicDataSource bds = this.basicDataSource;
        list.add(new SysInfo("InitialSize", Integer.valueOf(bds.getInitialSize()), group));
        list.add(new SysInfo("MaxActive", Integer.valueOf(bds.getMaxActive()), group));
        list.add(new SysInfo("MaxIdle", Integer.valueOf(bds.getMaxIdle()), group));
        list.add(new SysInfo("MinIdle", Integer.valueOf(bds.getMinIdle()), group));
        list.add(new SysInfo("MaxWait", Long.valueOf(bds.getMaxWait()), group));
        list.add(new SysInfo("NumActive", Integer.valueOf(bds.getNumActive()), group));
        list.add(new SysInfo("NumIdle", Integer.valueOf(bds.getNumIdle()), group));
        list.add(new SysInfo("DriverClass", bds.getDriverClassName(), group));
        list.add(new SysInfo("URL", bds.getUrl(), group));
        list.add(new SysInfo("Username", bds.getUsername(), group));
        list.add(new SysInfo("Password", "******", group));
    } catch (Exception ex) {
        log.warn("???: " + ex.getMessage(), ex);
    }
    return list;
}

From source file:com.manydesigns.portofino.model.database.ConnectionProvider.java

public void init(DatabasePlatformsRegistry databasePlatformsRegistry) {
    Connection conn = null;/*from   w ww  . j  a  v a2 s. c  o m*/
    ResultSet typeRs = null;
    String databaseName = getDatabase().getDatabaseName();
    try {
        conn = acquireConnection();

        DatabaseMetaData metadata = conn.getMetaData();

        databaseProductName = metadata.getDatabaseProductName();
        databaseProductVersion = metadata.getDatabaseProductVersion();

        try {
            databaseMajorVersion = metadata.getDatabaseMajorVersion();
            databaseMinorVersion = metadata.getDatabaseMinorVersion();
            databaseMajorMinorVersion = MessageFormat.format("{0}.{1}", databaseMajorVersion,
                    databaseMinorVersion);
        } catch (SQLException e) {
            databaseMajorMinorVersion = e.getMessage();
        }

        driverName = metadata.getDriverName();
        driverVersion = metadata.getDriverVersion();

        driverMajorVersion = metadata.getDriverMajorVersion();
        driverMinorVersion = metadata.getDriverMinorVersion();
        driverMajorMinorVersion = MessageFormat.format("{0}.{1}", driverMajorVersion, driverMinorVersion);

        try {
            JDBCMajorVersion = metadata.getJDBCMajorVersion();
            JDBCMinorVersion = metadata.getJDBCMinorVersion();
            JDBCMajorMinorVersion = MessageFormat.format("{0}.{1}", JDBCMajorVersion, JDBCMinorVersion);
        } catch (Throwable e) {
            JDBCMajorMinorVersion = e.getMessage();
        }

        // extract supported types
        types.clear();
        typeRs = metadata.getTypeInfo();
        while (typeRs.next()) {
            readType(typeRs);
        }
        fixMissingTypeAliases(types);
        Collections.sort(types, new TypeComparator());

        databasePlatform = databasePlatformsRegistry.findApplicableAbstraction(this);
        if (databasePlatform == null) {
            status = STATUS_ERROR;
            errorMessage = MessageFormat.format("Database platform not found for {0}", databaseProductName);
            logger.warn(errorMessage);
        } else {
            status = STATUS_CONNECTED;
            errorMessage = null;
        }
    } catch (Throwable e) {
        status = STATUS_ERROR;
        errorMessage = e.getMessage();
        logger.warn("Could not create database platform for " + databaseName, e);
    } finally {
        DbUtil.closeResultSetAndStatement(typeRs);
        releaseConnection(conn);
        lastTested = new Date();
    }
}

From source file:com.alibaba.dubbo.governance.status.DatabaseStatusChecker.java

public Status check() {
    boolean ok;/* w ww  .  j av  a 2s .c  o m*/
    try {
        Connection connection = dataSource.getConnection();
        try {
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet resultSet = metaData.getTypeInfo();
            try {
                ok = resultSet.next();
            } finally {
                resultSet.close();
            }
            if (message == null) {
                message = metaData.getURL() + " (" + metaData.getDatabaseProductName() + " "
                        + metaData.getDatabaseProductVersion() + ", "
                        + getIsolation(metaData.getDefaultTransactionIsolation()) + ")";
            }
            if (version == 0) {
                version = metaData.getDatabaseMajorVersion();
            }
        } finally {
            connection.close();
        }
    } catch (Throwable e) {
        logger.error(e.getMessage(), e);
        ok = false;
    }
    return new Status(!ok ? Status.Level.ERROR : (version < 5 ? Status.Level.WARN : Status.Level.OK), message);
}

From source file:com.jaxio.celerio.configuration.database.support.MetadataExtractor.java

private DatabaseInfo extractDatabaseInfo(DatabaseMetaData databaseMetaData) {
    DatabaseInfo database = new DatabaseInfo();

    // database/*from w ww  .  j  a v a  2 s  . c o m*/
    try {
        database.setDatabaseProductName(databaseMetaData.getDatabaseProductName());
    } catch (Exception e) { /* ignore */
    }

    try {
        database.setDatabaseProductVersion(databaseMetaData.getDatabaseProductVersion());
    } catch (Exception e) { /* ignore */
    }

    try {
        database.setDatabaseMajorVersion(databaseMetaData.getDatabaseMajorVersion());
    } catch (Exception e) { /* ignore */
    }

    try {
        database.setDatabaseMinorVersion(databaseMetaData.getDatabaseMinorVersion());
    } catch (Exception e) { /* ignore */
    }

    // driver
    try {
        database.setDriverName(databaseMetaData.getDriverName());
    } catch (Exception e) { /* ignore */
    }

    try {
        database.setDriverVersion(databaseMetaData.getDriverVersion());
    } catch (Exception e) { /* ignore */
    }

    try {
        database.setDriverMajorVersion(databaseMetaData.getDriverMajorVersion());
    } catch (Exception e) { /* ignore */
    }

    try {
        database.setDriverMinorVersion(databaseMetaData.getDriverMinorVersion());
    } catch (Exception e) { /* ignore */
    }

    return database;
}

From source file:net.sf.jasperreports.engine.query.JRJdbcQueryExecuter.java

/**
 * // w w  w.ja  va  2  s. c  o m
 */
public JRJdbcQueryExecuter(JasperReportsContext jasperReportsContext, JRDataset dataset,
        Map<String, ? extends JRValueParameter> parameters) {
    super(jasperReportsContext, dataset, parameters);

    connection = (Connection) getParameterValue(JRParameter.REPORT_CONNECTION);
    if (connection == null) {
        if (log.isWarnEnabled()) {
            log.warn("The supplied java.sql.Connection object is null.");
        }
    } else if (log.isDebugEnabled()) {
        try {
            DatabaseMetaData metaData = connection.getMetaData();
            log.debug("DB is " + metaData.getDatabaseProductName() + " version "
                    + metaData.getDatabaseProductVersion() + " (" + metaData.getDatabaseMajorVersion() + "/"
                    + metaData.getDatabaseMinorVersion() + ")");
            log.debug("driver is " + metaData.getDriverName() + " version " + metaData.getDriverVersion() + " ("
                    + metaData.getDriverMajorVersion() + "/" + metaData.getDriverMinorVersion() + ")");
            log.debug("jdbc " + metaData.getJDBCMajorVersion() + "/" + metaData.getJDBCMinorVersion());
            log.debug("connection URL is " + metaData.getURL());
        } catch (SQLException e) {
            log.debug("failed to read connection metadata", e);
        }
    }

    isCachedRowSet = getBooleanParameterOrProperty(JRJdbcQueryExecuterFactory.PROPERTY_CACHED_ROWSET, false);

    setTimeZone();

    registerFunctions();

    parseQuery();
}