Example usage for java.sql ResultSet getMetaData

List of usage examples for java.sql ResultSet getMetaData

Introduction

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

Prototype

ResultSetMetaData getMetaData() throws SQLException;

Source Link

Document

Retrieves the number, types and properties of this ResultSet object's columns.

Usage

From source file:com.myapp.dao.SalesOrderDAO.java

public ArrayList<SalesOrder> getAllOrders() throws SQLException {

    ArrayList<SalesOrder> retVal = null;
    try {/*from w  ww. jav  a  2s  .  c  o m*/
        // Load the driver.
        Class.forName("org.relique.jdbc.csv.CsvDriver");

        // Create a connection. CSV file is in D:
        Connection conn = DriverManager.getConnection("jdbc:relique:csv:C:");

        // Create a Statement object to execute the query with.
        Statement stmt = conn.createStatement();

        // Select columns from SalesOrder.csv
        ResultSet rs = stmt.executeQuery("SELECT * FROM SalesOrder");

        //loop through rs
        // Clean up
        ResultSetMetaData metaData = rs.getMetaData();
        int resultColumnCount = metaData.getColumnCount();

        while (rs.next()) {
            if (resultColumnCount > 1) {

                SalesOrder so = new SalesOrder();
                so.setSalesOrderID(rs.getString(1));
                so.setRevisionNumber(rs.getString(2));
                so.setOrderDate(rs.getString(3));
                so.setDueDate(rs.getString(4));
                so.setShipDate(rs.getString(5));
                so.setStatus(rs.getString(6));
                so.setOnlineOrderFlag(rs.getString(7));
                so.setSalesOrderNumber(rs.getString(8));
                so.setPurchaseOrderNumber(rs.getString(9));
                so.setAccountNumber(rs.getString(10));
                so.setCustomerID(rs.getString(11));
                so.setSalesPersonID(rs.getString(12));
                so.setTerritoryID(rs.getString(13));
                so.setBillToAddressID(rs.getString(14));
                so.setShipToAddressID(rs.getString(15));
                so.setShipMethodID(rs.getString(16));
                so.setCreditCardID(rs.getString(17));
                so.setCreditCardApprovalCode(rs.getString(18));
                so.setCurrencyRateID(rs.getString(19));
                so.setSubTotal(rs.getString(20));
                so.setTaxAmt(rs.getString(21));
                so.setFreight(rs.getString(22));
                so.setTotalDue(rs.getString(23));
                so.setComment(rs.getString(24));
                so.setModifiedDate(rs.getString(25));
                retVal.add(so);
            } else {
                Object obj = rs.getObject(1);

            }
        }

        conn.close();
    } catch (Exception e) {
        System.out.println("EXCEPTION: " + e.getMessage());
    }

    return retVal;
}

From source file:com.netspective.axiom.sql.ResultSetUtils.java

public Object[] getResultSetSingleRowArray(ResultSet rs) throws SQLException {
    if (rs.next()) {
        ResultSetMetaData rsmd = rs.getMetaData();
        int colsCount = rsmd.getColumnCount();
        Object[] result = new Object[colsCount];
        for (int i = 1; i <= colsCount; i++) {
            result[i - 1] = rs.getObject(i);
        }//from w  w w  .  ja  v a 2s . c  om
        return result;
    } else
        return null;
}

From source file:com.netspective.axiom.sql.ResultSetUtils.java

public Object[] getResultSetSingleRowAsArray(ResultSet rs) throws SQLException {
    if (rs.next()) {
        ResultSetMetaData rsmd = rs.getMetaData();
        int colsCount = rsmd.getColumnCount();
        Object[] result = new Object[colsCount];
        for (int i = 1; i <= colsCount; i++) {
            result[i - 1] = rs.getObject(i);
        }/* w w w. ja v a 2 s  .c  o  m*/
        return result;
    } else
        return null;
}

From source file:com.netspective.axiom.sql.ResultSetUtils.java

public String[] getResultSetSingleRowAsStrings(ResultSet rs) throws SQLException {
    if (rs.next()) {
        ResultSetMetaData rsmd = rs.getMetaData();
        int colsCount = rsmd.getColumnCount();
        String[] result = new String[colsCount];
        for (int i = 1; i <= colsCount; i++) {
            result[i - 1] = rs.getString(i);
        }//from w  ww .  j ava2 s  . co  m
        return result;
    } else
        return null;
}

From source file:com.mongosqlmigrator.harsha.sql.DocBuilder.java

