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:fr.eo.util.dumper.Dumper.java

private static String getFieldValue(RequestDefinitionBean request, int pos, ResultSet rs, String fieldName)
        throws SQLException {
    String str = String.valueOf(rs.getObject(fieldName)).trim();
    if (str.endsWith(".0")) {
        str = str.substring(0, str.length() - 2);
    }// ww  w .  ja va2 s . c  om
    if (str.equals("null") || str.isEmpty()) {
        return "NULL";
    }
    if (request.compressedStrPos.contains(pos)) {
        str = getCompressedString(str);
    } else if (request.numberPos.contains(pos)) {
        Double number = rs.getDouble(fieldName);
        str = nf.format(number);
    } else if (request.fieldStrPos.contains(pos)) {
        str = formatString(str);
    }

    return str;
}

From source file:at.molindo.dbcopy.util.Utils.java

public static void printRow(ResultSet pk) throws SQLException {
    System.out.println("--- " + pk.getRow() + ". row ---");
    for (int i = 1; i <= pk.getMetaData().getColumnCount(); i++) {
        System.out.println(pk.getMetaData().getColumnName(i) + ": " + pk.getObject(i));
    }/*www. j ava  2 s .  c o  m*/
}

From source file:com.egt.core.db.util.SqlAgent.java

private static SqlAgentMessage executeProcedure(String procedimiento, Long rastro, Object[] args,
        boolean logging) {
    Bitacora.trace(SqlAgent.class, "executeProcedure", procedimiento, rastro);
    Utils.traceObjectArray(args);/*from  w ww  . j a  v a 2 s.  c o  m*/
    String procedure = StringUtils.trimToEmpty(procedimiento);
    //      String archivo = logging ? getLogFileName(rastro) : null;
    String archivo = null;
    EnumCondicionEjeFun condicion = EnumCondicionEjeFun.EJECUCION_EN_PROGRESO;
    String mensaje = TLC.getBitacora().info(CBM2.PROCESS_EXECUTION_BEGIN, procedure);
    boolean ok = Auditor.grabarRastroProceso(rastro, condicion, archivo, mensaje);
    if (ok) {
        try {
            // <editor-fold defaultstate="collapsed">
            //              Object resultado = TLC.getAgenteSql().executeProcedure(procedure, args);
            //              if (resultado == null) {
            //                  condicion = EnumCondicionEjeFun.EJECUTADO_CON_ERRORES;
            //                  mensaje = TLC.getBitacora().error(CBM2.PROCESS_EXECUTION_ABEND, procedure);
            //              } else {
            //                  condicion = EnumCondicionEjeFun.EJECUTADO_SIN_ERRORES;
            //                  if (resultado instanceof Number) {
            //                      mensaje = TLC.getBitacora().info(procedure + " = " + resultado);
            //                  } else if (resultado instanceof ResultSet) {
            //                      ResultSet resultSet = (ResultSet) resultado;
            //                      if (resultSet.next()) {
            //                          mensaje = TLC.getBitacora().info(procedure + " = " + resultSet.getObject(1));
            //                      } else {
            //                          mensaje = TLC.getBitacora().info(procedure + " = " + STP.STRING_VALOR_NULO);
            //                      }
            //                  } else {
            //                      mensaje = TLC.getBitacora().warn(CBM2.PROCESS_EXECUTION_END, procedure);
            //                  }
            //              }
            // </editor-fold>
            Object resultado = TLC.getAgenteSql().executeProcedure(procedure, args);
            if (resultado == null) {
                condicion = EnumCondicionEjeFun.EJECUTADO_CON_ERRORES;
                mensaje = TLC.getBitacora().error(CBM2.PROCESS_EXECUTION_ABEND, procedure);
            } else {
                condicion = EnumCondicionEjeFun.EJECUTADO_SIN_ERRORES;
                mensaje = TLC.getBitacora().info(CBM2.PROCESS_EXECUTION_END, procedure);
                if (resultado instanceof ResultSet) {
                    ResultSet resultSet = (ResultSet) resultado;
                    if (resultSet.next()) {
                        mensaje += " (" + resultSet.getObject(1) + ") ";
                    }
                } else if (resultado.getClass().isPrimitive()) {
                    mensaje += " (" + resultado + ") ";
                }
            }
        } catch (Exception ex) {
            condicion = EnumCondicionEjeFun.EJECUTADO_CON_ERRORES;
            mensaje = ThrowableUtils.getString(ex);
            TLC.getBitacora().fatal(ex);
            TLC.getBitacora().fatal(CBM2.PROCESS_EXECUTION_ABEND, procedure);
        } finally {
            Auditor.grabarRastroProceso(rastro, condicion, archivo, mensaje);
            //              DB.close(resultSet);
        }
    } else {
        condicion = EnumCondicionEjeFun.EJECUCION_CANCELADA;
        mensaje = TLC.getBitacora().error(CBM2.PROCESS_EXECUTION_ABEND, procedure);
    }
    SqlAgentMessage message = new SqlAgentMessage(procedure);
    message.setArgs(args);
    message.setRastro(rastro);
    message.setCondicion(condicion);
    message.setArchivo(archivo);
    message.setMensaje(mensaje);
    return message;
}

