Example usage for java.sql DatabaseMetaData getURL

List of usage examples for java.sql DatabaseMetaData getURL

Introduction

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

Prototype

String getURL() throws SQLException;

Source Link

Document

Retrieves the URL for this DBMS.

Usage

From source file:net.solarnetwork.node.dao.jdbc.derby.DerbyOnlineSyncJob.java

private String getDbPath() {
    String dbPath = jdbcOperations.execute(new ConnectionCallback<String>() {
        @Override/*from w  w  w. j av  a  2  s  . c o  m*/
        public String doInConnection(Connection con) throws SQLException, DataAccessException {
            DatabaseMetaData meta = con.getMetaData();
            String url = meta.getURL();
            Pattern pat = Pattern.compile("^jdbc:derby:(\\w+)", Pattern.CASE_INSENSITIVE);
            Matcher m = pat.matcher(url);
            String dbName;
            if (m.find()) {
                dbName = m.group(1);
            } else {
                log.warn("Unable to find Derby database name in connection URL: {}", url);
                return null;
            }

            String home = System.getProperty("derby.system.home", "");
            File f = new File(home, dbName);
            return f.getPath();
        }
    });
    return dbPath;
}

From source file:fi.luontola.cqrshotel.JdbcConfiguration.java

@PostConstruct
public void statusReport() throws SQLException {
    try (Connection connection = DataSourceUtils.getConnection(dataSource)) {
        DatabaseMetaData metaData = connection.getMetaData();
        log.info("Database: {} {}", metaData.getDatabaseProductName(), metaData.getDatabaseProductVersion());
        log.info("User: {}", metaData.getUserName());
        log.info("Connection URL: {} (configuration was {})", metaData.getURL(), dataSourceProperties.getUrl());
        log.info("Flyway locations: {}", flywayProperties.getLocations());
    }//from w  w  w.  ja v  a 2s  .com
}

From source file:com.alibaba.dubbo.governance.status.DatabaseStatusChecker.java

public Status check() {
    boolean ok;/*from www . j a va 2  s .c  o  m*/
    try {
        Connection connection = dataSource.getConnection();
        try {
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet resultSet = metaData.getTypeInfo();
            try {
                ok = resultSet.next();
            } finally {
                resultSet.close();
            }
            if (message == null) {
                message = metaData.getURL() + " (" + metaData.getDatabaseProductName() + " "
                        + metaData.getDatabaseProductVersion() + ", "
                        + getIsolation(metaData.getDefaultTransactionIsolation()) + ")";
            }
            if (version == 0) {
                version = metaData.getDatabaseMajorVersion();
            }
        } finally {
            connection.close();
        }
    } catch (Throwable e) {
        logger.error(e.getMessage(), e);
        ok = false;
    }
    return new Status(!ok ? Status.Level.ERROR : (version < 5 ? Status.Level.WARN : Status.Level.OK), message);
}

From source file:DatabaseInfo.java

public void doGet(HttpServletRequest inRequest, HttpServletResponse outResponse)
        throws ServletException, IOException {

    PrintWriter out = null;/* ww w  . j  a v a  2 s  .  co m*/
    Connection connection = null;
    Statement statement;
    ResultSet rs;

    outResponse.setContentType("text/html");
    out = outResponse.getWriter();

    try {
        Context ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
        connection = ds.getConnection();

        DatabaseMetaData md = connection.getMetaData();
        statement = connection.createStatement();

        out.println("<HTML><HEAD><TITLE>Database Server Information</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("<H1>General Source Information</H1>");
        out.println("getURL() - " + md.getURL() + "<BR>");
        out.println("getUserName() - " + md.getUserName() + "<BR>");
        out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion() + "<BR>");
        out.println("getDriverMajorVersion - " + md.getDriverMajorVersion() + "<BR>");
        out.println("getDriverMinorVersion - " + md.getDriverMinorVersion() + "<BR>");
        out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh() + "<BR>");

        out.println("<H1>Feature Support</H1>");
        out.println("supportsAlterTableWithDropColumn - " + md.supportsAlterTableWithDropColumn() + "<BR>");
        out.println("supportsBatchUpdates - " + md.supportsBatchUpdates() + "<BR>");
        out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames() + "<BR>");
        out.println("supportsPositionedDelete - " + md.supportsPositionedDelete() + "<BR>");
        out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins() + "<BR>");
        out.println("supportsStoredProcedures - " + md.supportsStoredProcedures() + "<BR>");
        out.println("supportsMixedCaseQuotedIdentifiers - " + md.supportsMixedCaseQuotedIdentifiers() + "<BR>");
        out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL() + "<BR>");
        out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar() + "<BR>");

        out.println("<H1>Data Source Limits</H1>");
        out.println("getMaxRowSize - " + md.getMaxRowSize() + "<BR>");
        out.println("getMaxStatementLength - " + md.getMaxStatementLength() + "<BR>");
        out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect() + "<BR>");
        out.println("getMaxConnections - " + md.getMaxConnections() + "<BR>");
        out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength() + "<BR>");

        out.println("<H1>SQL Object Available</H1>");
        out.println("getTableTypes()<BR><UL>");
        rs = md.getTableTypes();
        while (rs.next()) {
            out.println("<LI>" + rs.getString(1));
        }
        out.println("</UL>");

        out.println("getTables()<BR><UL>");
        rs = md.getTables("accounts", "", "%", new String[0]);
        while (rs.next()) {
            out.println("<LI>" + rs.getString("TABLE_NAME"));
        }
        out.println("</UL>");

        out.println("<H1>Transaction Support</H1>");
        out.println("getDefaultTransactionIsolation() - " + md.getDefaultTransactionIsolation() + "<BR>");
        out.println(
                "dataDefinitionIgnoredInTransactions() - " + md.dataDefinitionIgnoredInTransactions() + "<BR>");

        out.println("<H1>General Source Information</H1>");
        out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect() + "<BR>");
        out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable() + "<BR>");
        out.println("getTimeDateFunctions - " + md.getTimeDateFunctions() + "<BR>");
        out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar() + "<BR>");

        out.println("getTypeInfo()<BR><UL>");
        rs = md.getTypeInfo();
        while (rs.next()) {
            out.println("<LI>" + rs.getString(1));
        }
        out.println("</UL>");

        out.println("</BODY></HTML>");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.haulmont.cuba.core.sys.dbupdate.DbUpdaterEngine.java

