Example usage for java.sql ResultSet findColumn

List of usage examples for java.sql ResultSet findColumn

Introduction

In this page you can find the example usage for java.sql ResultSet findColumn.

Prototype

int findColumn(String columnLabel) throws SQLException;

Source Link

Document

Maps the given ResultSet column label to its ResultSet column index.

Usage

From source file:org.hyperic.hq.measurement.server.session.DataManagerImpl.java

private AggMetricValue[] getHistDataSet(Integer[] mids, long start, long finish, long rangeBegin, long rangeEnd,
        long windowSize, final boolean returnNulls, AtomicLong publishedInterval, String threadName) {
    final CharSequence sqlBuf = getRawDataSql(mids, rangeBegin, rangeEnd, publishedInterval);
    final int buckets = (int) ((finish - start) / windowSize);
    final AggMetricValue[] array = new AggMetricValue[buckets];
    Connection conn = null;/*from  www .  ja  v  a  2s .co  m*/
    Statement stmt = null;
    ResultSet rs = null;
    try {
        conn = safeGetConnection();
        stmt = conn.createStatement();
        int timeout = stmt.getQueryTimeout();
        if (timeout == 0) {
            stmt.setQueryTimeout(transactionTimeout);
        }
        rs = stmt.executeQuery("/* " + threadName + " */ " + sqlBuf.toString());
        final int sumValCol = rs.findColumn("sumvalue");
        final int countValCol = rs.findColumn("cnt");
        final int minValCol = rs.findColumn("minvalue");
        final int maxValCol = rs.findColumn("maxvalue");
        final int timestampCol = rs.findColumn("timestamp");
        while (rs.next()) {
            final double sum = rs.getDouble(sumValCol);
            final double min = rs.getDouble(minValCol);
            final double max = rs.getDouble(maxValCol);
            final int count = rs.getInt(countValCol);
            final long timestamp = rs.getLong(timestampCol);
            final AggMetricValue val = new AggMetricValue(timestamp, sum, max, min, count);
            if ((timestamp < start) || (timestamp > finish)) {
                continue;
            }
            final int bucket = (int) (buckets - ((finish - timestamp) / (float) windowSize));
            if (bucket < 0) {
                continue;
            }
            merge(bucket, array, val, timestamp);
        }
    } catch (SQLException e) {
        throw new SystemException(e);
    } finally {
        DBUtil.closeJDBCObjects(getClass().getName(), conn, stmt, rs);
    }
    return array;
}

From source file:org.hyperic.hq.plugin.alfresco.AlfrescoServerDetector.java

private void setLuceneServices(List services, Connection conn, Statement stmt) throws SQLException {
    ResultSet rs = null;
    try {//from w  w w .jav  a2  s.  co m
        String sql = "select identifier,protocol from alf_store";
        rs = stmt.executeQuery(sql);
        int protocol_col = rs.findColumn("protocol");
        int identifier_col = rs.findColumn("identifier");
        while (rs.next()) {
            String protocol = rs.getString(protocol_col);
            String identifier = rs.getString(identifier_col);
            ServiceResource service = new ServiceResource();
            service.setType(this, TYPE_LUCENE);
            service.setServiceName(protocol + " / " + identifier);
            ConfigResponse productConfig = new ConfigResponse();
            productConfig.setValue(PROP_PROTOCOL, protocol);
            productConfig.setValue(PROP_IDENTIFIER, identifier);
            service.setProductConfig(productConfig);
            service.setMeasurementConfig();
            service.setControlConfig();
            services.add(service);
        }
    } finally {
        if (rs != null)
            rs.close();
    }
}

From source file:org.hyperic.hq.plugin.mysql_stats.MySqlServerDetector.java

