Example usage for java.sql ResultSetMetaData getColumnName

List of usage examples for java.sql ResultSetMetaData getColumnName

Introduction

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

Prototype

String getColumnName(int column) throws SQLException;

Source Link

Document

Get the designated column's name.

Usage

From source file:GetColumnNamesFromResultSet_MySQL.java

public static void getColumnNames(ResultSet rs) throws SQLException {
    if (rs == null) {
        return;//w w w. jav a2s  . com
    }
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();

    // get the column names; column indexes start from 1
    for (int i = 1; i < numberOfColumns + 1; i++) {
        String columnName = rsMetaData.getColumnName(i);
        // Get the name of the column's table name
        String tableName = rsMetaData.getTableName(i);
        System.out.println("column name=" + columnName + " table=" + tableName + "");
    }
}

From source file:GetColumnNamesFromResultSet_Oracle.java

public static void getColumnNames(ResultSet rs) throws SQLException {
    if (rs == null) {
        return;//from  ww  w.  j  a va  2s .c o m
    }
    // get result set meta data
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();

    // get the column names; column indexes start from 1
    for (int i = 1; i < numberOfColumns + 1; i++) {
        String columnName = rsMetaData.getColumnName(i);
        // Get the name of the column's table name
        String tableName = rsMetaData.getTableName(i);
        System.out.println("column name=" + columnName + " table=" + tableName + "");
    }
}

From source file:jongo.handler.JongoResultSetHandler.java

/**
 * Converts a ResultSet to a Map. Important to note that DATE, TIMESTAMP & TIME objects generate
 * a {@linkplain org.joda.time.DateTime} object using {@linkplain org.joda.time.format.ISODateTimeFormat}.
 * @param resultSet a {@linkplain java.sql.ResultSet}
 * @return a Map with the column names as keys and the values. null if something goes wrong.
 *//* ww  w. j  av a2s .  c  om*/
public static Map<String, String> resultSetToMap(ResultSet resultSet) {
    Map<String, String> map = new HashMap<String, String>();
    try {
        int columnCount = resultSet.getMetaData().getColumnCount();

        l.trace("Mapping a result set with " + columnCount + " columns to a Map");

        ResultSetMetaData meta = resultSet.getMetaData();
        for (int i = 1; i < columnCount + 1; i++) {
            String colName = meta.getColumnName(i).toLowerCase();
            int colType = meta.getColumnType(i);
            String v = resultSet.getString(i);
            if (colType == Types.DATE) {
                v = new DateTime(resultSet.getDate(i)).toString(dateFTR);
                l.trace("Mapped DATE column " + colName + " with value : " + v);
            } else if (colType == Types.TIMESTAMP) {
                v = new DateTime(resultSet.getTimestamp(i)).toString(dateTimeFTR);
                l.trace("Mapped TIMESTAMP column " + colName + " with value : " + v);
            } else if (colType == Types.TIME) {
                v = new DateTime(resultSet.getTimestamp(i)).toString(timeFTR);
                l.trace("Mapped TIME column " + colName + " with value : " + v);
            } else {
                l.trace("Mapped " + meta.getColumnTypeName(i) + " column " + colName + " with value : " + v);
            }
            map.put(colName, v);
        }
    } catch (SQLException e) {
        l.error("Failed to map ResultSet");
        l.error(e.getMessage());
        return null;
    }

    return map;
}

From source file:opa.Utils.java

/**
 * This method will convert a ResultSet row into a key/value Map.
 * //from w  w w  .  j av  a2s  .  c om
 * @param rs
 * @return A key/value map that may be empty if no fields were present in
 *         the ResultSet or null if an error occurred while getting data
 *         from the ResultSet
 */
public static Map<String, Object> resultSetToMap(ResultSet rs) {
    Map<String, Object> map = new HashMap<String, Object>();

    try {
        ResultSetMetaData metaData = rs.getMetaData();
        for (int i = 1; i <= metaData.getColumnCount(); ++i) { // heh, offset starting at 1
            String key = metaData.getColumnName(i);
            Object value = rs.getObject(i);

            map.put(key, value);
        }
    } catch (Exception e) {
        log.error(e, e);
        map = null;
    }

    return map;
}

From source file:com.splout.db.engine.JDBCManager.java

