Example usage for java.sql ResultSetMetaData getColumnCount

List of usage examples for java.sql ResultSetMetaData getColumnCount

Introduction

In this page you can find the example usage for java.sql ResultSetMetaData getColumnCount.

Prototype

int getColumnCount() throws SQLException;

Source Link

Document

Returns the number of columns in this ResultSet object.

Usage

From source file:net.orpiske.ssps.common.dependencies.cache.MultiRsHandler.java

@Override
protected DependencyCacheDto handleRow(ResultSet rs) throws SQLException {
    DependencyCacheDto dto = new DependencyCacheDto();

    ResultSetMetaData meta = rs.getMetaData();

    for (int i = 1; i <= meta.getColumnCount(); i++) {
        Object value = rs.getObject(i);
        String name = meta.getColumnName(i);

        try {/*from ww w  .ja  v a 2s .c om*/
            /*
             * We convert the column name to a more appropriate and java like name 
             * because some columns are usually named as some_thing whereas Java 
             * properties are named someThing. This call does this conversion.
             */
            String javaProperty = NameConverter.sqlToProperty(name);

            if (javaProperty.equals("version")) {
                Version version = Version.toVersion((String) value);

                PropertyUtils.setSimpleProperty(dto, javaProperty, version);
            } else {
                PropertyUtils.setSimpleProperty(dto, javaProperty, value);
            }
        } catch (Exception e) {
            throw new SQLException("Unable to set property " + name + " for bean" + dto.getClass(), e);
        }
    }

    return dto;
}

From source file:net.orpiske.ssps.common.registry.MultiRsHandler.java

@Override
protected SoftwareInventoryDto handleRow(ResultSet rs) throws SQLException {
    SoftwareInventoryDto dto = new SoftwareInventoryDto();

    ResultSetMetaData meta = rs.getMetaData();

    for (int i = 1; i <= meta.getColumnCount(); i++) {
        Object value = rs.getObject(i);
        String name = meta.getColumnName(i);

        try {/* w ww .  j a  v  a2  s .c  om*/
            /*
             * We convert the column name to a more appropriate and java like name 
             * because some columns are usually named as some_thing whereas Java 
             * properties are named someThing. This call does this conversion.
             */
            String javaProperty = NameConverter.sqlToProperty(name);

            if (javaProperty.equals("version")) {
                Version version = Version.toVersion((String) value);

                PropertyUtils.setSimpleProperty(dto, javaProperty, version);
            } else {
                PropertyUtils.setSimpleProperty(dto, javaProperty, value);
            }
        } catch (Exception e) {
            throw new SQLException("Unable to set property " + name + " for bean" + dto.getClass(), e);
        }
    }

    return dto;
}

From source file:net.orpiske.ssps.common.repository.search.cache.MultiRsHandler.java

@Override
protected PackageInfo handleRow(ResultSet rs) throws SQLException {
    PackageInfo dto = new PackageInfo();

    ResultSetMetaData meta = rs.getMetaData();

    for (int i = 1; i <= meta.getColumnCount(); i++) {
        Object value = rs.getObject(i);
        String name = meta.getColumnName(i);

        try {/*from   ww  w.j  a  va  2  s . c o m*/
            /*
             * We convert the column name to a more appropriate and java like name 
             * because some columns are usually named as some_thing whereas Java 
             * properties are named someThing. This call does this conversion.
             */
            String javaProperty = NameConverter.sqlToProperty(name);

            if (javaProperty.equals("version")) {
                Version version = Version.toVersion((String) value);

                PropertyUtils.setSimpleProperty(dto, javaProperty, version);
            } else {
                PropertyUtils.setSimpleProperty(dto, javaProperty, value);
            }
        } catch (Exception e) {
            throw new SQLException("Unable to set property " + name + " for bean" + dto.getClass(), e);
        }
    }

    return dto;
}

From source file:com.job.portal.utils.AbstractDAO.java

protected JSONObject getJSONObject(ResultSet rs) throws SQLException, JSONException {
    JSONObject obj = null;//from   w  w w.  java 2s  . c o  m
    ResultSetMetaData rsmd = rs.getMetaData();
    int size = rsmd.getColumnCount();
    if (rs.next()) {
        obj = new JSONObject();
        for (int i = 1; i <= size; i++) {
            obj.put(rsmd.getColumnName(i), rs.getString(i));
        }
    }
    return obj;
}

From source file:com.job.portal.utils.AbstractDAO.java

protected JSONArray getJSONArray(ResultSet rs) throws SQLException, JSONException {
    JSONArray arr = new JSONArray();
    JSONObject obj = null;//  ww w . jav  a2s  .c  o  m
    ResultSetMetaData rsmd = rs.getMetaData();
    int size = rsmd.getColumnCount();
    while (rs.next()) {
        obj = new JSONObject();
        for (int i = 1; i <= size; i++) {
            obj.put(rsmd.getColumnName(i), rs.getString(i));
        }
        arr.put(obj);
    }
    return arr;
}

