Example usage for java.sql DatabaseMetaData getDriverVersion

List of usage examples for java.sql DatabaseMetaData getDriverVersion

Introduction

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

Prototype

String getDriverVersion() throws SQLException;

Source Link

Document

Retrieves the version number of this JDBC driver as a String.

Usage

From source file:org.dashbuilder.dataprovider.sql.SQLDataSetTestBase.java

private void printDatabaseInfo() throws Exception {
    if (!_dbInfoPrinted) {
        DatabaseMetaData meta = conn.getMetaData();
        System.out.println(//from   w  w  w. ja  va  2s  .c o  m
                "\n********************************************************************************************");
        System.out.println(String.format("Database: %s %s", meta.getDatabaseProductName(),
                meta.getDatabaseProductVersion()));
        System.out.println(String.format("Driver: %s %s", meta.getDriverName(), meta.getDriverVersion()));
        System.out.println(
                "*********************************************************************************************\n");
        _dbInfoPrinted = true;
    }
}

From source file:org.dspace.storage.rdbms.DatabaseUtils.java

/**
 * Print basic information about the current database to System.out.
 * This is utilized by both the 'test' and 'info' commandline options.
 * @param connection current database connection
 * @throws SQLException if database error occurs
 *//*from w  w  w  .j a  v a 2 s. co m*/
private static void printDBInfo(Connection connection) throws SQLException {
    // Get basic Database info from connection
    DatabaseMetaData meta = connection.getMetaData();
    String dbType = getDbType(connection);
    System.out.println("\nDatabase Type: " + dbType);
    System.out.println("Database URL: " + meta.getURL());
    System.out.println("Database Schema: " + getSchemaName(connection));
    System.out.println("Database Username: " + meta.getUserName());
    System.out.println("Database Software: " + meta.getDatabaseProductName() + " version "
            + meta.getDatabaseProductVersion());
    System.out.println("Database Driver: " + meta.getDriverName() + " version " + meta.getDriverVersion());

    // For Postgres, report whether pgcrypto is installed
    // (If it isn't, we'll also write out warnings...see below)
    if (dbType.equals(DBMS_POSTGRES)) {
        boolean pgcryptoUpToDate = PostgresUtils.isPgcryptoUpToDate();
        Double pgcryptoVersion = PostgresUtils.getPgcryptoInstalledVersion(connection);
        System.out.println("PostgreSQL '" + PostgresUtils.PGCRYPTO + "' extension installed/up-to-date? "
                + pgcryptoUpToDate + " "
                + ((pgcryptoVersion != null) ? "(version=" + pgcryptoVersion + ")" : "(not installed)"));
    }
}

From source file:org.hyperic.hq.bizapp.server.session.SysStats.java

static Properties getDBStats(Connection connection) {
    Properties props = new Properties();
    DatabaseMetaData md;

    try {//w w w .  j  av  a 2s .c om
        md = connection.getMetaData();
        props.setProperty("hq.db.product.name", md.getDatabaseProductName());
        props.setProperty("hq.db.product.ver", md.getDatabaseProductVersion());
        props.setProperty("hq.db.driver.name", md.getDriverName());
        props.setProperty("hq.db.driver.ver", md.getDriverVersion());
    } catch (SQLException e) {
        _log.warn("Error get db stats");
        return props;
    }

    return props;
}

From source file:org.kawanfw.sql.servlet.DatabaseMetaDataExecutor.java

/**
 * /*from  w  ww.j ava2  s .  com*/
 * Calls a remote metadata method from the PC <br>
 * 
 * @throws IOException
 *             all network, etc. errors
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws NoSuchMethodException
 * @throws SecurityException
 * @throws InvocationTargetException
 * @throws IllegalArgumentException
 */
private void callMetaDataFunction(HttpServletRequest request, OutputStream out, Connection connection)
        throws SQLException, IOException, ClassNotFoundException, InstantiationException,
        IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException

