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.easyrec.utils.spring.store.dao.DaoUtils.java

/**
 * get a Integer object from a result set column with given name or null if no such
 * column exists in the result set//from  w  ww  .  j  a va 2s . c o  m
 *
 * @param rs
 * @param columnName
 * @return the value, which may be <code>null</code>
 * @throws SQLException
 */
public static Integer getIntegerIfPresent(ResultSet rs, String columnName) throws SQLException {
    try {
        rs.findColumn(columnName);
    } catch (SQLException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("getInteger() failed for column '" + columnName
                    + "'. ResultSet doesn't contain that column");
        }
        return null;
    }
    return getInteger(rs, columnName);
}

From source file:org.easyrec.utils.spring.store.dao.DaoUtils.java

/**
 * get a Boolean object from a result set column with given name or null if no such
 * column exists in the result set//  w  w  w . j ava  2 s. c  o  m
 *
 * @param rs
 * @param columnName
 * @return the value, which may be <code>null</code>
 * @throws SQLException
 */
public static Boolean getBooleanIfPresent(ResultSet rs, String columnName) throws SQLException {
    try {
        rs.findColumn(columnName);
    } catch (SQLException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("getBoolean() failed for column '" + columnName
                    + "'. ResultSet doesn't contain that column");
        }
        return null;
    }
    return getBoolean(rs, columnName);
}

From source file:org.easyrec.utils.spring.store.dao.DaoUtils.java

/**
 * get a Date object from a result set column with given name or null if no such
 * column exists in the result set//from   w  w  w.  ja va 2  s  .  c o  m
 *
 * @param rs
 * @param columnName
 * @return the value, which may be <code>null</code>
 * @throws SQLException
 */
public static Date getDateIfPresent(ResultSet rs, String columnName) throws SQLException {
    try {
        rs.findColumn(columnName);
    } catch (SQLException e) {
        if (logger.isDebugEnabled()) {
            logger.debug(
                    "getDate() failed for column '" + columnName + "'. ResultSet doesn't contain that column");
        }
        return null;
    }
    return getDate(rs, columnName);
}

From source file:org.easyrec.utils.spring.store.dao.DaoUtils.java

/**
 * get a Double object from a result set column with given name or null if no such
 * column exists in the result set/* w w  w.  j av  a  2 s  . co  m*/
 *
 * @param rs
 * @param columnName
 * @return the value, which may be <code>null</code>
 * @throws SQLException
 */
public static Double getDoubleIfPresent(ResultSet rs, String columnName) throws SQLException {
    try {
        rs.findColumn(columnName);
    } catch (SQLException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("getDouble() failed for column '" + columnName
                    + "'. ResultSet doesn't contain that column");
        }
        return null;
    }
    return getDouble(rs, columnName);
}

From source file:org.easyrec.utils.spring.store.dao.DaoUtils.java

/**
 * get a String object from a result set column with given name or null if no such
 * column exists in the result set//  w  ww. j  a  v a 2  s  . c o m
 *
 * @param rs         rs
 * @param columnName columnName
 * @return the value, which may be <code>null</code>
 * @throws SQLException SQLException
 */
public static String getStringIfPresent(ResultSet rs, String columnName) throws SQLException {
    int colIndex;
    try {
        colIndex = rs.findColumn(columnName);
    } catch (SQLException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("getString() failed for column '" + columnName
                    + "'. ResultSet doesn't contain that column");
        }
        return null;
    }
    return rs.getString(colIndex);
}

From source file:org.easyrec.utils.spring.store.dao.DaoUtils.java

/**
* get a Blob object from a result set column with given name or null if no such
* column exists in the result set//from  w ww.  j a v a 2s  .c o  m
*
* @param rs         rs
* @param columnName columnName
* @return the value, which may be <code>null</code>
* @throws SQLException SQLException
*/
public static Blob getBlobIfPresent(ResultSet rs, String columnName) throws SQLException {
    int colIndex;
    try {
        colIndex = rs.findColumn(columnName);
    } catch (SQLException e) {
        if (logger.isDebugEnabled()) {
            logger.debug(
                    "getBlob() failed for column '" + columnName + "'. ResultSet doesn't contain that column");
        }
        return null;
    }
    return rs.getBlob(colIndex);
}

