Example usage for java.sql ResultSet getObject

List of usage examples for java.sql ResultSet getObject

Introduction

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

Prototype

Object getObject(String columnLabel) throws SQLException;

Source Link

Document

Gets the value of the designated column in the current row of this ResultSet object as an Object in the Java programming language.

Usage

From source file:org.apache.drill.jdbc.test.JdbcAssert.java

static List<String> toStrings(ResultSet resultSet) throws SQLException {
    final List<String> list = new ArrayList<>();
    StringBuilder buf = new StringBuilder();
    final List<Ord<String>> columns = columnLabels(resultSet);
    while (resultSet.next()) {
        buf.setLength(0);/*from   w  ww.j  a v  a  2s .c o m*/
        for (Ord<String> column : columns) {
            buf.append(column.i == 1 ? "" : "; ").append(column.e).append("=")
                    .append(resultSet.getObject(column.i));
        }
        list.add(buf.toString());
    }
    return list;
}

From source file:org.apache.drill.jdbc.test.JdbcAssert.java

static String toString(ResultSet resultSet, int expectedRecordCount) throws SQLException {
    StringBuilder buf = new StringBuilder();
    int total = 0, n;
    while (resultSet.next()) {
        n = resultSet.getMetaData().getColumnCount();
        total++;/*from   w  ww.j a v a  2 s  .c o m*/
        String sep = "";
        for (int i = 1; i <= n; i++) {
            buf.append(sep).append(resultSet.getMetaData().getColumnLabel(i)).append("=")
                    .append(resultSet.getObject(i));
            sep = "; ";
        }
        buf.append("\n");
    }
    return buf.toString();
}

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   www . j  a  v  a 2  s .c o 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:com.splicemachine.homeless.TestUtils.java

private static Map<String, Object> resultSetToOrderedMap(ResultSet rs) throws SQLException {
    Map<String, Object> result = new LinkedHashMap<>();
    ResultSetMetaData rsmd = rs.getMetaData();
    int cols = rsmd.getColumnCount();

    for (int i = 1; i <= cols; i++) {
        result.put(rsmd.getColumnName(i), rs.getObject(i));
    }/*from   w w  w  .j a v  a2s  . c o m*/

    return result;
}

From source file:com.viettel.ws.client.JDBCUtil.java

/**
 * Create xml string - fastest, but may have encoding issues
 *
 * @param rs - a Result set// w ww .j  a  va  2 s .  c  om
 * @return - XML string of a result set
 * @throws SQLException - If error when read data from database
 */
public static String toXML(ResultSet rs) throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int colCount = rsmd.getColumnCount();
    StringBuilder xml = new StringBuilder();
    xml.append("<Results>");

    while (rs.next()) {
        xml.append("<Row>");

        for (int i = 1; i <= colCount; i++) {
            String columnName = rsmd.getColumnName(i);
            Object value = rs.getObject(i);
            xml.append("<").append(columnName).append(">");

            if (value != null) {
                xml.append(value.toString().trim());
            }
            xml.append("</").append(columnName).append(">");
        }
        xml.append("</Row>");
    }

    xml.append("</Results>");

    return xml.toString();
}

From source file:mangotiger.sql.SQL.java

public static List<Object> list(final Connection connection, final String sql, final Object[] parameters)
        throws SQLException {
    ResultSet resultSet = null;
    PreparedStatement statement = null;
    try {//from  w  ww . ja v  a  2  s .c  om
        final List<Object> list = new ArrayList<Object>();
        statement = newPreparedStatement(connection, sql, parameters);
        resultSet = statement.executeQuery();
        final ResultSetMetaData metaData = resultSet.getMetaData();
        final int columnCount = metaData.getColumnCount();
        while (resultSet.next()) {
            switch (columnCount) {
            case 1:
                list.add(resultSet.getObject(1));
                break;
            default:
                final Object[] row = new Object[columnCount];
                for (int i = 0; i < columnCount; ++i) {
                    row[i] = resultSet.getObject(i + 1);
                }
                list.add(row);
                break;
            }
        }
        return list;
    } catch (SQLException e) {
        log().error("unable to execute: " + asString(sql, parameters), e);
        throw e;
    } finally {
        close(statement, resultSet);
    }
}

