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:org.openconcerto.sql.model.SQLBase.java

public synchronized int[] getVersion() throws SQLException {
    if (this.dbVersion == null) {
        this.dbVersion = this.getDataSource()
                .useConnection(new ConnectionHandlerNoSetup<int[], SQLException>() {
                    @Override/*w  w  w  . j a  v  a 2s. co  m*/
                    public int[] handle(SQLDataSource ds) throws SQLException, SQLException {
                        final DatabaseMetaData md = ds.getConnection().getMetaData();
                        return new int[] { md.getDatabaseMajorVersion(), md.getDatabaseMinorVersion() };
                    }
                });
    }
    return this.dbVersion;
}

From source file:org.openmrs.module.emrmonitor.api.db.hibernate.HibernateEmrMonitorDAO.java

@Override
public Map<String, String> getDatabaseMetadata() {
    final Map<String, String> ret = new LinkedHashMap<String, String>();
    sessionFactory.getCurrentSession().doWork(new Work() {
        @Override//  ww  w. ja  v a  2 s  . c om
        public void execute(Connection connection) throws SQLException {
            DatabaseMetaData md = connection.getMetaData();
            ret.put("product.name", md.getDatabaseProductName());
            ret.put("product.version", md.getDatabaseProductVersion());
            ret.put("product.majorVersion", Integer.toString(md.getDatabaseMajorVersion()));
            ret.put("product.minorVersion", Integer.toString(md.getDatabaseMinorVersion()));
        }
    });
    return ret;
}

From source file:org.rhq.plugins.oracle.OracleDiscoveryComponent.java

public DiscoveredResourceDetails discoverResource(Configuration pluginConfig,
        ResourceDiscoveryContext resourceDiscoveryContext) throws InvalidPluginConfigurationException {

    Connection connection = null;
    try {//from ww  w. j a  va 2 s.  co  m
        connection = OracleServerComponent.buildConnection(pluginConfig);
        DatabaseMetaData dbmd = connection.getMetaData();
        String version = dbmd.getDatabaseMajorVersion() + "." + dbmd.getDatabaseMinorVersion();
        DiscoveredResourceDetails details = createResourceDetails(resourceDiscoveryContext, pluginConfig,
                version, null);
        return details;
    } catch (Exception e) {
        log.warn("Could not connect to oracle with supplied configuration", e);
        throw new InvalidPluginConfigurationException("Unable to connect to Oracle", e);
    } finally {
        JDBCUtil.safeClose(connection);
    }
}

From source file:org.rimudb.Database.java

/**
 * Get this database's meta data/*w  w  w  .  ja  v  a 2 s  . co  m*/
 */
private RimuDBDatabaseMetaData lookupDatabaseInfo() throws RimuDBException {
    RimuDBDatabaseMetaData metaData = new RimuDBDatabaseMetaData();
    Connection con = null;
    DatabaseMetaData dbmd = null;
    ResultSet rs = null;

    try {
        con = getDatabaseConnection();
        dbmd = con.getMetaData();

        metaData.setDatabaseProductName(dbmd.getDatabaseProductName());
        metaData.setDatabaseMajorVersion(dbmd.getDatabaseMajorVersion());
        metaData.setSupportsGetGeneratedKeys(dbmd.supportsGetGeneratedKeys());

    } catch (SQLException e) {
        throw new RimuDBException(e);

    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
            }
        }
    }

    return metaData;
}

From source file:org.talend.metadata.managment.model.DBConnectionFillerImpl.java

