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:Main.java

public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);/*from  w  ww .j  a  va2  s.co m*/
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    DatabaseMetaData metadata = connection.getMetaData();

    int majorVersion = metadata.getDatabaseMajorVersion();
    System.out.println("majorVersion = " + majorVersion);

    int minorVersion = metadata.getDatabaseMinorVersion();
    System.out.println("minorVersion = " + minorVersion);

    String productName = metadata.getDatabaseProductName();
    System.out.println("productName = " + productName);

    String productVersion = metadata.getDatabaseProductVersion();
    System.out.println("productVersion = " + productVersion);
    connection.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();

    DatabaseMetaData meta = conn.getMetaData();
    try {// w w w.  j  av a  2s.  co  m
        int majorVersion = meta.getDatabaseMajorVersion();
        System.out.println("major Version:" + majorVersion);
    } catch (Exception e) {
        System.out.println("major Version:unsupported feature");
    }

    try {
        int minorVersion = meta.getDatabaseMinorVersion();
        System.out.println("minorVersion:" + minorVersion);
    } catch (Exception e) {
        System.out.println("minorVersion unsupported feature");
    }

    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    DatabaseMetaData meta = conn.getMetaData();
    try {//from  w w  w.  j  a  v  a  2  s . c o m
        int majorVersion = meta.getDatabaseMajorVersion();
        System.out.println("major Version: " + majorVersion);
        int minorVersion = meta.getDatabaseMinorVersion();
        System.out.println("minorVersion" + minorVersion);
    } catch (Exception e) {
        System.out.println("minorVersion unsupported feature");
    }

    String productName = meta.getDatabaseProductName();
    String productVersion = meta.getDatabaseProductVersion();
    System.out.println("productName" + productName);
    System.out.println("productVersion" + productVersion);
    conn.close();
}

From source file:TestDatabaseMetaDataToolDatabaseInformation.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    DatabaseMetaData meta = conn.getMetaData();
    // Oracle (and some other vendors) do not support
    // some the following methods; therefore, we need
    // to use try-catch block.
    try {/*  w  ww  .  ja  v a2  s .co  m*/
        int majorVersion = meta.getDatabaseMajorVersion();
        System.out.println("major Version: " + majorVersion);
    } catch (Exception e) {
        System.out.println("major Version: unsupported feature");
    }

    try {
        int minorVersion = meta.getDatabaseMinorVersion();
        System.out.println("minorVersion" + minorVersion);
    } catch (Exception e) {
        System.out.println("minorVersion unsupported feature");
    }

    String productName = meta.getDatabaseProductName();
    String productVersion = meta.getDatabaseProductVersion();
    System.out.println("productName" + productName);
    System.out.println("productVersion" + productVersion);
    conn.close();
}

From source file:com.aionemu.commons.database.DatabaseFactory.java

public synchronized static void init(String configFilePath) {
    if (dataSource != null) {
        return;/* w w  w .ja v  a2 s  .  c o  m*/
    }

    if (!configFilePath.equals("")) {
        try {
            Properties dbProps = PropertiesUtils.load(configFilePath);
            ConfigurableProcessor.process(DatabaseConfig.class, dbProps);
        } catch (IOException ex) {
            log.fatal("Cannot load database config", ex);
        }
    }

    try {
        DatabaseConfig.DATABASE_DRIVER.newInstance();
    } catch (Exception e) {
        log.fatal("Error obtaining DB driver", e);
        throw new Error("DB Driver doesnt exist!");
    }

    connectionPool = new GenericObjectPool();

    if (DatabaseConfig.DATABASE_CONNECTIONS_MIN > DatabaseConfig.DATABASE_CONNECTIONS_MAX) {
        log.error("Please check your database configuration. Minimum amount of connections is > maximum");
        DatabaseConfig.DATABASE_CONNECTIONS_MAX = DatabaseConfig.DATABASE_CONNECTIONS_MIN;
    }

    connectionPool.setMaxIdle(DatabaseConfig.DATABASE_CONNECTIONS_MIN);
    connectionPool.setMaxActive(DatabaseConfig.DATABASE_CONNECTIONS_MAX);
    /* test if connection is still valid before returning */
    //connectionPool.setTestOnBorrow(true);

    try {
        dataSource = setupDataSource();
        Connection c = getConnection();
        DatabaseMetaData dmd = c.getMetaData();
        databaseName = dmd.getDatabaseProductName();
        databaseMajorVersion = dmd.getDatabaseMajorVersion();
        databaseMinorVersion = dmd.getDatabaseMinorVersion();
        c.close();
    } catch (Exception e) {
        log.fatal("Error with connection string: " + DatabaseConfig.DATABASE_URL, e);
        throw new Error("DatabaseFactory not initialized!");
    }

    log.info("Successfully connected to database");
}

From source file:ca.nrc.cadc.db.DBUtil.java