private void getSingleValuedEntity(Map<String, Object> firstRow, ResultSet rs, Entity entity,
        Map<String, Object> entityMap) throws SQLException {
    int totalRows = rs.getMetaData().getColumnCount();

    for (int i = 0; i < totalRows; i++) {
        extractFieldValue(firstRow, entity, entityMap, rs.getMetaData().getColumnLabel(i + 1),
                rs.getMetaData().getColumnLabel(i + 1), null);
    }//from  w w w. j a va2 s. c  o m

    for (Iterator<Field> iterator = entity.fields.iterator(); iterator.hasNext();) {
        Field field = iterator.next();
        FieldType fieldType = FieldType.valueOf(field.allAttributes.get("type").toUpperCase());

        if (firstRow.get(field.column) != null) {
            entityMap.remove(field.column);
            extractFieldValue(firstRow, entity, entityMap, field.name, field.column, fieldType);
        } else if (field.defaultValue != null) {
            entityMap.put(field.name, field.defaultValue);
        }

    }
}

From source file:com.netspective.axiom.sql.ResultSetUtils.java

/**
 * Given a ResultSet, return a Map of all the column names in the ResultSet
 * in lowercase as the key and the index of the column as the value.
 *//*from w w  w. ja  v  a  2  s  .  co  m*/
public Map getColumnNamesIndexMap(ResultSet rs) throws SQLException {
    Map map = new HashMap();
    ResultSetMetaData rsmd = rs.getMetaData();
    int colsCount = rsmd.getColumnCount();
    for (int i = 1; i <= colsCount; i++) {
        map.put(rsmd.getColumnName(i).toLowerCase(), new Integer(i));
    }
    return map;
}

From source file:db.migration.V055__UpdateECTS.java

private int getNextHibernateSequence(JdbcTemplate jdbcTemplate) {
    // Returns next global id
    List<Map> resultSet = jdbcTemplate.query("SELECT nextval('public.hibernate_sequence')",
            new RowMapper<Map>() {
                @Override//w ww  . ja va2  s .  com
                public Map mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Map r = new HashMap<String, Object>();

                    ResultSetMetaData metadata = rs.getMetaData();
                    for (int i = 1; i <= metadata.getColumnCount(); i++) {
                        String cname = metadata.getColumnName(i);
                        int ctype = metadata.getColumnType(i);

                        switch (ctype) {
                        case Types.BIGINT: // id
                            r.put(cname, rs.getInt(cname));
                            break;

                        default:
                            break;
                        }
                    }

                    return r;
                }
            });

    for (Map m : resultSet) {
        return (int) m.get("nextval");
    }
    return 0;
}

From source file:com.netspective.axiom.sql.ResultSetUtils.java

public Map[] getResultSetRowsAsMapArray(ResultSet rs, boolean useLabelAsKey) throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int colsCount = rsmd.getColumnCount();
    String[] columnNames = new String[colsCount];
    for (int c = 1; c <= colsCount; c++) {
        columnNames[c - 1] = useLabelAsKey ? rsmd.getColumnLabel(c).toLowerCase()
                : rsmd.getColumnName(c).toLowerCase();
    }// ww  w.  ja va  2  s  .  c o m

    ArrayList result = new ArrayList();
    while (rs.next()) {
        Map rsMap = new HashMap();
        for (int i = 1; i <= colsCount; i++) {
            rsMap.put(columnNames[i - 1], rs.getObject(i));
        }
        result.add(rsMap);
    }

    if (result.size() > 0)
        return (Map[]) result.toArray(new Map[result.size()]);
    else
        return null;
}

From source file:annis.sqlgen.FindSqlGenerator.java

public Match mapRow(ResultSet rs, int rowNum) throws SQLException {
    Match match = new Match();

    // get size of solution
    ResultSetMetaData metaData = rs.getMetaData();
    int columnCount = metaData.getColumnCount();

    // the order of columns is not determined and I have to combined two
    // values, so save them here and combine later
    String node_name = null;/*w  w  w .  j a  v  a2 s  . c  om*/
    List<String> corpus_path = null;

    //get path
    if (outputCorpusPath) {
        for (int column = 1; column <= columnCount; ++column) {
            if (corpusPathExtractor != null && metaData.getColumnName(column).startsWith("path_name")) {
                corpus_path = corpusPathExtractor.extractCorpusPath(rs, metaData.getColumnName(column));
            }
        }
    }

    // one match per column
    for (int column = 1; column <= columnCount; ++column) {

        if (metaData.getColumnName(column).startsWith("node_name")) {
            node_name = rs.getString(column);
        } else // no more matches in this row if an id was NULL
        if (rs.wasNull()) {
            break;
        }

        if (outputCorpusPath && node_name != null) {
            match.setSaltId(buildSaltId(corpus_path, node_name));
            node_name = null;
        }
    }

    return match;
}

From source file:com.abixen.platform.service.businessintelligence.multivisualisation.application.service.database.AbstractDatabaseService.java

private ResultSetMetaData getDatabaseMetaData(Connection connection, String tableName) throws SQLException {
    Statement stmt = connection.createStatement();
    //FixMe/*from  w ww .j ava  2s  .co m*/
    ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName);
    return rs.getMetaData();
}