private void setTableServices(List services, ConfigResponse serverConfig) {

    final String tableRegex = serverConfig.getValue("tableRegex", "");

    if (tableRegex.trim().length() <= 0) {

        _log.debug("Table config is blank, skipping table AI");

        return;//from   ww  w .  ja v  a  2 s .co  m

    }

    _log.debug("Discovering tables with regex " + tableRegex);

    final Pattern regex =

            Pattern.compile(tableRegex, Pattern.CASE_INSENSITIVE);

    Statement stmt = null;

    ResultSet rs = null;

    final String sql =

            "SELECT table_name, table_schema " +

                    "FROM information_schema.tables " +

                    "WHERE lower(table_schema) != 'information_schema' " +

                    "AND engine is not null";

    try {

        stmt = _conn.createStatement();

        rs = stmt.executeQuery(sql);

        final int tableNameCol = rs.findColumn("table_name"),

                dbNameCol = rs.findColumn("table_schema");

        final boolean debug = _log.isDebugEnabled();

        while (rs.next()) {

            final String table = rs.getString(tableNameCol);

            final String dbName = rs.getString(dbNameCol);

            if (regex.matcher(table).find()) {

                if (debug) {

                    _log.debug("Adding table " + table);

                }

                ServiceResource service = new ServiceResource();

                service.setType(this, TABLE_SERVICE);

                service.setServiceName(dbName + "/" + table);

                ConfigResponse productConfig = new ConfigResponse();

                productConfig.setValue("table", table);

                productConfig.setValue("database", dbName);

                service.setProductConfig(productConfig);

                service.setMeasurementConfig(serverConfig);

                service.setControlConfig(productConfig);

                services.add(service);

            }

        }

    } catch (SQLException e) {

        _log.warn(e.getMessage(), e);

    } finally {

        DBUtil.closeJDBCObjects(_logCtx, null, stmt, rs);

    }
}

From source file:org.hyperic.hq.plugin.mysql_stats.MySqlServerDetector.java

private void setMasterSlaveStatusService(List services,

        ConfigResponse serverConfig) {//from  w  ww . j a v  a2s  .  c  o m

    String url = serverConfig.getValue(JDBCMeasurementPlugin.PROP_URL);

    String user = serverConfig.getValue(JDBCMeasurementPlugin.PROP_USER);

    String pass = serverConfig.getValue(JDBCMeasurementPlugin.PROP_PASSWORD);

    Connection conn = null;

    Statement stmt = null;

    ResultSet rs = null;

    try {

        conn = getConnection(url, user, pass, serverConfig);

        stmt = conn.createStatement();

        rs = stmt.executeQuery("show full processlist");

        int userCol = rs.findColumn("User"),

                addrCol = rs.findColumn("Host");

        while (rs.next()) {

            String pUser = rs.getString(userCol);

            if (!pUser.equalsIgnoreCase("slave")) {

                continue;

            }

            final String addr = rs.getString(addrCol);

            ServiceResource service = new ServiceResource();

            service.setType(this, SLAVE_STATUS);

            service.setServiceName(SLAVE_STATUS + " " + addr);

            ConfigResponse productConfig = new ConfigResponse();

            service.setProductConfig(productConfig);

            ConfigResponse replConfig = new ConfigResponse();

            replConfig.setValue("slaveAddress", addr);

            service.setMeasurementConfig(replConfig);

            services.add(service);

        }

    } catch (SQLException e) {

        return;

    } finally {

        DBUtil.closeJDBCObjects(_logCtx, conn, stmt, rs);

    }
}

From source file:org.hyperic.hq.plugin.mysql_stats.MySqlStatsMeasurementPlugin.java

private double getMasterSlaveStatusMetric(Metric metric) throws SQLException, MetricUnreachableException {
    Connection conn = getCachedConnection(metric);
    Statement stmt = null;//from w w  w.j  a v a  2 s  . co m
    ResultSet rs = null;
    final boolean isAvail = metric.isAvail();
    final String slaveAddr = metric.getObjectProperty("slaveAddress");
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery("show full processlist");
        int userCol = rs.findColumn("User"), addrCol = rs.findColumn("Host"), timeCol = rs.findColumn("Time");
        while (rs.next()) {
            String pUser = rs.getString(userCol);
            String addr = rs.getString(addrCol);
            if (!pUser.equalsIgnoreCase("slave") && !addr.equals(slaveAddr)) {
                continue;
            }
            if (isAvail) {
                return Metric.AVAIL_UP;
            }
            return rs.getDouble(timeCol);
        }
    } finally {
        // don't close connection, it is cached
        DBUtil.closeJDBCObjects(_logCtx, null, stmt, rs);
    }
    if (isAvail) {
        return Metric.AVAIL_DOWN;
    }
    throw new MetricUnreachableException("Cannot retrieve mysql process time for slave " + slaveAddr);
}

From source file:org.hyperic.hq.plugin.oracle.OracleServerDetector.java

