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.apache.ode.scheduler.simple.jdbc.SchedulerDAOConnectionImpl.java

private Dialect guessDialect() {
    Dialect d = Dialect.UNKNOWN;/*from  ww w. jav a 2s  . c o  m*/
    Connection con = null;
    try {
        con = getConnection();
        DatabaseMetaData metaData = con.getMetaData();
        if (metaData != null) {
            String dbProductName = metaData.getDatabaseProductName();
            int dbMajorVer = metaData.getDatabaseMajorVersion();
            __log.debug("Using database " + dbProductName + " major version " + dbMajorVer);
            if (dbProductName.indexOf("DB2") >= 0) {
                d = Dialect.DB2;
            } else if (dbProductName.indexOf("Derby") >= 0) {
                d = Dialect.DERBY;
            } else if (dbProductName.indexOf("Firebird") >= 0) {
                d = Dialect.FIREBIRD;
            } else if (dbProductName.indexOf("HSQL") >= 0) {
                d = Dialect.HSQL;
            } else if (dbProductName.indexOf("Microsoft SQL") >= 0) {
                d = Dialect.SQLSERVER;
            } else if (dbProductName.indexOf("MySQL") >= 0) {
                d = Dialect.MYSQL;
            } else if (dbProductName.indexOf("Sybase") >= 0 || dbProductName.indexOf("ASE") >= 0
                    || dbProductName.indexOf("Adaptive") >= 0) {
                d = Dialect.SYBASE;
                if (dbMajorVer >= 12) {
                    d = Dialect.SYBASE12;
                }
            }
        }
    } catch (SQLException e) {
        __log.warn("Unable to determine database dialect", e);
    } finally {
        close(con);
    }
    __log.debug("Using database dialect: " + d);
    return d;
}

From source file:org.apache.ode.scheduler.simple.JdbcDelegate.java

private Dialect guessDialect() {
    Dialect d = Dialect.UNKNOWN;//from   w  ww .j a v a  2s. com
    Connection con = null;
    try {
        con = getConnection();
        DatabaseMetaData metaData = con.getMetaData();
        if (metaData != null) {
            String dbProductName = metaData.getDatabaseProductName();
            int dbMajorVer = metaData.getDatabaseMajorVersion();
            __log.info("Using database " + dbProductName + " major version " + dbMajorVer);
            if (dbProductName.indexOf("DB2") >= 0) {
                d = Dialect.DB2;
            } else if (dbProductName.indexOf("Derby") >= 0) {
                d = Dialect.DERBY;
            } else if (dbProductName.indexOf("Firebird") >= 0) {
                d = Dialect.FIREBIRD;
            } else if (dbProductName.indexOf("HSQL") >= 0) {
                d = Dialect.HSQL;
            } else if (dbProductName.indexOf("H2") >= 0) {
                d = Dialect.H2;
            } else if (dbProductName.indexOf("Microsoft SQL") >= 0) {
                d = Dialect.SQLSERVER;
            } else if (dbProductName.indexOf("MySQL") >= 0) {
                d = Dialect.MYSQL;
            } else if (dbProductName.indexOf("Sybase") >= 0 || dbProductName.indexOf("Adaptive") >= 0) {
                d = Dialect.SYBASE;
                if (dbMajorVer == 12) {
                    d = Dialect.SYBASE12;
                }
            }
        }
    } catch (SQLException e) {
        __log.warn("Unable to determine database dialect", e);
    } finally {
        close(con);
    }
    __log.info("Using database dialect: " + d);
    return d;
}

From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java

/**
 * This method is called when the dictionary first sees any connection.
 * It is used to initialize dictionary metadata if needed. If you
 * override this method, be sure to call
 * <code>super.connectedConfiguration</code>.
 *//*from   w  w w .  java  2  s.com*/