From source file:org.fao.geonet.arcgis.ArcSDEJdbcConnection.java

@Override
public Map<String, String> retrieveMetadata(AtomicBoolean cancelMonitor, String arcSDEVersion)
        throws Exception {
    Map<String, String> results = new HashMap<>();

    ArcSDEVersionFactory arcSDEVersionFactory = new ArcSDEVersionFactory();
    String metadataTable = arcSDEVersionFactory.getTableName(arcSDEVersion);
    String columnName = arcSDEVersionFactory.getMetadataColumnName(arcSDEVersion);

    String sqlQuery = "SELECT " + columnName + ", UUID FROM " + metadataTable;

    getJdbcTemplate().query(sqlQuery, new RowCallbackHandler() {
        @Override/* w w w .ja  v  a2 s . c  o  m*/
        public void processRow(ResultSet rs) throws SQLException {
            // Cancel processing
            if (cancelMonitor.get()) {
                Log.warning(ARCSDE_LOG_MODULE_NAME,
                        "Cancelling metadata retrieve using " + "ArcSDE connection (via JDBC)");
                rs.getStatement().cancel();
                results.clear();
            }

            String document = "";
            int colId = rs.findColumn(columnName);
            int colIdUuid = rs.findColumn("UUID");
            // very simple type check:
            if (rs.getObject(colId) != null) {
                if (rs.getMetaData().getColumnType(colId) == Types.BLOB) {
                    Blob blob = rs.getBlob(columnName);
                    byte[] bdata = blob.getBytes(1, (int) blob.length());
                    document = new String(bdata);

                } else if (rs.getMetaData().getColumnType(colId) == Types.LONGVARBINARY) {
                    byte[] byteData = rs.getBytes(colId);
                    document = new String(byteData);

                } else if (rs.getMetaData().getColumnType(colId) == Types.LONGNVARCHAR
                        || rs.getMetaData().getColumnType(colId) == Types.LONGVARCHAR
                        || rs.getMetaData().getColumnType(colId) == Types.VARCHAR
                        || rs.getMetaData().getColumnType(colId) == Types.SQLXML) {
                    document = rs.getString(colId);

                } else {
                    throw new SQLException("Trying to harvest from a column with an invalid datatype: "
                            + rs.getMetaData().getColumnTypeName(colId));
                }

                String uuid = rs.getString(colIdUuid);
                ;
                results.put(uuid, document);
            }

        }
    });

    Log.info(ARCSDE_LOG_MODULE_NAME,
            "Finished retrieving metadata, found: #" + results.size() + " metadata records");

    return results;
}

From source file:org.georepublic.db.utils.ResultSetConverter.java