From source file:ips1ap101.lib.core.db.util.SqlAgent.java

private static SqlAgentMessage executeProcedure(String procedimiento, Long rastro, Object[] args,
        boolean logging) {
    Bitacora.trace(SqlAgent.class, "executeProcedure", procedimiento, rastro);
    Utils.traceObjectArray(args);/*ww w .  j  a va  2  s  . c om*/
    String procedure = StringUtils.trimToEmpty(procedimiento);
    //      String archivo = logging ? getLogFileName(rastro) : null;
    String archivo = null;
    CondicionEjeFunEnumeration condicion = CondicionEjeFunEnumeration.EJECUCION_EN_PROGRESO;
    String mensaje = TLC.getBitacora().info(CBM.PROCESS_EXECUTION_BEGIN, procedure);
    boolean ok = Auditor.grabarRastroProceso(rastro, condicion, archivo, mensaje);
    if (ok) {
        try {
            // <editor-fold defaultstate="collapsed">
            //              Object resultado = TLC.getAgenteSql().executeProcedure(procedure, args);
            //              if (resultado == null) {
            //                  condicion = CondicionEjeFunEnumeration.EJECUTADO_CON_ERRORES;
            //                  mensaje = TLC.getBitacora().error(CBM.PROCESS_EXECUTION_ABEND, procedure);
            //              } else {
            //                  condicion = CondicionEjeFunEnumeration.EJECUTADO_SIN_ERRORES;
            //                  if (resultado instanceof Number) {
            //                      mensaje = TLC.getBitacora().info(procedure + " = " + resultado);
            //                  } else if (resultado instanceof ResultSet) {
            //                      ResultSet resultSet = (ResultSet) resultado;
            //                      if (resultSet.next()) {
            //                          mensaje = TLC.getBitacora().info(procedure + " = " + resultSet.getObject(1));
            //                      } else {
            //                          mensaje = TLC.getBitacora().info(procedure + " = " + STP.STRING_VALOR_NULO);
            //                      }
            //                  } else {
            //                      mensaje = TLC.getBitacora().info(CBM.PROCESS_EXECUTION_END, procedure);
            //                  }
            //              }
            // </editor-fold>
            Object resultado = TLC.getAgenteSql().executeProcedure(procedure, args);
            if (resultado == null) {
                condicion = CondicionEjeFunEnumeration.EJECUTADO_CON_ERRORES;
                mensaje = TLC.getBitacora().error(CBM.PROCESS_EXECUTION_ABEND, procedure);
            } else {
                condicion = CondicionEjeFunEnumeration.EJECUTADO_SIN_ERRORES;
                mensaje = TLC.getBitacora().info(CBM.PROCESS_EXECUTION_END, procedure);
                if (resultado instanceof ResultSet) {
                    ResultSet resultSet = (ResultSet) resultado;
                    if (resultSet.next()) {
                        mensaje += " (" + resultSet.getObject(1) + ") ";
                    }
                } else if (resultado.getClass().isPrimitive()) {
                    mensaje += " (" + resultado + ") ";
                }
            }
        } catch (Exception ex) {
            condicion = CondicionEjeFunEnumeration.EJECUTADO_CON_ERRORES;
            mensaje = ThrowableUtils.getString(ex);
            TLC.getBitacora().fatal(ex);
            TLC.getBitacora().fatal(CBM.PROCESS_EXECUTION_ABEND, procedure);
        } finally {
            Auditor.grabarRastroProceso(rastro, condicion, archivo, mensaje);
            //              DB.close(resultSet);
        }
    } else {
        condicion = CondicionEjeFunEnumeration.EJECUCION_CANCELADA;
        mensaje = TLC.getBitacora().error(CBM.PROCESS_EXECUTION_ABEND, procedure);
    }
    SqlAgentMessage message = new SqlAgentMessage(procedure);
    message.setArgumentos(args);
    message.setRastro(rastro);
    message.setCondicion(condicion);
    message.setArchivo(archivo);
    message.setMensaje(mensaje);
    return message;
}

From source file:com.alibaba.otter.node.etl.common.db.utils.SqlUtils.java

/**
 * Retrieve a JDBC column value from a ResultSet, using the most appropriate
 * value type. The returned value should be a detached value object, not
 * having any ties to the active ResultSet: in particular, it should not be
 * a Blob or Clob object but rather a byte array respectively String
 * representation.//w  ww .ja va  2 s .c o m
 * <p>
 * Uses the <code>getObject(index)</code> method, but includes additional
 * "hacks" to get around Oracle 10g returning a non-standard object for its
 * TIMESTAMP datatype and a <code>java.sql.Date</code> for DATE columns
 * leaving out the time portion: These columns will explicitly be extracted
 * as standard <code>java.sql.Timestamp</code> object.
 * 
 * @param rs is the ResultSet holding the data
 * @param index is the column index
 * @return the value object
 * @throws SQLException if thrown by the JDBC API
 * @see java.sql.Blob
 * @see java.sql.Clob
 * @see java.sql.Timestamp
 */
