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:com.jagornet.dhcp.db.DbSchemaManager.java

/**
 * Validate schema./*  w  ww .  ja va2  s .  c om*/
 * 
 * @param dataSource the data source
 * 
 * @throws SQLException if there is a problem with the database
 * @throws IOExcpetion if there is a problem reading the schema file
 * 
 * returns true if database was created, false otherwise
 */
public static boolean validateSchema(DataSource dataSource, String schemaFilename, int schemaVersion)
        throws SQLException, IOException {
    boolean schemaCreated = false;

    List<String> tableNames = new ArrayList<String>();

    Connection conn = dataSource.getConnection();
    DatabaseMetaData dbMetaData = conn.getMetaData();

    log.info("JDBC Connection Info:\n" + "url = " + dbMetaData.getURL() + "\n" + "database = "
            + dbMetaData.getDatabaseProductName() + " " + dbMetaData.getDatabaseProductVersion() + "\n"
            + "driver = " + dbMetaData.getDriverName() + " " + dbMetaData.getDriverVersion());

    String[] types = { "TABLE" };
    ResultSet rs = dbMetaData.getTables(null, null, "%", types);
    if (rs.next()) {
        tableNames.add(rs.getString("TABLE_NAME"));
    } else {
        createSchema(dataSource, schemaFilename);
        dbMetaData = conn.getMetaData();
        rs = dbMetaData.getTables(null, null, "%", types);
        schemaCreated = true;
    }
    while (rs.next()) {
        tableNames.add(rs.getString("TABLE_NAME"));
    }

    String[] schemaTableNames;
    if (schemaVersion <= 1) {
        schemaTableNames = TABLE_NAMES;
    } else {
        schemaTableNames = TABLE_NAMES_V2;
    }

    if (tableNames.size() == schemaTableNames.length) {
        for (int i = 0; i < schemaTableNames.length; i++) {
            if (!tableNames.contains(schemaTableNames[i])) {
                throw new IllegalStateException("Invalid database schema: unknown tables");
            }
        }
    } else {
        throw new IllegalStateException("Invalid database schema: wrong number of tables");
    }

    return schemaCreated;
}

From source file:TerminalMonitor.java

static public void showVersion(DatabaseMetaData meta) {
    try {/*  www.j  a  v  a  2  s .c om*/
        System.out.println("TerminalMonitor v2.0");
        System.out.println("DBMS: " + meta.getDatabaseProductName() + " " + meta.getDatabaseProductVersion());
        System.out.println("JDBC Driver: " + meta.getDriverName() + " " + meta.getDriverVersion());
    } catch (SQLException e) {
        System.out.println("Failed to get version info: " + e.getMessage());
    }
}

From source file:net.bull.javamelody.internal.model.JavaInformations.java

private static void appendDataBaseVersion(StringBuilder result, Connection connection) throws SQLException {
    final DatabaseMetaData metaData = connection.getMetaData();
    // Scurit: pour l'instant on n'indique pas metaData.getUserName()
    result.append(metaData.getURL()).append('\n');
    result.append(metaData.getDatabaseProductName()).append(", ").append(metaData.getDatabaseProductVersion())
            .append('\n');
    result.append("Driver JDBC:\n").append(metaData.getDriverName()).append(", ")
            .append(metaData.getDriverVersion());
}

From source file:web.mvc.servlets.MyServlet.java

private void fillDataFromDataSource(List<InfoItem> data, DriverManagerDataSource dataSource)
        throws SQLException {
    try (Connection connect = dataSource.getConnection()) {
        DatabaseMetaData metaData = connect.getMetaData();
        data.add(new InfoItem("database name", metaData.getDatabaseProductName()));
        data.add(new InfoItem("database version", metaData.getDatabaseProductVersion()));
        data.add(new InfoItem("driver name", metaData.getDriverName()));
        data.add(new InfoItem("driver version", metaData.getDriverVersion()));
    }//ww  w .  j a v a2 s. c o  m
}

From source file:org.tec.webapp.jdbc.service.impl.SystemSvcImpl.java