protected String getSchemaName(ResultSet schemaRs, DatabaseMetaData dbJDBCMetadata, Catalog catalog) {
    String schemaName = null;//  ww  w.java2  s. c o m
    String catalogName = null;
    try {
        schemaName = schemaRs.getString(MetaDataConstants.TABLE_SCHEM.name());
        // MOD klliu bug 19004 2011-03-31
        if (!(MetadataConnectionUtils.isPostgresql(dbJDBCMetadata)
                || MetadataConnectionUtils.isSybase(dbJDBCMetadata))) {
            catalogName = schemaRs.getString(MetaDataConstants.TABLE_CATALOG.name());
        }

        // the case for mssql
        if (MetadataConnectionUtils.isMssql(dbJDBCMetadata) && dbJDBCMetadata.getDatabaseMajorVersion() > 8
                && dbJDBCMetadata.getDriverMajorVersion() > 1) {
            if (catalogName != null && catalogName != schemaName) {
                schemaName = catalogName;
            }
        }
        if (!MetadataConnectionUtils.isMssql(dbJDBCMetadata.getConnection()) && catalogName != null
                && !catalogName.equals(catalog.getName())) {
            return null;
        }
    } catch (Exception e) {
        // not some things need to do
    }
    return schemaName;
}

From source file:org.talend.repository.ui.wizards.metadata.connection.database.DatabaseForm.java

private String checkDBVersion() {
    String msg = null;/*w  ww. j  a  va2s. c  om*/
    EDatabaseVersion4Drivers version = EDatabaseVersion4Drivers
            .indexOfByVersionDisplay(dbVersionCombo.getText());
    ExtractMetaDataUtils extractMeta = ExtractMetaDataUtils.getInstance();
    DatabaseConnection connection = getConnection();
    List<EDatabaseVersion4Drivers> dbTypeList = EDatabaseVersion4Drivers
            .indexOfByDbType(connection.getDatabaseType());
    if (version != null && dbTypeList.size() > 1) {
        EDatabaseTypeName dbType = EDatabaseTypeName.getTypeFromDbType(getConnection().getDatabaseType());
        if (dbType == null || dbType.equals(EDatabaseTypeName.ACCESS) || dbType.equals(EDatabaseTypeName.PSQL)
                || dbType.equals(EDatabaseTypeName.PLUSPSQL) || dbType.equals(EDatabaseTypeName.IMPALA)) {
            // no version check for these dbs
            return null;
        }
        if (connection.getDriverClass() == null && dbType != EDatabaseTypeName.GENERAL_JDBC) {
            String driverClass = extractMeta.getDriverClassByDbType(connection.getDatabaseType());
            connection.setDriverClass(driverClass);
        }
        java.sql.Connection sqlConn = MetadataConnectionUtils.createConnection(connection).getObject();
        // if the dbtype is Access,it will throw a sqlException
        if (sqlConn != null) {
            try {
                DatabaseMetaData dm = extractMeta.getDatabaseMetaData(sqlConn, connection);
                int versionNum = dm.getDatabaseMajorVersion();
                String[] strArray = version.getVersionValue().split("_"); //$NON-NLS-1$
                if (strArray.length > 1 && strArray[1].startsWith(Integer.toString(versionNum))) {
                    msg = null;
                } else {
                    msg = "Version detected on server is \"" + strArray[0] + " " + versionNum + "\"."; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                }
            } catch (SQLException exp) {
                ExceptionHandler.process(exp);
            } finally {
                if (sqlConn != null) {
                    ConnectionUtils.closeConnection(sqlConn);
                }
            }
        }
    }
    return msg;
}

From source file:org.unitils.core.dbsupport.OracleDbSupport.java

/**
 * @return The major version number of the Oracle database server that is used (e.g. for Oracle version 9.2.0.1, 9 is returned
 *//*from ww w  . j a va  2 s. c o m*/
protected Integer getOracleMajorVersionNumber() {
    if (oracleMajorVersionNumber == null) {
        Connection connection = null;
        try {
            connection = getSQLHandler().getDataSource().getConnection();
            DatabaseMetaData metaData = connection.getMetaData();
            oracleMajorVersionNumber = metaData.getDatabaseMajorVersion();
        } catch (SQLException e) {
            throw new UnitilsException("Unable to determine database major version", e);
        } finally {
            closeQuietly(connection);
        }
    }
    return oracleMajorVersionNumber;
}

From source file:org.wso2.carbon.analytics.datasource.rdbms.RDBMSUtils.java