protected String getConnectionUrl(Connection connection) {
    try {/*from w  ww . j ava 2s .  c  om*/
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        return databaseMetaData.getURL();
    } catch (Throwable e) {
        log.warn("Unable to get connection url");
        return null;
    }
}

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

protected Environment lookupDB() {
    try {//  ww w.  j a v  a 2s.  c om
        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:net.sf.jasperreports.engine.query.OracleProcedureCallHandler.java

protected boolean isDataDirectDriver() {
    Connection connection;//from ww w.  java2 s  . c om
    try {
        connection = statement.getConnection();
    } catch (SQLException e) {
        log.error("Failure while detecting driver", e);
        return false;
    }

    DatabaseMetaData metaData = null;
    try {
        metaData = connection.getMetaData();
    } catch (SQLException e) {
        log.error("Failure while detecting driver", e);
    }

    String connectionURL = null;
    if (metaData != null) {
        try {
            connectionURL = metaData.getURL();
        } catch (SQLException e) {
            log.error("Failure while detecting driver", e);
        }
    }

    if (connectionURL != null) {
        if (connectionURL.contains(URL_DATADIRECT) || connectionURL.contains(URL_TIBCO)) {
            return true;
        }
        if (connectionURL.contains(URL_ORACLE)) {
            return false;
        }
    }

    if (ORACLE_CONNECTION_CLASS != null) {
        try {
            if (connection.isWrapperFor(ORACLE_CONNECTION_CLASS)) {
                return false;
            }
        } catch (SQLException e) {
            log.error("Failure while detecting driver", e);
        }
    }

    if (metaData != null) {
        try {
            String driverName = metaData.getDriverName();
            if (driverName.equals(DRIVER_NAME_ORACLE)) {
                return false;
            }
            if (driverName.equals(DRIVER_NAME_DATADIRECT)) {
                return true;
            }
        } catch (SQLException e) {
            log.error("Failure while detecting driver", e);
        }
    }

    //fallback to Oracle
    return false;
}

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

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

    try {//from w  ww.  j av a2 s.  c o m
        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:com.nextep.designer.sqlgen.ui.editors.SQLEditor.java

/**
 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#dispose()
 *///from   w  w w. j a  va2 s . c o m
@Override
public void dispose() {
    Designer.getListenerService().unregisterListeners(this);
    IEditorInput input = getEditorInput();
    if (input instanceof IConnectable) {
        final IConnectable connectableInput = (IConnectable) input;
        Connection conn = connectableInput.getSqlConnection();
        if (conn != null) {
            try {
                final DatabaseMetaData md = conn.getMetaData();
                log.info("Disconnecting from " + md.getURL());
                conn.close();
                connectableInput.setSqlConnection(null);
            } catch (SQLException e) {
                log.warn("Unable to close connection: " + e.getMessage(), e);
            }
        }
    }
    super.dispose();
}

From source file:jef.database.DbUtils.java

/**
 * ???/*  ww  w.  j a v a  2  s  .  c o  m*/
 * 
 * @param conn
 * @return
 * @throws SQLException
 */
public static ConnectInfo tryAnalyzeInfo(Connection conn) throws SQLException {
    DatabaseMetaData meta = conn.getMetaData();
    ConnectInfo info = new ConnectInfo();
    info.user = meta.getUserName();
    info.url = meta.getURL();
    info.parse();// ?profile, ?????
    return info;
}