public void connectedConfiguration(Connection conn) throws SQLException {
    if (!connected) {
        DatabaseMetaData metaData = null;
        try {
            metaData = conn.getMetaData();

            databaseProductName = nullSafe(metaData.getDatabaseProductName());
            databaseProductVersion = nullSafe(metaData.getDatabaseProductVersion());
            setMajorVersion(metaData.getDatabaseMajorVersion());
            setMinorVersion(metaData.getDatabaseMinorVersion());
            try {
                // JDBC3-only method, so it might throw an
                // AbstractMethodError
                int JDBCMajorVersion = metaData.getJDBCMajorVersion();
                isJDBC3 = JDBCMajorVersion >= 3;
                isJDBC4 = JDBCMajorVersion >= 4;
            } catch (Throwable t) {
                // ignore if not JDBC3
            }
        } catch (Exception e) {
            if (log.isTraceEnabled())
                log.trace(e.toString(), e);
        }

        if (log.isTraceEnabled()) {
            log.trace(DBDictionaryFactory.toString(metaData));

            if (isJDBC3) {
                try {
                    log.trace(_loc.get("connection-defaults", new Object[] { conn.getAutoCommit(),
                            conn.getHoldability(), conn.getTransactionIsolation() }));
                } catch (Throwable t) {
                    log.trace("Unable to trace connection settings", t);
                }
            }
        }

        // Configure the naming utility
        if (supportsDelimitedIdentifiers == null) // not explicitly set
            configureNamingUtil(metaData);

        // Auto-detect generated keys retrieval support unless user specified it.
        if (supportsGetGeneratedKeys == null) {
            supportsGetGeneratedKeys = (isJDBC3) ? metaData.supportsGetGeneratedKeys() : false;
        }
        if (log.isInfoEnabled()) {
            log.info(_loc.get("dict-info", new Object[] { metaData.getDatabaseProductName(), getMajorVersion(),
                    getMinorVersion(), metaData.getDriverName(), metaData.getDriverVersion() }));
        }
    }
    connected = true;
}

From source file:org.apache.openjpa.jdbc.sql.HSQLDictionary.java

/**
 * Determine HSQLDB version either by using JDBC 3 method or, if it
 * is not available, by parsing the value returned by
 * {@linkplain DatabaseMetaData#getDatabaseProductVersion()}.
 *//*from  ww w . j  a  va  2s . com*/
protected void determineHSQLDBVersion(Connection con) throws SQLException {
    DatabaseMetaData metaData = con.getMetaData();

    if (isJDBC3) {
        dbMajorVersion = metaData.getDatabaseMajorVersion();
    } else {
        // String is like "2.0.0"
        String productVersion = metaData.getDatabaseProductVersion();
        String[] version = productVersion.split("\\.");
        dbMajorVersion = Integer.parseInt(version[0]);
    }
}

From source file:org.apache.openjpa.jdbc.sql.MySQLDictionary.java

@Override
public void connectedConfiguration(Connection conn) throws SQLException {
    super.connectedConfiguration(conn);

    DatabaseMetaData metaData = conn.getMetaData();
    int maj = 0;/*from   ww  w .j  av a 2s  .c o m*/
    int min = 0;
    if (isJDBC3) {
        maj = metaData.getDatabaseMajorVersion();
        min = metaData.getDatabaseMinorVersion();
    } else {
        try {
            // The product version looks like 4.1.3-nt or 5.1.30
            String productVersion = metaData.getDatabaseProductVersion();
            int[] versions = getMajorMinorVersions(productVersion);
            maj = versions[0];
            min = versions[1];
        } catch (IllegalArgumentException e) {
            // we don't understand the version format.
            // That is ok. We just take the default values.
            if (log.isWarnEnabled())
                log.warn(e.toString(), e);
        }
    }
    if (maj < 4 || (maj == 4 && min < 1)) {
        supportsSubselect = false;
        allowsAliasInBulkClause = false;
        supportsForeignKeysComposite = false;
    }
    if (maj > 5 || (maj == 5 && min >= 1))
        supportsXMLColumn = true;

    if (metaData.getDriverMajorVersion() < 5)
        driverDeserializesBlobs = true;
}

From source file:org.apache.openjpa.jdbc.sql.PostgresDictionary.java

/**
 * Determine XML column support and backslash handling.
 *//*from   www  .  ja  v a2 s  .  c o  m*/