From source file:com.kumarvv.setl.utils.SqlRunner.java

/**
 * retrieves single row Map from datasource
 *
 * @param sql/*from   w w  w  .j a v a 2  s  . co  m*/
 * @param ds
 * @return
 */
public Map<String, Object> getSingleRowMap(String sql, DS ds) {
    Map<String, Object> map = new HashMap<>();
    if (StringUtils.isEmpty(sql)) {
        return map;
    }

    try (JdbcRowSet jrs = rowSetUtil.getRowSet(ds)) {
        jrs.setCommand(sql);
        jrs.execute();
        if (!jrs.next()) {
            return map;
        }
        ResultSetMetaData meta = jrs.getMetaData();
        for (int i = 1; i <= meta.getColumnCount(); i++) {
            map.put(meta.getColumnName(i), jrs.getObject(i));
        }
    } catch (Exception e) {
    }
    return map;
}

From source file:net.orpiske.ssps.common.db.MultiRsHandler.java

@Override
protected T handleRow(ResultSet rs) throws SQLException {
    T dto;/*from   ww w.  j a  v a2s .  co  m*/

    try {
        dto = clazz.newInstance();
    } catch (InstantiationException e1) {
        throw new SQLException("Unable to instantiate DTO class: " + e1.getMessage(), e1);
    } catch (IllegalAccessException e1) {
        throw new SQLException("Illegal to instantiate DTO class: " + e1.getMessage(), e1);
    }

    ResultSetMetaData meta = rs.getMetaData();

    for (int i = 1; i <= meta.getColumnCount(); i++) {
        Object value = rs.getObject(i);
        String name = meta.getColumnName(i);

        try {
            /*
             * We convert the column name to a more appropriate and java like name 
             * because some columns are usually named as some_thing whereas Java 
             * properties are named someThing. This call does this conversion.
             */
            String javaProperty = NameConverter.sqlToProperty(name);

            PropertyUtils.setSimpleProperty(dto, javaProperty, value);
        } catch (Exception e) {
            throw new SQLException("Unable to set property " + name + " for bean" + dto.getClass(), e);
        }
    }

    return dto;
}

From source file:cherry.foundation.etl.ExtractorResultSetExtractor.java

@Override
public Long extractData(ResultSet rs) throws SQLException, DataAccessException {
    try {//w ww.  ja v a2s .c o m

        ResultSetMetaData metaData = rs.getMetaData();
        Column[] col = new Column[metaData.getColumnCount()];
        for (int i = 1; i <= col.length; i++) {
            col[i - 1] = new Column();
            col[i - 1].setType(metaData.getColumnType(i));
            col[i - 1].setLabel(metaData.getColumnLabel(i));
        }

        consumer.begin(col);

        long count;
        for (count = 0L; rs.next(); count++) {

            Object[] record = new Object[col.length];
            for (int i = 1; i <= record.length; i++) {
                record[i - 1] = rs.getObject(i);
            }

            consumer.consume(record);
            limiter.tick();
        }

        consumer.end();
        return count;

    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
}

From source file:net.orpiske.ssps.common.registry.SoftwareInventoryRsHandler.java

@Override
public SoftwareInventoryDto handle(ResultSet rs) throws SQLException {

    // No records to handle :O
    if (!rs.next()) {
        return null;
    }/*w  ww  . ja  va  2  s.  co m*/

    ResultSetMetaData meta = rs.getMetaData();

    for (int i = 1; i <= meta.getColumnCount(); i++) {
        Object value = rs.getObject(i);
        String name = meta.getColumnName(i);

        try {
            /*
             * We convert the column name to a more appropriate and java like name 
             * because some columns are usually named as some_thing whereas Java 
             * properties are named someThing. This call does this conversion.
             */
            String javaProperty = NameConverter.sqlToProperty(name);

            if (javaProperty.equals("version")) {
                Version version = Version.toVersion((String) value);

                PropertyUtils.setSimpleProperty(dto, javaProperty, version);
            } else {
                PropertyUtils.setSimpleProperty(dto, javaProperty, value);
            }

        } catch (Exception e) {
            throw new SQLException("Unable to set property " + name + " for bean" + dto.getClass(), e);
        }
    }

    return dto;
}

From source file:MainClass.java

public void setQuery(String q) {
    cache = new Vector();
    try {/*from w ww.  jav a  2 s  .  c  o m*/
        ResultSet rs = statement.executeQuery(q);
        ResultSetMetaData meta = rs.getMetaData();
        colCount = meta.getColumnCount();

        headers = new String[colCount];
        for (int h = 1; h <= colCount; h++) {
            headers[h - 1] = meta.getColumnName(h);
        }

        while (rs.next()) {
            String[] record = new String[colCount];
            for (int i = 0; i < colCount; i++) {
                record[i] = rs.getString(i + 1);
            }
            cache.addElement(record);
        }
        fireTableChanged(null);
    } catch (Exception e) {
        cache = new Vector();
        e.printStackTrace();
    }
}