/**
 * {@inheritDoc}/*from ww  w  .  ja  v a2s  .  com*/
 */
@Override()
public SerializableList<StatusBean> getStatus() {
    Connection conn = null;
    try {
        SerializableList<StatusBean> slist = new SerializableList<StatusBean>();

        // get java information
        slist.add(new StatusBean("java.version", System.getProperty("java.version")));

        //get Servlet information
        slist.add(new StatusBean("server.info", mServletContext.getServerInfo()));

        StringBuilder buff = new StringBuilder();

        buff.append(mServletContext.getMajorVersion());
        buff.append('.');
        buff.append(mServletContext.getMinorVersion());

        slist.add(new StatusBean("servlet.version", buff.toString()));

        // get database information
        conn = mDataSource.getConnection();
        DatabaseMetaData dmd = conn.getMetaData();
        slist.add(new StatusBean("database.server", dmd.getDatabaseProductName()));
        slist.add(new StatusBean("database.version", dmd.getDatabaseProductVersion()));
        slist.add(new StatusBean("jdbc.driver", dmd.getDriverName()));
        slist.add(new StatusBean("jdbc.driver.version", dmd.getDriverVersion()));

        // spring
        slist.add(new StatusBean("spring.version", SpringVersion.getVersion()));

        return slist;
    } catch (Throwable e) {
        throw new RuntimeException("failed to get system status", e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Throwable e) {
                mLogger.error("failed to get system status", e);
            }
        }
    }
}

From source file:org.tec.webapp.orm.service.impl.SystemSvcImpl.java

/**
 * {@inheritDoc}/*  ww  w.jav a  2  s  . com*/
 */
@Override()
public SerializableList<StatusBean> getStatus() {
    Connection conn = null;
    try {
        SerializableList<StatusBean> slist = new SerializableList<StatusBean>();

        // get java information
        slist.add(new StatusBean("java.version", System.getProperty("java.version")));

        //get Servlet information
        slist.add(new StatusBean("server.info", mServletContext.getServerInfo()));

        StringBuilder buff = new StringBuilder();

        buff.append(mServletContext.getMajorVersion());
        buff.append('.');
        buff.append(mServletContext.getMinorVersion());

        slist.add(new StatusBean("servlet.version", buff.toString()));

        // get database information
        conn = mDataSource.getConnection();
        DatabaseMetaData dmd = conn.getMetaData();
        slist.add(new StatusBean("database.server", dmd.getDatabaseProductName()));
        slist.add(new StatusBean("database.version", dmd.getDatabaseProductVersion()));
        slist.add(new StatusBean("jdbc.driver", dmd.getDriverName()));
        slist.add(new StatusBean("jdbc.driver.version", dmd.getDriverVersion()));

        // spring
        slist.add(new StatusBean("spring.version", SpringVersion.getVersion()));

        // hibernate
        slist.add(new StatusBean("hibernate.version", Version.getVersionString()));
        slist.add(new StatusBean("hibernate.session.factory", mSessionFactory.getClass().getName()));

        return slist;
    } catch (Throwable e) {
        throw new RuntimeException("failed to get system status", e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Throwable e) {
                mLogger.error("failed to get system status", e);
            }
        }
    }
}

From source file:org.focusns.service.env.impl.EnvironmentServiceImpl.java

protected Environment lookupDB() {
    try {/*from w  w w  .  j  a va 2s.  c o  m*/
        DatabaseMetaData metaData = dataSource.getConnection().getMetaData();
        //
        EnvironmentDB envDB = new EnvironmentDB();
        envDB.setDatabaseName(metaData.getDatabaseProductName());
        envDB.setDatabaseVersion(metaData.getDatabaseProductVersion());
        envDB.setDriverName(metaData.getDriverName());
        envDB.setDriverVersion(metaData.getDriverVersion());
        envDB.setUrl(metaData.getURL());
        envDB.setUsername(metaData.getUserName());
        envDB.setMaxConnections(metaData.getMaxConnections());
        //
        metaData.getConnection().close();
        //
        return envDB;
    } catch (SQLException e) {
        throw new UnsupportedOperationException(e);
    }
}