private List getSegmentServices(Statement stmt, String instance) throws SQLException {
    List rtn = new ArrayList();
    ResultSet rs = null;
    try {/*www  .j a  va  2 s . c  o  m*/
        // Discover tables
        rs = stmt.executeQuery(SEGMENT_QUERY);
        int segment_col = rs.findColumn("SEGMENT_NAME");
        int ts_col = rs.findColumn("TABLESPACE_NAME");
        while (rs != null && rs.next()) {
            String segment = rs.getString(segment_col);
            String tablespace = rs.getString(ts_col);
            ServiceResource service = new ServiceResource();
            service.setType(this, SEGMENT);
            service.setServiceName(segment);
            service.setDescription("Segment in the " + instance + " database instance");
            ConfigResponse productConfig = new ConfigResponse();
            ConfigResponse metricConfig = new ConfigResponse();
            productConfig.setValue(OracleMeasurementPlugin.PROP_SEGMENT, segment);
            productConfig.setValue(OracleMeasurementPlugin.PROP_TABLESPACE, tablespace);
            service.setProductConfig(productConfig);
            service.setMeasurementConfig(metricConfig);
            service.setControlConfig();
            rtn.add(service);
        }
    } finally {
        DBUtil.closeResultSet(log, rs);
    }
    return rtn;
}

From source file:org.hyperic.hq.plugin.oracle.OracleServerDetector.java

private List getTablespaceServices(Statement stmt, String instance) throws SQLException {
    List rtn = new ArrayList();
    ResultSet rs = null;
    try {/*from   w  ww  .  j  a v a2s .  co  m*/
        // Discover tablespaces
        rs = stmt.executeQuery(TABLESPACE_QUERY);
        int ts_col = rs.findColumn("TABLESPACE_NAME");
        while (rs != null && rs.next()) {
            String tablespace = rs.getString(ts_col);
            ServiceResource service = new ServiceResource();
            service.setType(this, TABLESPACE);
            service.setServiceName(tablespace);
            service.setDescription("Tablespace on the " + instance + " database instance");
            ConfigResponse productConfig = new ConfigResponse();
            ConfigResponse metricConfig = new ConfigResponse();
            productConfig.setValue(OracleMeasurementPlugin.PROP_TABLESPACE, tablespace);
            service.setProductConfig(productConfig);
            service.setMeasurementConfig(metricConfig);
            ConfigResponse svcProps = new ConfigResponse();
            // 9i and 10g only
            if (!getTypeInfo().getVersion().equals(VERSION_8i)) {
                svcProps.setValue("block_size", rs.getString("BLOCK_SIZE"));
                svcProps.setValue("allocation_type", rs.getString("ALLOCATION_TYPE"));
                svcProps.setValue("space_management", rs.getString("SEGMENT_SPACE_MANAGEMENT"));
            }
            svcProps.setValue("contents", rs.getString("CONTENTS"));
            svcProps.setValue("logging", rs.getString("LOGGING"));
            service.setCustomProperties(svcProps);
            rtn.add(service);
        }
    } finally {
        DBUtil.closeResultSet(log, rs);
    }
    return rtn;
}

From source file:org.hyperic.hq.plugin.sybase.SybaseMeasurementPlugin.java

private ResultSet getResultSet(Statement stmt, String col) throws SQLException {
    do {/*from   w w  w .jav a 2s . c  o  m*/
        ResultSet rs = null;
        try {
            rs = stmt.getResultSet();
            if (rs == null)
                break;
            rs.findColumn(col);
            return rs;
        } catch (SQLException e) {
            //don't close the resultset!!!
        }
    } while (stmt.getMoreResults() == true && stmt.getUpdateCount() != -1);
    throw new SQLException();
}

From source file:org.hyperic.hq.plugin.sybase.SybaseMeasurementPlugin.java

private float getNumReuse(Connection conn, String configOpt) throws SQLException {
    Statement stmt = null;/*from w w w. j  ava2  s  .co m*/
    ResultSet rs = null;
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery("sp_monitorconfig '" + configOpt + "'");
        ResultSetMetaData rsmd = rs.getMetaData();
        // I have seen Num_Reuse, Reuse and Reuse_cnt so far for this
        String reuseCol = "Num_Reuse";
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            String name = rsmd.getColumnName(i);
            if (name.indexOf("Reuse") != -1) {
                reuseCol = name;
            }
        }
        int col = rs.findColumn(reuseCol);
        if (rs.next()) {
            return rs.getFloat(col);
        }
    } finally {
        DBUtil.closeJDBCObjects(log, null, stmt, rs);
    }
    throw new SQLException();
}

From source file:org.hyperic.hq.plugin.sybase.SybaseServerDetector.java

private List getDatabases(Statement stmt) throws SQLException {
    List rtn = new ArrayList();
    ResultSet rs = null;
    try {//from  w w w .j a  v a 2 s.  co  m
        stmt.execute("use master");
        rs = stmt.executeQuery("select name from sysdatabases");
        int name_col = rs.findColumn("name");
        while (rs.next()) {
            String database = rs.getString(name_col);
            rtn.add(database);
        }
    } finally {
        if (rs != null)
            rs.close();
    }
    return rtn;
}