public static JSONArray convertGeoJson(ResultSet rs) throws SQLException, JSONException {

    JSONArray json = new JSONArray();
    ResultSetMetaData rsmd = rs.getMetaData();

    while (rs.next()) {
        int numColumns = rsmd.getColumnCount();
        JSONObject obj = new JSONObject();
        JSONObject feat = new JSONObject();

        feat.put("type", "Feature");

        for (int i = 1; i < numColumns + 1; i++) {
            String column_name = rsmd.getColumnName(i);

            if (StringUtils.equals(column_name, "the_geom")) {
                continue;
            }/*from   www  .java2s .  c o  m*/
            if (StringUtils.equals(column_name, "geojson")) {
                continue;
            }
            if (rsmd.getColumnType(i) == java.sql.Types.ARRAY) {
                obj.put(column_name, rs.getArray(column_name));
            } else if (rsmd.getColumnType(i) == java.sql.Types.BIGINT) {
                obj.put(column_name, rs.getInt(column_name));
            } else if (rsmd.getColumnType(i) == java.sql.Types.BOOLEAN) {
                obj.put(column_name, rs.getBoolean(column_name));
            } else if (rsmd.getColumnType(i) == java.sql.Types.BLOB) {
                obj.put(column_name, rs.getBlob(column_name));
            } else if (rsmd.getColumnType(i) == java.sql.Types.DOUBLE) {
                obj.put(column_name, rs.getDouble(column_name));
            } else if (rsmd.getColumnType(i) == java.sql.Types.FLOAT) {
                obj.put(column_name, rs.getFloat(column_name));
            } else if (rsmd.getColumnType(i) == java.sql.Types.INTEGER) {
                obj.put(column_name, rs.getInt(column_name));
            } else if (rsmd.getColumnType(i) == java.sql.Types.NVARCHAR) {
                obj.put(column_name, rs.getNString(column_name));
            } else if (rsmd.getColumnType(i) == java.sql.Types.VARCHAR) {
                obj.put(column_name, rs.getString(column_name));
            } else if (rsmd.getColumnType(i) == java.sql.Types.TINYINT) {
                obj.put(column_name, rs.getInt(column_name));
            } else if (rsmd.getColumnType(i) == java.sql.Types.SMALLINT) {
                obj.put(column_name, rs.getInt(column_name));
            } else if (rsmd.getColumnType(i) == java.sql.Types.DATE) {
                obj.put(column_name, rs.getDate(column_name));
            } else if (rsmd.getColumnType(i) == java.sql.Types.TIMESTAMP) {
                obj.put(column_name, rs.getTimestamp(column_name));
            } else {
                obj.put(column_name, rs.getObject(column_name));
            }
        }

        feat.put("properties", obj);

        try {
            rs.findColumn("lon");
            rs.findColumn("lat");

            JSONObject geo = new JSONObject();
            JSONArray coord = new JSONArray();

            coord.put(rs.getDouble("lon"));
            coord.put(rs.getDouble("lat"));

            geo.put("type", "point");
            geo.put("coordinates", coord);

            feat.put("geometry", geo);
        } catch (Exception ex1) {
            ;
        }

        json.put(feat);
    }

    return json;

}

From source file:org.hyperic.hq.appdef.server.session.AppdefStatDAO.java