From source file:com.netspective.axiom.connection.BasicConnectionProviderEntry.java

public void init(String dataSourceId, Connection conn) {
    this.dataSourceId = dataSourceId;

    try {//from ww w. j  a va 2 s  .  c om
        try {
            DatabasePolicy policy = DatabasePolicies.getInstance().getDatabasePolicy(conn);
            put(KEYNAME_DATABASE_POLICY_CLASSNAME, policy.getClass().getName());
            put(KEYNAME_DATABASE_POLICY_DBMSID, policy.getDbmsIdentifier());
        } catch (Exception dpe) {
            put(KEYNAME_DATABASE_POLICY_CLASSNAME, dpe.toString());
        }

        DatabaseMetaData dbmd = conn.getMetaData();

        put(KEYNAME_DRIVER_NAME, dbmd.getDriverName());
        put(KEYNAME_DATABASE_PRODUCT_NAME, dbmd.getDatabaseProductName());
        put(KEYNAME_DATABASE_PRODUCT_VERSION, dbmd.getDatabaseProductVersion());
        put(KEYNAME_DRIVER_VERSION, dbmd.getDriverVersion());
        put(KEYNAME_URL, dbmd.getURL());
        put(KEYNAME_USER_NAME, dbmd.getUserName());

        String resultSetType = "unknown";
        if (dbmd.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE))
            resultSetType = "scrollable (insensitive)";
        else if (dbmd.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE))
            resultSetType = "scrollable (sensitive)";
        else if (dbmd.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY))
            resultSetType = "non-scrollabe (forward only)";
        put(KEYNAME_RESULTSET_TYPE, resultSetType);

        valid = true;
    } catch (Exception e) {
        exception = e;
    } finally {
        try {
            conn.close();
        } catch (SQLException e) {
            log.error("SQL Exception while closing connection", e);
        }
    }
}

From source file:net.certifi.audittablegen.AuditTableGen.java

/**
 * Examines the DataSource metadata for information pertaining to the
 * driver, catalog, schema and the presence of audit table configuration
 * data./*from   w  ww. ja  v a 2  s  .c o  m*/
 * 
 * @return String containing datasource information.
 * @throws SQLException 
 */
String getDataSourceInfo() throws SQLException {

    Connection conn = dataSource.getConnection();
    DatabaseMetaData dmd = conn.getMetaData();
    StringBuilder s = new StringBuilder();

    s.append("Driver Name: ").append(dmd.getDriverName()).append("Driver Version: ")
            .append(dmd.getDriverVersion()).append(System.lineSeparator()).append("CatalogSeperator: ")
            .append(dmd.getCatalogSeparator()).append(System.lineSeparator()).append("CatalogTerm: ")
            .append(dmd.getCatalogTerm()).append(System.lineSeparator()).append("SchemaTerm: ")
            .append(dmd.getSchemaTerm()).append(System.lineSeparator()).append("Catalogs: ");

    ResultSet rs = dmd.getCatalogs();
    while (rs.next()) {
        s.append(rs.getString("TABLE_CAT")).append(",");
        logger.debug("Catalog: {}", rs.getString("TABLE_CAT"));
    }
    rs.close();
    s.append(System.lineSeparator());

    s.append("Schemas: ");
    rs = dmd.getSchemas();
    while (rs.next()) {
        logger.debug("Schema: {}", rs.getString("TABLE_SCHEM"));
        s.append("{catalog}:").append(rs.getString("TABLE_CATALOG")).append(" {schema}:")
                .append(rs.getString("TABLE_SCHEM")).append(",");
    }
    rs.close();
    s.append(System.lineSeparator()).append("Target Catalog: ").append(catalog).append(System.lineSeparator())
            .append("Target Schema: ").append(schema).append(System.lineSeparator());

    //       if (dmr.hasAuditConfigTable()){
    //           s.append("Has auditConfigSource table").append(System.lineSeparator());
    //       }

    conn.close();

    return s.toString();

}

