Example usage for java.sql ResultSet getFloat

List of usage examples for java.sql ResultSet getFloat

Introduction

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

Prototype

float getFloat(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a float in the Java programming language.

Usage

From source file:com.cnd.greencube.server.dao.jdbc.JdbcDAO.java

@SuppressWarnings("rawtypes")
private Object getColumnValue(ResultSet rs, ResultSetMetaData meta, int index, Class clazz) throws Exception {
    Object value = null;//from ww  w  .j  a  v a  2s.c  o m

    int type = meta.getColumnType(index);
    if (clazz == String.class) {
        value = rs.getString(index);
    } else if (clazz == Integer.class) {
        value = rs.getInt(index);
    } else if (clazz == Boolean.class) {
        value = rs.getBoolean(index);
    } else if (clazz == byte[].class) {
        if (type == Types.BLOB)
            value = rs.getBlob(index);
        else
            value = rs.getBytes(index);
    } else if (clazz == Long.class) {
        value = rs.getLong(index);
    } else if (clazz == BigInteger.class) {
        value = rs.getBigDecimal(index);
    } else if (clazz == Float.class) {
        value = rs.getFloat(index);
    } else if (clazz == Double.class) {
        value = rs.getDouble(index);
    } else if (clazz == java.util.Date.class) {
        Timestamp time = rs.getTimestamp(index);
        if (time == null)
            value = null;
        else {
            value = new java.util.Date(time.getTime());
        }
    } else if (clazz == java.sql.Date.class) {
        value = rs.getDate(index);
    } else if (clazz == java.sql.Time.class) {
        value = rs.getTime(index);
    } else if (clazz == java.sql.Timestamp.class) {
        value = rs.getTimestamp(index);
    } else {
        throw new Exception("Cannote determin this column type:" + meta.getColumnName(index));
    }
    return value;
}

From source file:edu.caltechUcla.sselCassel.projects.jMarkets.server.data.DBWriter.java

public float getAvgTransactionPrice(int sessionId, int periodId, String securityName) {
    float avgPrice = -1;
    Connection conn = null;//from   w w w.  j  av  a 2  s  . c  o m
    Object[] results = null;
    try {
        conn = dbc.getConnection();
        StringBuffer query = new StringBuffer();
        query.append(
                "select avg(tb.price) from periods p, period_securities ps, securities s, security_pricelevels sl, transaction_book tb, transaction_sides ts, offer_book ob");
        query.append(" where p.session_id=").append(sessionId).append(" and p.period_id=").append(periodId)
                .append(" and s.security_name='").append(securityName).append("' and ");
        query.append(
                "ps.session_id=p.session_id and ps.period_id=p.period_id and security_id=s.id and sl.period_security_id=ps.id and sl.id=ob.pricelevel_id and ob.id=ts.offer_id and ts.transaction_id=tb.id group by s.security_name");

        results = dbc.executeQuery(query.toString(), conn);

        ResultSet rs = (ResultSet) results[0];
        if (rs.next())
            avgPrice = rs.getFloat(1);

    } catch (Exception e) {
        log.error("Error querying average transaction price... ", e);
    } finally {
        dbc.closeQuery(results, conn);
    }

    return avgPrice;
}

From source file:org.ut.biolab.medsavant.server.serverapi.ProjectManager.java

public Object[] getVariantTableViewInfo(String sid, int projectid, int refid)
        throws SQLException, SessionExpiredException {
    TableSchema table = MedSavantDatabase.VarianttablemapTableSchema;
    SelectQuery query = new SelectQuery();
    query.addFromTable(table.getTable());
    query.addColumns(table.getDBColumn(VariantTablemapTableSchema.COLUMNNAME_OF_VARIANT_SUBSET_TABLENAME),
            table.getDBColumn(VariantTablemapTableSchema.COLUMNNAME_OF_SUBSET_MULTIPLIER));
    query.addCondition(BinaryConditionMS
            .equalTo(table.getDBColumn(VariantTablemapTableSchema.COLUMNNAME_OF_PROJECT_ID), projectid));
    query.addCondition(BinaryConditionMS
            .equalTo(table.getDBColumn(VariantTablemapTableSchema.COLUMNNAME_OF_REFERENCE_ID), refid));
    query.addCondition(BinaryConditionMS
            .equalTo(table.getDBColumn(VariantTablemapTableSchema.COLUMNNAME_OF_PUBLISHED), true));

    query.addOrdering(table.getDBColumn(VariantTablemapTableSchema.COLUMNNAME_OF_UPDATE_ID), Dir.DESCENDING);

    ResultSet rs = ConnectionController.executeQuery(sid, query.toString());

    if (rs.next()) {
        //If the subset table doesn't exist, then the subset view cannot exist either.
        rs.getString(1);/* w  w  w . ja  v  a 2 s  .c  om*/
        String subsetViewName = rs.wasNull() ? null : DBSettings.getVariantSubsetViewName(projectid, refid);

        return new Object[] { DBSettings.getVariantViewName(projectid, refid), subsetViewName, rs.getFloat(2) };
    } else {
        return null;
    }
}

From source file:se.technipelago.weather.chart.Generator.java

private float getSolarHours(final Timespan period) {
    PreparedStatement stmt = null;
    ResultSet result = null;
    float f = 0f;

    init();//  w w w .j  a v  a2  s.  c o  m

    try {
        stmt = conn.prepareStatement(
                "SELECT COUNT(*) / 6 AS hours FROM archive WHERE solar > 120 AND ts BETWEEN ? AND ?");
        stmt.setTimestamp(1, new java.sql.Timestamp(period.getStartTime().getTime()));
        stmt.setTimestamp(2, new java.sql.Timestamp(period.getEndTime().getTime()));
        result = stmt.executeQuery();
        if (result.next()) {
            f = result.getFloat(1);
        }
    } catch (SQLException ex) {
        log.log(Level.SEVERE, null, ex);
    } finally {
        if (result != null) {
            try {
                result.close();
            } catch (SQLException ex) {
                log.log(Level.WARNING, "Failed to close ResultSet", ex);
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException ex) {
                log.log(Level.WARNING, "Failed to close select statement", ex);
            }
        }
    }
    return f;
}

From source file:Reportes.Reporte.java

public ChartPanel generarReporteAportePorSede() {
    String consultaSQL = "SELECT nombre_sede, SUM(valor_vehiculo) FROM ventas_vehiculos NATURAL JOIN vehiculos NATURAL JOIN sedes GROUP BY nombre_sede;";
    String[] nombreSedes;//from w  ww . j av a 2 s.  c o  m
    float[] valorVentas;
    int filas = 0;

    ResultSet tabla = new OperacionesBD().consultas(consultaSQL);
    reporte = APORTE_SEDE;
    generarReporte(tabla);
    tabla = new OperacionesBD().consultas(consultaSQL);
    try {
        while (tabla.next()) {
            filas++;
        }
    } catch (SQLException ex) {
        ex.printStackTrace();
    }

    nombreSedes = new String[filas];
    valorVentas = new float[filas];

    tabla = new OperacionesBD().consultas(consultaSQL);
    filas = 0;
    try {
        while (tabla.next()) {
            nombreSedes[filas] = tabla.getString(1);
            valorVentas[filas] = tabla.getFloat(2);
            filas++;
        }
    } catch (SQLException ex) {
        ex.printStackTrace();
    }

    DefaultCategoryDataset data = new DefaultCategoryDataset();
    for (int i = 0; i < nombreSedes.length; i++) {
        data.setValue((double) valorVentas[i], "Aporte por sede", nombreSedes[i]);
    }

    consultaSQL = "SELECT nombre_sede FROM sedes EXCEPT SELECT nombre_sede FROM ventas_vehiculos NATURAL JOIN vehiculos NATURAL JOIN sedes GROUP BY nombre_sede;";
    tabla = new OperacionesBD().consultas(consultaSQL);
    try {
        while (tabla.next()) {
            data.setValue(0.0, "Aporte por sede", tabla.getString(1));
        }
    } catch (SQLException ex) {
        ex.printStackTrace();
    }

    ChartPanel panel = new BarChart().reporteAportesVentas(data);

    return panel;
}

From source file:dsd.dao.ParametersDAO.java

public static List<Parameter> GetParameterHistory(long parameterID) {
    try {/*from   w ww  .jav  a  2  s  .  co m*/
        Connection con = DAOProvider.getDataSource().getConnection();
        ArrayList<Parameter> parametersList = new ArrayList<Parameter>();
        try {
            Object[] parameters = new Object[1];
            parameters[0] = new Long(parameterID);

            ResultSet results = DAOProvider.SelectTableSecure(
                    // table part
                    tableNameParameters + " join " + tableNameParameterData + " on " + tableNameParameters
                            + ".ID = " + tableNameParameterData + "." + tableParameterDataFields[0],
                    // select part
                    tableNameParameterData + "." + "ID" + " as " + tableNameParameterData + "_" + "ID" + ", "
                            + tableNameParameterData + "." + tableParameterDataFields[0] + " as "
                            + tableNameParameterData + "_" + tableParameterDataFields[0] + ", "
                            + tableNameParameterData + "." + tableParameterDataFields[1] + " as "
                            + tableNameParameterData + "_" + tableParameterDataFields[1] + ", "
                            + tableNameParameterData + "." + tableParameterDataFields[2] + " as "
                            + tableNameParameterData + "_" + tableParameterDataFields[2] + ", "
                            + tableNameParameterData + "." + tableParameterDataFields[3] + " as "
                            + tableNameParameterData + "_" + tableParameterDataFields[3] + ", "
                            + "parameters.* ",
                    // where part
                    " parameters_id = ? ",
                    // order by part
                    "", con, parameters);
            while (results.next()) {
                Parameter parameter = new Parameter();
                parameter.setParameterID(results.getLong("ID"));
                parameter.setAbbreviation(results.getString(tableParametersFields[1]));
                parameter.setCategory(
                        eParameterCategory.getParameterCategory(results.getInt(tableParametersFields[4])));
                parameter.setName(results.getString(tableParametersFields[0]));
                parameter.setParameterDataID(results.getLong(tableNameParameterData + "_ID"));
                parameter.setTimestamp(results
                        .getTimestamp(tableNameParameterData + "_" + tableParameterDataFields[3]).getTime());
                parameter.setUnit(results.getString(tableParametersFields[2]));
                parameter.setUserID(results.getInt(tableNameParameterData + "_" + tableParameterDataFields[2]));
                parameter
                        .setValue(results.getFloat(tableNameParameterData + "_" + tableParameterDataFields[1]));
                parametersList.add(parameter);
            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
        con.close();
        return parametersList;
    } catch (Exception exc) {
        exc.printStackTrace();
    }
    return null;
}

From source file:com.nridge.core.ds.rdbms.hsqldb.HDBSQLTable.java

private void addTableRowFromFunctionResultSet(DataTable aTable, ResultSet aResultSet) {
    DataField dataField;/*from   w ww.j a  v  a2  s  .  c o  m*/
    Logger appLogger = mAppMgr.getLogger(this, "addTableRowFromFunctionResultSet");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    FieldRow fieldRow = aTable.newRow();

    int columnNumber = 0;
    for (DataField pField : aTable.getColumnBag().getFields()) {
        columnNumber++;
        dataField = new DataField(pField);

        try {
            switch (pField.getType()) {
            case Integer:
                dataField.setValue(aResultSet.getInt(columnNumber));
                break;
            case Long:
                dataField.setValue(aResultSet.getLong(columnNumber));
                break;
            case Float:
                dataField.setValue(aResultSet.getFloat(columnNumber));
                break;
            case Double:
                dataField.setValue(aResultSet.getDouble(columnNumber));
                break;
            case Boolean:
                dataField.setValue(aResultSet.getBoolean(columnNumber));
                break;
            case Date:
                dataField.setValue(aResultSet.getDate(columnNumber));
                break;
            case Time:
                dataField.setValue(aResultSet.getTime(columnNumber));
                break;
            case DateTime:
                dataField.setValue(aResultSet.getTimestamp(columnNumber));
                break;
            default:
                dataField.setValue(aResultSet.getString(columnNumber));
                break;
            }

            if (!aResultSet.wasNull())
                aTable.setValueByName(fieldRow, pField.getName(), dataField.getValue());
        } catch (SQLException e) {
            appLogger.error(String.format("SQL Exception (%s): %s", pField.getName(), e.getMessage()));
        }
    }

    aTable.addRow(fieldRow);

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);
}

From source file:org.trafodion.rest.util.JdbcT2Util.java

private synchronized JSONArray convertResultSetToJSON(java.sql.ResultSet rs) throws Exception {
    if (LOG.isDebugEnabled())
        LOG.debug("Begin convertResultSetToJSON");

    JSONArray json = new JSONArray();

    try {// w  ww  .j a va  2  s. c  o  m

        java.sql.ResultSetMetaData rsmd = rs.getMetaData();

        while (rs.next()) {
            int numColumns = rsmd.getColumnCount();
            JSONObject obj = new JSONObject();

            for (int i = 1; i < numColumns + 1; i++) {

                String column_name = rsmd.getColumnName(i);

                if (rsmd.getColumnType(i) == java.sql.Types.ARRAY) {
                    obj.put(column_name, rs.getArray(column_name));
                } else if (rsmd.getColumnType(i) == java.sql.Types.BIGINT) {
                    obj.put(column_name, rs.getLong(column_name));
                } else if (rsmd.getColumnType(i) == java.sql.Types.BOOLEAN) {
                    obj.put(column_name, rs.getBoolean(column_name));
                } else if (rsmd.getColumnType(i) == java.sql.Types.BLOB) {
                    obj.put(column_name, rs.getBlob(column_name));
                } else if (rsmd.getColumnType(i) == java.sql.Types.DOUBLE) {
                    obj.put(column_name, rs.getDouble(column_name));
                } else if (rsmd.getColumnType(i) == java.sql.Types.FLOAT) {
                    obj.put(column_name, rs.getFloat(column_name));
                } else if (rsmd.getColumnType(i) == java.sql.Types.INTEGER) {
                    obj.put(column_name, rs.getInt(column_name));
                } else if (rsmd.getColumnType(i) == java.sql.Types.NVARCHAR) {
                    obj.put(column_name, rs.getNString(column_name));
                } else if (rsmd.getColumnType(i) == java.sql.Types.CHAR
                        || rsmd.getColumnType(i) == java.sql.Types.VARCHAR) {
                    //prevent obj.put from removing null key value from JSONObject
                    String s = rs.getString(column_name);
                    if (s == null)
                        obj.put(column_name, new String(""));
                    else
                        obj.put(column_name, rs.getString(column_name));
                } else if (rsmd.getColumnType(i) == java.sql.Types.TINYINT) {
                    obj.put(column_name, rs.getInt(column_name));
                } else if (rsmd.getColumnType(i) == java.sql.Types.SMALLINT) {
                    obj.put(column_name, rs.getInt(column_name));
                } else if (rsmd.getColumnType(i) == java.sql.Types.DATE) {
                    obj.put(column_name, rs.getDate(column_name));
                } else if (rsmd.getColumnType(i) == java.sql.Types.TIMESTAMP) {
                    obj.put(column_name, rs.getTimestamp(column_name));
                } else {
                    obj.put(column_name, rs.getObject(column_name));
                }

            } //end foreach 
            json.put(obj);

        } //end while 

    } catch (SQLException e) {
        e.printStackTrace();
        if (LOG.isDebugEnabled())
            LOG.error(e.getMessage());
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        if (LOG.isDebugEnabled())
            LOG.error(e.getMessage());
        throw e;
    }

    if (LOG.isDebugEnabled())
        LOG.debug("End convertResultSetToJSON");

    return json;
}

From source file:com.nridge.core.ds.rdbms.hsqldb.HDBSQLTable.java

private void addTableRowFromResultSet(DataTable aTable, ResultSet aResultSet) {
    String columnName;//  w  w  w  .  j a v a  2 s . c om
    DataField dataField;
    Logger appLogger = mAppMgr.getLogger(this, "addTableRowFromResultSet");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    FieldRow fieldRow = aTable.newRow();

    for (DataField pField : aTable.getColumnBag().getFields()) {
        dataField = new DataField(pField);

        try {
            columnName = columnName(pField.getName());

            switch (pField.getType()) {
            case Integer:
                dataField.setValue(aResultSet.getInt(columnName));
                break;
            case Long:
                dataField.setValue(aResultSet.getLong(columnName));
                break;
            case Float:
                dataField.setValue(aResultSet.getFloat(columnName));
                break;
            case Double:
                dataField.setValue(aResultSet.getDouble(columnName));
                break;
            case Boolean:
                dataField.setValue(aResultSet.getBoolean(columnName));
                break;
            case Date:
                dataField.setValue(aResultSet.getDate(columnName));
                break;
            case Time:
                dataField.setValue(aResultSet.getTime(columnName));
                break;
            case DateTime:
                dataField.setValue(aResultSet.getTimestamp(columnName));
                break;
            default:
                dataField.setValue(aResultSet.getString(columnName));
                break;
            }

            if (!aResultSet.wasNull())
                aTable.setValueByName(fieldRow, pField.getName(), dataField.getValue());
        } catch (SQLException e) {
            appLogger.error(String.format("SQL Exception (%s): %s", pField.getName(), e.getMessage()));
        } catch (NSException e) {
            appLogger.error(String.format("NS Exception (%s): %s", pField.getName(), e.getMessage()));
        }
    }

    aTable.addRow(fieldRow);

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);
}

From source file:com.splicemachine.derby.impl.load.HdfsImportIT.java

@Test
public void testImportNullFields() throws Exception {
    PreparedStatement ps = methodWatcher.prepareStatement(format("call SYSCS_UTIL.IMPORT_DATA(" + "'%s'," + // schema name
            "'%s'," + // table name
            "null," + // insert column list
            "'%s'," + // file path
            "','," + // column delimiter
            "'%s'," + // character delimiter
            "null," + // timestamp format
            "null," + // date format
            "null," + // time format
            "%d," + // max bad records
            "'%s'," + // bad record dir
            "null," + // has one line records
            "null)", // char set
            spliceSchemaWatcher.schemaName, TABLE_10, getResourceDirectory() + "null_field.csv", "\"", 0,
            BADDIR.getCanonicalPath()));
    ps.execute();//ww w . j  a v  a  2 s. c  o  m

    ResultSet rs = methodWatcher
            .executeQuery(format("select * from %s.%s", spliceSchemaWatcher.schemaName, TABLE_10));
    int count = 0;
    while (rs.next()) {
        Integer i = rs.getInt(1);
        Float j = rs.getFloat(2);
        String k = rs.getString(3);
        Timestamp l = rs.getTimestamp(4);
        Assert.assertEquals(i.byteValue(), 0);
        Assert.assertEquals(j.byteValue(), 0);
        Assert.assertNull("String failure " + k, k);
        Assert.assertNull("Timestamp failure " + l, l);
        count++;
    }
    Assert.assertTrue("import failed!" + count, count == 1);
}