{

    // The method name
    String methodName = request.getParameter(Parameter.METHOD_NAME);
    // methodName = HtmlConverter.fromHtml(methodName);

    // The parms name
    String paramsTypes = request.getParameter(Parameter.PARAMS_TYPES);
    String paramsValues = request.getParameter(Parameter.PARAMS_VALUES);

    // Make sure all values are not null and trimed

    methodName = this.getTrimValue(methodName);
    paramsTypes = this.getTrimValue(paramsTypes);
    paramsValues = this.getTrimValue(paramsValues);

    debug("actionInvokeRemoteMethod:methodName       : " + methodName);

    // paramsTypes = HtmlConverter.fromHtml(paramsTypes);
    // paramsValues = HtmlConverter.fromHtml(paramsValues);

    List<String> listParamsTypes = ListOfStringTransport.fromJson(paramsTypes);
    List<String> listParamsValues = ListOfStringTransport.fromJson(paramsValues);

    debug("actionInvokeRemoteMethod:listParamsTypes      : " + listParamsTypes);
    debug("actionInvokeRemoteMethod:listParamsValues     : " + listParamsValues);

    DatabaseMetaData databaseMetaData = connection.getMetaData();

    // Trap DatabaseMetaData.getTables() & DatabaseMetaData.getUDTs()
    // that have special array String[] or int[] parameters
    if (methodName.equals("getTables") || methodName.equals("getUDTs") || methodName.equals("getPrimaryKeys")) {
        DatabaseMetaDataSpecial databaseMetaDataSpecial = new DatabaseMetaDataSpecial(databaseMetaData,
                methodName, listParamsValues);
        ResultSet rs = databaseMetaDataSpecial.execute();
        dumpResultSetOnServletOutStream(rs);
        return;
    }

    @SuppressWarnings("rawtypes")
    Class[] argTypes = new Class[listParamsTypes.size()];
    Object[] values = new Object[listParamsValues.size()];

    for (int i = 0; i < listParamsTypes.size(); i++) {
        String value = listParamsValues.get(i);

        String javaType = listParamsTypes.get(i);
        JavaValueBuilder javaValueBuilder = new JavaValueBuilder(javaType, value);

        argTypes[i] = javaValueBuilder.getClassOfValue();
        values[i] = javaValueBuilder.getValue();

        // Trap NULL values
        if (values[i].equals("NULL")) {
            values[i] = null;
        }

        debug("argTypes[i]: " + argTypes[i]);
        debug("values[i]  : " + values[i]);
    }

    Class<?> c = Class.forName("java.sql.DatabaseMetaData");
    Object theObject = databaseMetaData;

    // Invoke the method
    Method main = null;
    Object resultObj = null;

    // Get the Drvier Info
    String database = "";
    String productVersion = "";
    String DriverName = "";
    String DriverVersion = "";
    String driverInfo = Tag.PRODUCT;

    // try {
    // database = databaseMetaData.getDatabaseProductName();
    // productVersion = databaseMetaData.getDatabaseProductVersion();
    // DriverName = databaseMetaData.getDriverName();
    // DriverVersion= databaseMetaData.getDriverVersion();
    // driverInfo += database + " " + productVersion + " " + DriverName +
    // " " + DriverVersion;
    // } catch (Exception e1) {
    // ServerLogger.getLogger().log(Level.WARNING, Tag.PRODUCT +
    // "Impossible to get User Driver info.");
    // }

    database = databaseMetaData.getDatabaseProductName();
    productVersion = databaseMetaData.getDatabaseProductVersion();
    DriverName = databaseMetaData.getDriverName();
    DriverVersion = databaseMetaData.getDriverVersion();
    driverInfo += database + " " + productVersion + " " + DriverName + " " + DriverVersion;

    String methodParams = getMethodParams(values);

    try {
        main = c.getDeclaredMethod(methodName, argTypes);
    } catch (SecurityException e) {
        throw new SecurityException(driverInfo + " - Security - Impossible to get declared DatabaseMetaData."
                + methodName + "(" + methodParams + ")");
    } catch (NoSuchMethodException e) {
        throw new NoSuchMethodException(
                driverInfo + " - No Such Method - Impossible get declared DatabaseMetaData." + methodName + "("
                        + methodParams + ")");
    }

    try {
        resultObj = main.invoke(theObject, values);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
                driverInfo + " - Impossible to call DatabaseMetaData." + methodName + "(" + methodParams + ")");
    } catch (IllegalAccessException e) {
        throw new IllegalAccessException(driverInfo + " - Impossible to access DatabaseMetaData method."
                + methodName + "(" + methodParams + ")");
    } catch (InvocationTargetException e) {
        throw new InvocationTargetException(e, driverInfo + " - Impossible to invoke DatabaseMetaData method."
                + methodName + "(" + methodParams + ")");
    }

    if (resultObj instanceof ResultSet) {
        ResultSet rs = (ResultSet) resultObj;
        dumpResultSetOnServletOutStream(rs);

    } else {
        // All other formats are handled in String
        String result = null;
        if (resultObj != null)
            result = resultObj.toString();
        debug("actionInvokeRemoteMethod:result: " + result);
        result = HtmlConverter.toHtml(result);

        //out.println(TransferStatus.SEND_OK);
        //out.println(result);
        ServerSqlManager.writeLine(out, TransferStatus.SEND_OK);
        ServerSqlManager.writeLine(out, result);
    }

}