From source file:info.magnolia.about.app.AboutPresenter.java

public AboutView start() {

    // magnolia information
    LicenseFileExtractor licenseProperties = LicenseFileExtractor.getInstance();
    String mgnlEdition = getEditionName();
    String mgnlVersion = licenseProperties.get(LicenseFileExtractor.VERSION_NUMBER);
    String authorInstance = serverConfiguration.isAdmin() ? i18n.translate("about.app.main.instance.author")
            : i18n.translate("about.app.main.instance.public");

    // system information
    String osInfo = String.format("%s %s (%s)", magnoliaProperties.getProperty("os.name"),
            magnoliaProperties.getProperty("os.version"), magnoliaProperties.getProperty("os.arch"));
    String javaInfo = String.format("%s (build %s)", magnoliaProperties.getProperty("java.version"),
            magnoliaProperties.getProperty("java.runtime.version"));
    String serverInfo = MgnlContext.getWebContext().getServletContext().getServerInfo();

    String dbInfo;/*from w  w  w  .ja  v  a2s .  co m*/
    String dbDriverInfo;
    Connection connection = null;
    try {

        String connectionString[] = getConnectionString();

        String repoHome = magnoliaProperties.getProperty("magnolia.repositories.home");
        String repoName = getRepoName();
        connectionString[0] = StringUtils.replace(connectionString[0], "${wsp.home}",
                repoHome + "/" + repoName + "/workspaces/default");
        connection = DriverManager.getConnection(connectionString[0], connectionString[1], connectionString[2]);
        DatabaseMetaData meta = connection.getMetaData();
        dbInfo = meta.getDatabaseProductName() + " " + meta.getDatabaseProductVersion();
        if (dbInfo.toLowerCase().indexOf("mysql") != -1) {
            String engine = getMySQLEngineInfo(connection, connectionString);
            if (engine != null) {
                dbInfo += engine;
            }
        }
        dbDriverInfo = meta.getDriverName() + " " + meta.getDriverVersion();

    } catch (SQLException e) {
        log.debug("Failed to read DB and driver info from connection with {}", e.getMessage(), e);
        dbInfo = i18n.translate("about.app.main.unknown");
        dbDriverInfo = dbInfo;
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                // ignore, nothing we can do
            }
        }
    }

    String jcrInfo;
    try {
        Repository repo = JcrUtils.getRepository();
        jcrInfo = String.format("%s %s", repo.getDescriptor("jcr.repository.name"),
                repo.getDescriptor("jcr.repository.version"));
    } catch (RepositoryException e) {
        log.debug("JCR repository information is not available", e);
        jcrInfo = "-";
    }

    // Prepare information for the view
    viewData.addItemProperty(AboutView.MAGNOLIA_EDITION_KEY, new ObjectProperty<String>(mgnlEdition));
    viewData.addItemProperty(AboutView.MAGNOLIA_VERSION_KEY, new ObjectProperty<String>(mgnlVersion));
    viewData.addItemProperty(AboutView.MAGNOLIA_INSTANCE_KEY, new ObjectProperty<String>(authorInstance));
    viewData.addItemProperty(AboutView.OS_INFO_KEY, new ObjectProperty<String>(osInfo));
    viewData.addItemProperty(AboutView.JAVA_INFO_KEY, new ObjectProperty<String>(javaInfo));
    viewData.addItemProperty(AboutView.SERVER_INFO_KEY, new ObjectProperty<String>(serverInfo));
    viewData.addItemProperty(AboutView.JCR_INFO_KEY, new ObjectProperty<String>(jcrInfo));
    viewData.addItemProperty(AboutView.DB_INFO_KEY, new ObjectProperty<String>(dbInfo));
    viewData.addItemProperty(AboutView.DB_DRIVER_INFO_KEY, new ObjectProperty<String>(dbDriverInfo));
    view.setDataSource(viewData);

    return view;
}