public static Map<String, Object> lookupDatabaseInfo(DataSource ds) throws AnalyticsException {
    Connection conn = null;/*ww  w  .ja  va 2 s  . c  o m*/
    try {
        conn = ds.getConnection();
        DatabaseMetaData dmd = conn.getMetaData();
        Map<String, Object> result = new HashMap<String, Object>();
        result.put(DATABASE_PRODUCT_NAME, dmd.getDatabaseProductName());
        result.put(VERSION,
                Double.parseDouble(dmd.getDatabaseMajorVersion() + "." + dmd.getDatabaseMinorVersion()));
        return result;
    } catch (SQLException e) {
        throw new AnalyticsException("Error in looking up database type: " + e.getMessage(), e);
    } finally {
        RDBMSUtils.cleanupConnection(null, null, conn);
    }
}

From source file:org.wso2.carbon.attachment.mgt.core.dao.impl.jpa.AbstractJPAVendorAdapter.java

/**
 * Determine the data base dialect type based on connection meta-data
 * @return/*  ww w.  jav a 2  s  .  c o m*/
 */
protected DatabaseType determineDbType() {
    Connection con = null;
    DatabaseType dbType = null;
    try {
        con = getDBConnection();
        DatabaseMetaData metaData = con.getMetaData();
        if (metaData != null) {
            String dbProductName = metaData.getDatabaseProductName().toLowerCase();
            int dbMajorVer = metaData.getDatabaseMajorVersion();
            if (log.isDebugEnabled())
                log.debug("Using database " + dbProductName + " major version " + dbMajorVer);
            if (dbProductName.contains("db2")) {
                dbType = DatabaseType.DB2;
            } else if (dbProductName.contains("oracle")) {
                dbType = DatabaseType.ORACLE;
            } else if (dbProductName.contains("derby")) {
                dbType = DatabaseType.DERBY;
            } else if (dbProductName.contains("h2")) {
                dbType = DatabaseType.H2;
            } else if (dbProductName.contains("hsql")) {
                dbType = DatabaseType.HSQL;
            } else if (dbProductName.contains("microsoft sql")) {
                dbType = DatabaseType.SQL_SERVER;
            } else if (dbProductName.contains("mysql")) {
                dbType = DatabaseType.MYSQL;
            } else if (dbProductName.contains("postgresql")) {
                dbType = DatabaseType.POSTGRESQL;
            } else if (dbProductName.contains("sybase")) {
                dbType = DatabaseType.SYBASE;
            }
        }
    } catch (SQLException e) {
        log.warn("Unable to determine database dialect.", e);
    } finally {
        close(con);
    }
    return dbType;
}

From source file:org.wso2.carbon.bpel.b4p.coordination.dao.jpa.AbstractJPAVendorAdapter.java

protected DatabaseType determineDbType() {
    Connection con = null;/* ww w .  j a va2 s  .  co m*/
    DatabaseType dbType = null;
    try {
        con = getDBConnection();
        DatabaseMetaData metaData = con.getMetaData();
        if (metaData != null) {
            String dbProductName = metaData.getDatabaseProductName().toLowerCase();
            int dbMajorVer = metaData.getDatabaseMajorVersion();
            if (log.isDebugEnabled()) {
                log.debug("Using database " + dbProductName + " major version " + dbMajorVer);
            }
            if (dbProductName.contains("db2")) {
                dbType = DatabaseType.DB2;
            } else if (dbProductName.contains("oracle")) {
                dbType = DatabaseType.ORACLE;
            } else if (dbProductName.contains("derby")) {
                dbType = DatabaseType.DERBY;
            } else if (dbProductName.contains("h2")) {
                dbType = DatabaseType.H2;
            } else if (dbProductName.contains("hsql")) {
                dbType = DatabaseType.HSQL;
            } else if (dbProductName.contains("microsoft sql")) {
                dbType = DatabaseType.SQL_SERVER;
            } else if (dbProductName.contains("mysql")) {
                dbType = DatabaseType.MYSQL;
            } else if (dbProductName.contains("postgresql")) {
                dbType = DatabaseType.POSTGRESQL;
            } else if (dbProductName.contains("sybase")) {
                dbType = DatabaseType.SYBASE;
            }
        }
    } catch (SQLException e) {
        log.warn("Unable to determine database dialect.", e);
    } finally {
        close(con);
    }
    return dbType;
}