From source file:org.opoo.oqs.core.AbstractQueryFactory.java

public void afterPropertiesSet() {
    String databaseName = null;//from   w w  w.  jav  a 2s  .c  om
    int databaseMajorVersion = 0;
    Connection conn = null;
    try {
        conn = connectionManager.getConnection();
        DatabaseMetaData meta = conn.getMetaData();
        databaseName = meta.getDatabaseProductName();
        databaseMajorVersion = getDatabaseMajorVersion(meta);
        log.info("RDBMS: " + databaseName + ", version: " + meta.getDatabaseProductVersion());
        log.info("JDBC driver: " + meta.getDriverName() + ", version: " + meta.getDriverVersion());
    } catch (SQLException sqle) {
        log.warn("Could not obtain connection metadata", sqle);
    } catch (UnsupportedOperationException uoe) {
        // user supplied JDBC connections
    } finally {
        connectionManager.releaseConnection(conn);
    }
    if (dialect == null) {
        dialect = this.determineDialect(databaseName, databaseMajorVersion);
    }
}

From source file:org.rhq.enterprise.server.util.SystemDatabaseInformation.java

private SystemDatabaseInformation() {
    DataSource ds = null;// ww  w . ja v  a2  s  . co  m
    Connection conn = null;
    try {
        ds = LookupUtil.getDataSource();
        conn = ds.getConnection();
        DatabaseMetaData metadata = conn.getMetaData();

        String url = metadata.getURL();
        String productName = metadata.getDatabaseProductName();
        String productVersion = metadata.getDatabaseProductVersion();
        String driverName = metadata.getDriverName();
        String driverVersion = metadata.getDriverVersion();

        Map<Property, String> values = new HashMap<Property, String>();
        values.put(Property.DATABASE_CONNECTION_URL, url);
        values.put(Property.DATABASE_PRODUCT_NAME, productName);
        values.put(Property.DATABASE_PRODUCT_VERSION, productVersion);
        values.put(Property.DATABASE_DRIVER_NAME, driverName);
        values.put(Property.DATABASE_DRIVER_VERSION, driverVersion);

        values.put(Property.CURRENT_MEASUREMENT_TABLE, MeasurementDataManagerUtility.getCurrentRawTable());
        values.put(Property.NEXT_MEASUREMENT_TABLE_ROTATION,
                MeasurementDataManagerUtility.getNextRotationTime());

        properties = Collections.unmodifiableMap(values);

    } catch (Exception e) {
        log.error("Could not load properties for " + SystemDatabaseInformation.class.getSimpleName());
    } finally {
        if (properties == null) {
            Map<Property, String> values = new HashMap<Property, String>();
            for (Property prop : Property.values()) {
                values.put(prop, "unknown");
            }
            properties = Collections.unmodifiableMap(values);
        }
        JDBCUtil.safeClose(conn);
    }
}

From source file:org.sakaiproject.warehouse.util.db.DbLoader.java

protected void printInfo() throws SQLException {
    DatabaseMetaData dbMetaData = con.getMetaData();
    dbName = dbMetaData.getDatabaseProductName();
    dbVersion = dbMetaData.getDatabaseProductVersion();
    driverName = dbMetaData.getDriverName();
    driverVersion = dbMetaData.getDriverVersion();

    logger.debug("Starting DbLoader...");
    logger.debug("Database name: '" + dbName + "'");
    logger.debug("Database version: '" + dbVersion + "'");
    logger.debug("Driver name: '" + driverName + "'");
    logger.debug("Driver version: '" + driverVersion + "'");
    logger.debug("Database url: '" + dbMetaData.getURL() + "'");
}