public ResourceTreeNode[] getNavMapDataForApplication(AuthzSubject subject, final Application app)
        throws SQLException {
    StringBuffer buf = new StringBuffer().append("SELECT appsvc.service_id, pm.name,")
            .append(" appsvc.service_type_id,").append(" svct.name as service_type_name,")
            .append(" appsvc.application_id, appsvc.group_id").append(" FROM EAM_APP_SERVICE appsvc, ")
            .append(TBL_SERVICE).append("_TYPE svct, ").append(TBL_GROUP).append(" grp, (")
            .append(getPermGroupSQL(subject.getId())).append(") pm")
            .append(" WHERE svct.id = appsvc.service_type_id AND ")
            .append(" grp.id = appsvc.group_id AND pm.group_id = grp.id")
            .append(" AND appsvc.application_id = ").append(app.getId()).append(" UNION ALL ")
            .append("SELECT appsvc.service_id, res2.name,").append(" appsvc.service_type_id,")
            .append(" svct.name as service_type_name,").append(" appsvc.application_id, appsvc.group_id")
            .append(" FROM EAM_APP_SERVICE appsvc, ").append(TBL_SERVICE).append("_TYPE svct, (")
            .append(getPermServiceSQL(subject.getId())).append(") pm, ").append(TBL_SERVICE)
            .append(" svc JOIN ").append(TBL_RES).append(" res2 ON svc.resource_id = res2.id ")
            .append(" WHERE svct.id = appsvc.service_type_id AND ").append(" svc.id = appsvc.service_id AND ")
            .append(" pm.service_id = svc.id AND ").append(" appsvc.application_id = ").append(app.getId())
            .append(" ORDER BY service_type_id, service_id");

    if (log.isDebugEnabled()) {
        log.debug(buf.toString());//  w w w  .j  a v  a2 s  .c o m
    }

    StopWatch timer = new StopWatch();
    ResourceTreeNode[] appNode = this.jdbcTemplate.query(buf.toString(),
            new ResultSetExtractor<ResourceTreeNode[]>() {
                public ResourceTreeNode[] extractData(ResultSet rs) throws SQLException, DataAccessException {
                    Map<String, ResourceTreeNode> svcMap = new HashMap<String, ResourceTreeNode>();

                    ResourceTreeNode appNode = new ResourceTreeNode(app.getName(),
                            getAppdefTypeLabel(app.getEntityId().getType(),
                                    app.getAppdefResourceType().getName()),
                            app.getEntityId(), ResourceTreeNode.RESOURCE);

                    int svc_id_col = rs.findColumn("service_id"), name_col = rs.findColumn("name"),
                            service_type_col = rs.findColumn("service_type_id"),
                            type_name_col = rs.findColumn("service_type_name"),
                            group_id_col = rs.findColumn("group_id");

                    while (rs.next()) {
                        int serviceId = rs.getInt(svc_id_col);
                        String serviceName = rs.getString(name_col);
                        int serviceTypeId = rs.getInt(service_type_col);
                        String serviceTypeName = rs.getString(type_name_col);
                        int groupId = rs.getInt(group_id_col);
                        String thisGroupName = rs.getString(name_col);
                        // means that column is null, hence row is not a group
                        if (groupId == 0) {
                            thisGroupName = null;
                        } else {
                            serviceName = null;
                        }

                        if (thisGroupName != null) {
                            String key = APPDEF_TYPE_GROUP + "-" + groupId;
                            svcMap.put(key, new ResourceTreeNode(thisGroupName,
                                    getAppdefTypeLabel(APPDEF_TYPE_GROUP, serviceTypeName),
                                    AppdefEntityID.newGroupID(new Integer(groupId)), ResourceTreeNode.CLUSTER));
                        } else if (serviceName != null) {
                            String key = APPDEF_TYPE_SERVICE + "-" + serviceId;
                            svcMap.put(key,
                                    new ResourceTreeNode(serviceName,
                                            getAppdefTypeLabel(APPDEF_TYPE_SERVICE, serviceTypeName),
                                            AppdefEntityID.newServiceID(new Integer(serviceId)),
                                            app.getEntityId(), serviceTypeId));
                        }
                    }

                    appNode.setSelected(true);
                    ResourceTreeNode[] svcNodes = svcMap.values().toArray(new ResourceTreeNode[0]);
                    ResourceTreeNode.alphaSortNodes(svcNodes);
                    appNode.addDownChildren(svcNodes);

                    return new ResourceTreeNode[] { appNode };
                }
            });

    if (log.isDebugEnabled()) {
        log.debug("getNavMapDataForApplication() executed in: " + timer);
        log.debug("SQL: " + buf);
    }
    return appNode;
}

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

private TreeSet<HighLowMetricValue> getRawDataPoints(Measurement m, long begin, long end,
        AtomicLong publishedInterval) {
    final StringBuilder sqlBuf = getRawDataSql(m, begin, end, publishedInterval);
    final TreeSet<HighLowMetricValue> rtn = new TreeSet<HighLowMetricValue>(getTimestampComparator());
    Connection conn = null;/*from  w w w  .  j  av  a  2  s.  c  om*/
    Statement stmt = null;
    ResultSet rs = null;
    try {
        conn = safeGetConnection();
        stmt = conn.createStatement();
        rs = stmt.executeQuery(sqlBuf.toString());
        final int valCol = rs.findColumn("value");
        final int timestampCol = rs.findColumn("timestamp");
        while (rs.next()) {
            final double val = rs.getDouble(valCol);
            final long timestamp = rs.getLong(timestampCol);
            rtn.add(new HighLowMetricValue(val, timestamp));
        }
    } catch (SQLException e) {
        throw new SystemException(e);
    } finally {
        DBUtil.closeJDBCObjects(LOG_CTX, conn, stmt, rs);
    }
    return rtn;
}