Example usage for java.sql DatabaseMetaData getDriverName

List of usage examples for java.sql DatabaseMetaData getDriverName

Introduction

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

Prototype

String getDriverName() throws SQLException;

Source Link

Document

Retrieves the name of this JDBC driver.

Usage

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   ww  w  . j a va  2 s  .com*/
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.flywaydb.test.dbunit.DefaultDatabaseConnectionFactory.java

public IDatabaseConnection createConnection(final Connection con, final DatabaseMetaData databaseMetaData)
        throws SQLException, DatabaseUnitException {
    IDatabaseConnection connection = null;

    // FIXME not nice I found not a fast possibility to generate inside H2
    // the tables inside a
    // schema as oracle do.
    final String driverName = databaseMetaData.getDriverName();

    if (driverName.toLowerCase().contains("oracle")) {
        // oracle schema name is the user name
        connection = new DatabaseConnection(con, databaseMetaData.getUserName().toUpperCase());
    } else {// www.j a  v  a2s . c  o  m
        if (driverName.contains("H2")) {
            // H2
            connection = new DatabaseConnection(con);
        } else {
            // all other
            connection = new DatabaseConnection(con);
        }
    }

    // final DatabaseConfig config = connection.getConfig();
    // // oracle 10g
    // // FIXME at the moment we have a hard coded oracle notation
    // config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new
    // Oracle10DataTypeFactory());

    return connection;
}

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

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

    try {/*ww w  . j a  va 2 s  .c  o m*/
        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

/**
 * /*w w  w .j ava2  s  .co m*/
 * 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.kuali.rice.test.ClearDatabaseLifecycle.java

private boolean isUsingDerby(DatabaseMetaData metaData) throws SQLException {
    return metaData.getDriverName().toLowerCase().contains("derby");
}

From source file:org.kuali.rice.test.ClearDatabaseLifecycle.java

private boolean isUsingOracle(DatabaseMetaData metaData) throws SQLException {
    return metaData.getDriverName().toLowerCase().contains("oracle");
}

From source file:org.kuali.rice.test.ClearDatabaseLifecycle.java

private boolean isUsingMySQL(DatabaseMetaData metaData) throws SQLException {
    return metaData.getDriverName().toLowerCase().contains("mysql");
}

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

public void afterPropertiesSet() {
    String databaseName = null;/*from   www  .j  a  va  2  s . c o  m*/
    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;/* w w w  . j a  v a  2s  . com*/
    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() + "'");
}