private static void testDS(DataSource ds, boolean keepOpen) throws SQLException {
    Connection con = null;/* ww w .j  a  v  a2  s . co m*/
    try {
        con = ds.getConnection();
        DatabaseMetaData meta = con.getMetaData();
        if (!log.getEffectiveLevel().equals(Level.DEBUG))
            return;
        log.debug("connected to server: " + meta.getDatabaseProductName() + " " + meta.getDatabaseMajorVersion()
                + "." + meta.getDatabaseMinorVersion() + " driver: " + meta.getDriverName() + " "
                + meta.getDriverMajorVersion() + "." + meta.getDriverMinorVersion());
    } finally {
        if (!keepOpen && con != null)
            try {
                con.close();
            } catch (Exception ignore) {
            }
    }
}

From source file:com.trackplus.ddl.DataReader.java

private static void logDatabaseMetaDataInfo(DatabaseInfo databaseInfo, Connection connection)
        throws DDLException {
    DatabaseMetaData databaseMetaData = null;
    try {/*from   w w w.j  av a  2  s.  co  m*/
        databaseMetaData = connection.getMetaData();
        int majorVersion = databaseMetaData.getDatabaseMajorVersion();
        int minorVersion = databaseMetaData.getDatabaseMinorVersion();
        String productName = databaseMetaData.getDatabaseProductName();
        String productVersion = databaseMetaData.getDatabaseProductVersion();
        int driverMajorVersion = databaseMetaData.getDriverMajorVersion();
        int driverMinorVersion = databaseMetaData.getDriverMinorVersion();

        LOGGER.debug("DB DRIVER=" + databaseInfo.getDriver());
        LOGGER.debug("DB URL=" + databaseInfo.getUrl());
        LOGGER.debug("DB USER=" + databaseInfo.getUser());
        String password = databaseInfo.getPassword() == null ? null
                : databaseInfo.getPassword().replaceAll(".", "*");
        LOGGER.debug("DB PASSWORD=" + password + "\n");

        LOGGER.debug("DB majorVersion=" + majorVersion);
        LOGGER.debug("DB minorVersion=" + minorVersion);
        LOGGER.debug("DB productName=" + productName);
        LOGGER.debug("DB productVersion=" + productVersion);
        LOGGER.debug("DB driverMajorVersion=" + driverMajorVersion);
        LOGGER.debug("DB driverMinorVersion=" + driverMinorVersion);
    } catch (SQLException e) {
        throw new DDLException(e.getMessage(), e);
    }
}

From source file:cz.jirutka.spring.data.jdbc.sql.SQL2008SqlGenerator.java

@Override
public boolean isCompatible(DatabaseMetaData metadata) throws SQLException {
    String productName = metadata.getDatabaseProductName();
    int majorVersion = metadata.getDatabaseMajorVersion();

    return "Apache Derby".equals(productName) || "Oracle".equals(productName) && majorVersion >= 12
            || "Microsoft SQL Server".equals(productName) && majorVersion >= 11; // >= 2012
}

From source file:com.alibaba.otter.canal.example.db.dialect.AbstractDbDialect.java

public AbstractDbDialect(final JdbcTemplate jdbcTemplate, LobHandler lobHandler) {
    this.jdbcTemplate = jdbcTemplate;
    this.lobHandler = lobHandler;
    // ?transction
    this.transactionTemplate = new TransactionTemplate();
    transactionTemplate.setTransactionManager(new DataSourceTransactionManager(jdbcTemplate.getDataSource()));
    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);

    // ??//  w ww  . ja  v a2s . co m
    jdbcTemplate.execute(new ConnectionCallback() {

        public Object doInConnection(Connection c) throws SQLException, DataAccessException {
            DatabaseMetaData meta = c.getMetaData();
            databaseName = meta.getDatabaseProductName();
            databaseMajorVersion = meta.getDatabaseMajorVersion();
            databaseMinorVersion = meta.getDatabaseMinorVersion();

            return null;
        }
    });

    initTables(jdbcTemplate);
}

From source file:com.bstek.bdf2.core.orm.jdbc.dialect.AbstractDialect.java

/**
 * ???/*from  www . jav a  2  s  . c  o  m*/
 * @param connection ?
 * @param dbProductName ???
 * @param dbMajorVersion ??
 * @return ?????
 */
public boolean support(Connection connection, String dbProductName, String dbMajorVersion) {
    try {
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        String databaseProductName = databaseMetaData.getDatabaseProductName();
        int databaseMajorVersion = databaseMetaData.getDatabaseMajorVersion();
        boolean containsMysql = StringUtils.containsIgnoreCase(databaseProductName, dbProductName);
        if (StringUtils.isNotEmpty(dbMajorVersion)) {
            return containsMysql && Integer.valueOf(dbMajorVersion) == databaseMajorVersion;
        }
        return containsMysql;
    } catch (SQLException e) {
        return false;
    }
}