From source file:org.sakaiproject.warehouse.util.db.DbLoader.java

protected String getLocalDataTypeName(String genericDataTypeName) {

    String localDataTypeName = null;

    try {/*  w  ww.j a v  a  2  s.c o  m*/
        DatabaseMetaData dbmd = con.getMetaData();
        String dbName = dbmd.getDatabaseProductName();
        String dbVersion = dbmd.getDatabaseProductVersion();
        String driverName = dbmd.getDriverName();
        String driverVersion = dbmd.getDriverVersion();

        // Check for a mapping in DbLoader.xml
        localDataTypeName = propertiesHandler.properties.getMappedDataTypeName(dbName, dbVersion, driverName,
                driverVersion, genericDataTypeName);

        if (localDataTypeName != null)
            return localDataTypeName;

        // Find the type code for this generic type name
        int dataTypeCode = getJavaSqlType(genericDataTypeName);

        // Find the first local type name matching the type code
        ResultSet rs = dbmd.getTypeInfo();
        try {
            while (rs.next()) {
                int localDataTypeCode = rs.getInt("DATA_TYPE");

                if (dataTypeCode == localDataTypeCode) {
                    try {
                        localDataTypeName = rs.getString("TYPE_NAME");
                    } catch (SQLException sqle) {
                    }
                    break;
                }
            }
        } finally {
            rs.close();
        }

        if (localDataTypeName != null)
            return localDataTypeName;

        // No matching type found, report an error
        logger.error("Error in DbLoader.getLocalDataTypeName()");
        logger.error("Your database driver, '" + driverName + "', version '" + driverVersion
                + "', was unable to find a local type name that matches the generic type name, '"
                + genericDataTypeName + "'.");
        logger.error("Please add a mapped type for database '" + dbName + "', version '" + dbVersion
                + "' inside your properties file and run this program again.");
        logger.error("Exiting...");
    } catch (Exception e) {
        logger.error("Error in DbLoader.getLocalDataTypeName()", e);
    }

    return null;
}

From source file:org.seasar.dbflute.helper.jdbc.connection.DfDataSourceHandler.java

protected void processConnectionMetaInfo(Connection conn) throws SQLException {
    if (_alreadySetupMeta) {
        return;/* w  ww.  j a va 2  s.c  o  m*/
    }
    try {
        final DfConnectionMetaInfo metaInfo = new DfConnectionMetaInfo();
        final DatabaseMetaData metaData = conn.getMetaData();
        metaInfo.setProductName(metaData.getDatabaseProductName());
        metaInfo.setProductVersion(metaData.getDatabaseProductVersion());
        metaInfo.setDriverName(metaData.getDriverName());
        metaInfo.setDriverVersion(metaData.getDriverVersion());
        final int majorVersion = metaData.getJDBCMajorVersion();
        final int minorVersion = metaData.getJDBCMinorVersion();
        metaInfo.setJdbcVersion(majorVersion + "." + minorVersion);
        _log.info("  product = " + metaInfo.getProductDisp());
        _log.info("  driver  = " + metaInfo.getDriverDisp());
        _connectionMetaInfo = metaInfo;
    } catch (SQLException continued) {
        _log.info("*Failed to get connection meta: " + continued.getMessage());
        _connectionMetaInfo = null;
    }
    _alreadySetupMeta = true;
}

From source file:org.sonar.server.platform.monitoring.DatabaseMonitor.java

private void completeDbAttributes(Map<String, Object> attributes) {
    try (DbSession dbSession = dbClient.openSession(false); Connection connection = dbSession.getConnection()) {
        DatabaseMetaData metadata = connection.getMetaData();
        attributes.put("Database", metadata.getDatabaseProductName());
        attributes.put("Database Version", metadata.getDatabaseProductVersion());
        attributes.put("Username", metadata.getUserName());
        attributes.put("URL", metadata.getURL());
        attributes.put("Driver", metadata.getDriverName());
        attributes.put("Driver Version", metadata.getDriverVersion());
        attributes.put("Version Status", getMigrationStatus());
    } catch (SQLException e) {
        throw new IllegalStateException("Fail to get DB metadata", e);
    }/*  w  ww  .  j av  a2s  .co m*/
}