public void connectedConfiguration(Connection conn) throws SQLException {
    super.connectedConfiguration(conn);

    DatabaseMetaData metaData = conn.getMetaData();
    int maj = 0;
    int min = 0;
    if (isJDBC3) {
        maj = metaData.getDatabaseMajorVersion();
        min = metaData.getDatabaseMinorVersion();
    } else {
        try {
            // The product version looks like "8.3.5".
            String productVersion = metaData.getDatabaseProductVersion();
            String majMin[] = productVersion.split("\\.");
            maj = Integer.parseInt(majMin[0]);
            min = Integer.parseInt(majMin[1]);
        } catch (Exception e) {
            // We don't understand the version format.
            if (log.isWarnEnabled())
                log.warn(e.toString(), e);
        }
    }

    if ((maj >= 9 || (maj == 8 && min >= 3))) {
        supportsXMLColumn = true;
    }
    // Old PostgreSQL requires double-escape for strings.
    if ((maj <= 8 || (maj == 9 && min == 0))) {
        requiresSearchStringEscapeForLike = true;
        searchStringEscape = "\\\\";
    }
}

From source file:org.dashbuilder.dataprovider.backend.sql.JDBCUtils.java

public static Dialect dialect(Connection connection) {
    try {/*from   w  w w.  ja  va  2 s  .c o  m*/
        DatabaseMetaData m = connection.getMetaData();
        String url = m.getURL();
        if (!StringUtils.isBlank(url)) {
            return dialect(url, m.getDatabaseMajorVersion());
        }
        String dbName = m.getDatabaseProductName();
        return dialect(dbName.toLowerCase());
    } catch (SQLException e) {
        e.printStackTrace();
        return DEFAULT;
    }
}

From source file:org.dbmaintain.database.impl.HsqldbDatabase.java

/**
 * @return The major version number of the Hsql database server that is used (e.g. for Hsql version 1.8.0, 1 is returned
 */// w  ww .  ja v a  2s  . c o m
protected Integer getHsqldbMajorVersionNumber() {
    if (hsqlMajorVersionNumber == null) {
        Connection connection = null;
        try {
            connection = getDataSource().getConnection();
            DatabaseMetaData metaData = connection.getMetaData();
            hsqlMajorVersionNumber = metaData.getDatabaseMajorVersion();

        } catch (SQLException e) {
            throw new DatabaseException("Unable to determine database major version.", e);
        } finally {
            closeQuietly(connection);
        }
    }
    return hsqlMajorVersionNumber;
}

From source file:org.dbmaintain.database.impl.OracleDatabase.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 w  w  w  .j a  va 2 s.co  m*/
protected Integer getOracleMajorVersionNumber() {
    if (oracleMajorVersionNumber == null) {
        Connection connection = null;
        try {
            connection = getDataSource().getConnection();
            DatabaseMetaData metaData = connection.getMetaData();
            oracleMajorVersionNumber = metaData.getDatabaseMajorVersion();
        } catch (SQLException e) {
            throw new DatabaseException("Unable to determine database major version", e);
        } finally {
            closeQuietly(connection);
        }
    }
    return oracleMajorVersionNumber;
}

From source file:org.diffkit.util.DKSqlUtil.java

public static Map<String, ?> getDatabaseInfo(Connection connection_) throws SQLException {
    if (connection_ == null)
        return null;
    DatabaseMetaData dbMeta = connection_.getMetaData();
    if (dbMeta == null)
        return null;
    Map<String, Object> info = new HashMap<String, Object>();
    info.put(DATABASE_MAJOR_VERSION_KEY, new Integer(dbMeta.getDatabaseMajorVersion()));
    info.put(DATABASE_MINOR_VERSION_KEY, new Integer(dbMeta.getDatabaseMinorVersion()));
    info.put(DATABASE_PRODUCT_NAME_KEY, dbMeta.getDatabaseProductName());
    info.put(DATABASE_PRODUCT_VERSION_KEY, dbMeta.getDatabaseProductVersion());
    return info;/*from   ww w.  ja  v  a 2 s  .  c  om*/
}