From source file:com.stratelia.webactiv.util.DBUtil.java

/**
 * Gets a long value from a current result set.
 * @param resultSet//w ww .j  a v a2  s .c  om
 * @param index
 * @return the long value if it exists, null otherwise.
 * @throws SQLException
 */
public static Long getLong(ResultSet resultSet, int index) throws SQLException {
    if (resultSet.getObject(index) != null) {
        return resultSet.getLong(index);
    }
    return null;
}

From source file:Main.java

public static String convertResultSetToXML(ResultSet rs)
        throws SQLException, ParserConfigurationException, TransformerException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int colCount = rsmd.getColumnCount();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element results = doc.createElement("Results");
    doc.appendChild(results);// w  w  w  . j  ava 2 s  .  c  o m

    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);
            if (value != null) {
                Element node = doc.createElement(columnName.replaceAll("\\(", "_").replaceAll("\\)", "_"));
                node.appendChild(doc.createTextNode(value.toString()));
                row.appendChild(node);
            }
        }
    }

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");

    return output;
}

From source file:at.alladin.rmbt.statisticServer.StatisticsResource.java

private static void fillJSON(final String lang, final ResultSet rs, final JSONArray providers)
        throws SQLException, JSONException {
    final ResultSetMetaData metaData = rs.getMetaData();
    final int columnCount = metaData.getColumnCount();
    while (rs.next()) {
        final JSONObject obj = new JSONObject();
        for (int j = 1; j <= columnCount; j++) {
            final String colName = metaData.getColumnName(j);
            Object data = rs.getObject(j);
            if (colName.equals("name") && data == null)
                if (lang != null && lang.equals("de"))
                    data = "Andere Betreiber";
                else
                    data = "Other operators";
            if (colName.equals("shortname") && data == null) {
                if (lang != null && lang.equals("de"))
                    data = "Andere";
                else
                    data = "Others";
            }/*from  ww w.ja  v a  2 s.  com*/
            obj.put(colName, data);
        }
        providers.put(obj);
    }
}

From source file:CSVWriter.java

private static String getColumnValue(ResultSet rs, int colType, int colIndex)
    throws SQLException, IOException {

  String value = "";
      //from w  w w . ja v  a2  s  . c  o m
switch (colType)
{
  case Types.BIT:
    Object bit = rs.getObject(colIndex);
    if (bit != null) {
      value = String.valueOf(bit);
    }
  break;
  case Types.BOOLEAN:
    boolean b = rs.getBoolean(colIndex);
    if (!rs.wasNull()) {
      value = Boolean.valueOf(b).toString();
    }
  break;
  case Types.CLOB:
    Clob c = rs.getClob(colIndex);
    if (c != null) {
      value = read(c);
    }
  break;
  case Types.BIGINT:
  case Types.DECIMAL:
  case Types.DOUBLE:
  case Types.FLOAT:
  case Types.REAL:
  case Types.NUMERIC:
    BigDecimal bd = rs.getBigDecimal(colIndex);
    if (bd != null) {
      value = "" + bd.doubleValue();
    }
  break;
  case Types.INTEGER:
  case Types.TINYINT:
  case Types.SMALLINT:
    int intValue = rs.getInt(colIndex);
    if (!rs.wasNull()) {
      value = "" + intValue;
    }
  break;
  case Types.JAVA_OBJECT:
    Object obj = rs.getObject(colIndex);
    if (obj != null) {
      value = String.valueOf(obj);
    }
  break;
  case Types.DATE:
    java.sql.Date date = rs.getDate(colIndex);
    if (date != null) {
      value = DATE_FORMATTER.format(date);;
    }
  break;
  case Types.TIME:
    Time t = rs.getTime(colIndex);
    if (t != null) {
      value = t.toString();
    }
  break;
  case Types.TIMESTAMP:
    Timestamp tstamp = rs.getTimestamp(colIndex);
    if (tstamp != null) {
      value = TIMESTAMP_FORMATTER.format(tstamp);
    }
  break;
  case Types.LONGVARCHAR:
  case Types.VARCHAR:
  case Types.CHAR:
    value = rs.getString(colIndex);
  break;
  default:
    value = "";
}

    
if (value == null)
{
  value = "";
}
    
return value;
      
}