public static QueryResult convertResultSetToQueryResult(ResultSet rs, int maxResults) throws SQLException {
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    String[] columnNames = new String[columns];
    for (int i = 0; i < columns; i++) {
        columnNames[i] = md.getColumnName(i);
    }//from ww w  .j  ava  2s  .c o  m

    List<Object[]> list = new ArrayList<Object[]>();
    while (rs.next() && list.size() < maxResults) {
        Object[] row = new Object[columns];
        for (int i = 1; i <= columns; ++i) {
            row[i] = rs.getObject(i);
        }
        list.add(row);
    }

    if (list.size() == maxResults) {
        throw new SQLException("Hard limit on number of results reached (" + maxResults
                + "), please use a LIMIT for this query.");
    }

    return new QueryResult(columnNames, list);
}

From source file:Main.java

public static Document documentify(ResultSet rs) throws ParserConfigurationException, SQLException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element results = doc.createElement("Results");
    doc.appendChild(results);/*from   ww  w. j a va 2 s .co m*/
    ResultSetMetaData rsmd = rs.getMetaData();
    int colCount = rsmd.getColumnCount();

    while (rs.next()) {
        Element row = doc.createElement("Row");
        results.appendChild(row);
        for (int i = 1; i <= colCount; i++) {
            String columnName = rsmd.getColumnName(i);
            Object value = rs.getObject(i);
            Element node = doc.createElement(columnName);
            node.appendChild(doc.createTextNode(value.toString()));
            row.appendChild(node);
        }
    }
    return doc;
}

From source file:modelo.ApiManager.java

public static List resultSetToArrayList(ResultSet rs) throws SQLException {
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    ArrayList list = new ArrayList();

    while (rs.next()) {
        HashMap row = new HashMap(columns);
        for (int i = 1; i <= columns; ++i) {
            row.put(md.getColumnName(i), rs.getObject(i));
        }/*from  w w w .  j  a v  a2 s.co m*/
        list.add(row);
    }

    return list;
}

From source file:Main.java

private static void addChildren(Writer writer, ResultSet rs) throws SQLException, IOException {
    ResultSetMetaData metaData = rs.getMetaData();
    int nbColumns = metaData.getColumnCount();
    StringBuffer buffer = new StringBuffer();
    while (rs.next()) {
        buffer.setLength(0);/*from   w w w .  ja  va  2 s  .co  m*/
        buffer.append("<" + metaData.getTableName(1) + ">");
        for (int i = 1; i <= nbColumns; i++) {
            buffer.append("<" + metaData.getColumnName(i) + ">");
            buffer.append(rs.getString(i));
            buffer.append("</" + metaData.getColumnName(i) + ">");
        }
        buffer.append("</" + metaData.getTableName(1) + ">");
        writer.write(buffer.toString());
    }
}

From source file:Main.java

public static Element appendResultSetToNode(Element root, String rowTag, ResultSet rs) throws SQLException {
    Document doc = root.getOwnerDocument();

    ResultSetMetaData meta = rs.getMetaData();
    int columnCount = meta.getColumnCount();
    int rowCount = 0;
    while (rs.next()) {
        Element rowElement = doc.createElement(rowTag);
        rowElement.setAttribute("row", "" + rowCount);
        for (int i = 1; i <= columnCount; i++) {
            rowElement.setAttribute(meta.getColumnName(i), rs.getString(i));
        }/* w ww .  jav a  2  s. c  om*/
        rowCount++;
        root.appendChild(rowElement);
    }

    return root;
}

From source file:com.datamoin.tajo.tpcds.TpcDSTestUtil.java

public static String resultSetToString(ResultSet resultSet) throws SQLException {
    StringBuilder sb = new StringBuilder();
    ResultSetMetaData rsmd = resultSet.getMetaData();
    int numOfColumns = rsmd.getColumnCount();

    for (int i = 1; i <= numOfColumns; i++) {
        if (i > 1)
            sb.append(",");
        String columnName = rsmd.getColumnName(i);
        sb.append(columnName);//w w w.  ja v  a  2s.  com
    }
    sb.append("\n-------------------------------\n");

    while (resultSet.next()) {
        for (int i = 1; i <= numOfColumns; i++) {
            if (i > 1)
                sb.append(",");
            String columnValue = resultSet.getString(i);
            if (resultSet.wasNull()) {
                columnValue = "null";
            }
            sb.append(columnValue);
        }
        sb.append("\n");
    }
    return sb.toString();
}