private static String getResultSetValue(ResultSet rs, int index) throws SQLException {
    Object obj = rs.getObject(index);
    return (obj == null) ? null : convertUtilsBean.convert(obj);
}

From source file:com.github.mgramin.liwide_rest.util.Convertor.java

public static JSONArray convertToJSON(ResultSet resultSet) throws Exception {
    JSONArray jsonArray = new JSONArray();
    while (resultSet.next()) {
        int total_rows = resultSet.getMetaData().getColumnCount();
        JSONObject obj = new JSONObject();
        for (int i = 0; i < total_rows; i++) {
            obj.put(resultSet.getMetaData().getColumnLabel(i + 1).toLowerCase(), resultSet.getObject(i + 1));
        }//  ww  w . j  a  va 2  s  .co m
        jsonArray.put(obj);
    }
    return jsonArray;
}

From source file:com.mum.edu.cs472.dictServlet.java

public static JSONArray convertToJSON(ResultSet resultSet) throws Exception {
    JSONArray jsonArray = new JSONArray();
    while (resultSet.next()) {
        int total_rows = resultSet.getMetaData().getColumnCount();
        JSONObject obj = new JSONObject();
        for (int i = 0; i < total_rows; i++) {
            obj.put(resultSet.getMetaData().getColumnLabel(i + 1).toLowerCase(), resultSet.getObject(i + 1));
            jsonArray.add(obj);/*from  www . j ava 2s .c  o m*/
        }
    }
    return jsonArray;
}

From source file:cn.clickvalue.cv2.model.rowmapper.BeanPropertyRowMapper.java

/**
 * Retrieve a JDBC column value from a ResultSet, using the most appropriate
 * value type. The returned value should be a detached value object, not having
 * any ties to the active ResultSet: in particular, it should not be a Blob or
 * Clob object but rather a byte array respectively String representation.
 * <p>Uses the <code>getObject(index)</code> method, but includes additional "hacks"
 * to get around Oracle 10g returning a non-standard object for its TIMESTAMP
 * datatype and a <code>java.sql.Date</code> for DATE columns leaving out the
 * time portion: These columns will explicitly be extracted as standard
 * <code>java.sql.Timestamp</code> object.
 * @param rs is the ResultSet holding the data
 * @param index is the column index//from  w  w w . j a  v a  2 s  . c  om
 * @return the value object
 * @throws SQLException if thrown by the JDBC API
 * @see java.sql.Blob
 * @see java.sql.Clob
 * @see java.sql.Timestamp
 */
public static Object getResultSetValue(ResultSet rs, int index) throws SQLException {
    Object obj = rs.getObject(index);
    if (obj instanceof Blob) {
        obj = rs.getBytes(index);
    } else if (obj instanceof Clob) {
        obj = rs.getString(index);
    } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.TIMESTAMP")) {
        obj = rs.getTimestamp(index);
    } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.DATE")) {
        String metaDataClassName = rs.getMetaData().getColumnClassName(index);
        if ("java.sql.Timestamp".equals(metaDataClassName)
                || "oracle.sql.TIMESTAMP".equals(metaDataClassName)) {
            obj = rs.getTimestamp(index);
        } else {
            obj = rs.getDate(index);
        }
    } else if (obj != null && obj instanceof java.sql.Date) {
        if ("java.sql.Timestamp".equals(rs.getMetaData().getColumnClassName(index))) {
            obj = rs.getTimestamp(index);
        }
    }
    return obj;
}

From source file:com.espertech.esperio.db.SupportDatabaseService.java

public static Object[][] readAll(String tableName) throws SQLException {
    Connection connection = getConnection(PARTURL, DBUSER, DBPWD);
    connection.setAutoCommit(true);//from w w  w .j  a  v a  2 s  .co  m
    Statement stmt = connection.createStatement();
    String sql = "select * from " + tableName;
    log.info("Executing sql : " + sql);
    ResultSet resultSet = stmt.executeQuery(sql);

    List<Object[]> rows = new ArrayList<Object[]>();
    while (resultSet.next()) {
        List<Object> row = new ArrayList<Object>();
        for (int i = 0; i < resultSet.getMetaData().getColumnCount(); i++) {
            row.add(resultSet.getObject(i + 1));
        }
        rows.add(row.toArray());
    }

    Object[][] arr = new Object[rows.size()][];
    for (int i = 0; i < rows.size(); i++) {
        arr[i] = rows.get(i);
    }

    stmt.close();
    connection.close();
    return arr;
}

From source file:com.espertech.esperio.db.core.DBUtil.java

/**
 * Returns the object value for a given column and type.
 * @param rs result set// w  w  w  . j a  va2s  .com
 * @param index column index
 * @param valueType value type
 * @return object value
 * @throws java.sql.SQLException if the column could not be read
 */
public static Object getValue(ResultSet rs, int index, int valueType) throws SQLException {
    if (valueType == Types.INTEGER) {
        return rs.getInt(index);
    } else if (valueType == Types.BIGINT) {
        return rs.getLong(index);
    } else if (valueType == Types.BLOB) {
        Blob blob = rs.getBlob(index);
        return getBlobValue(blob);
    }
    return rs.getObject(index);
}