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:InsertRows.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;//from  w  ww  .  ja va  2 s  .  c  om
    Statement stmt;
    try {
        Class.forName("myDriver.ClassName");

    } catch (java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }

    try {

        con = DriverManager.getConnection(url, "myLogin", "myPassword");

        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

        ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES");

        uprs.moveToInsertRow();

        uprs.updateString("COF_NAME", "Kona");
        uprs.updateInt("SUP_ID", 150);
        uprs.updateFloat("PRICE", 10.99f);
        uprs.updateInt("SALES", 0);
        uprs.updateInt("TOTAL", 0);

        uprs.insertRow();

        uprs.updateString("COF_NAME", "Kona_Decaf");
        uprs.updateInt("SUP_ID", 150);
        uprs.updateFloat("PRICE", 11.99f);
        uprs.updateInt("SALES", 0);
        uprs.updateInt("TOTAL", 0);

        uprs.insertRow();

        uprs.beforeFirst();

        System.out.println("Table COFFEES after insertion:");
        while (uprs.next()) {
            String name = uprs.getString("COF_NAME");
            int id = uprs.getInt("SUP_ID");
            float price = uprs.getFloat("PRICE");
            int sales = uprs.getInt("SALES");
            int total = uprs.getInt("TOTAL");
            System.out.print(name + "   " + id + "   " + price);
            System.out.println("   " + sales + "   " + total);
        }

        uprs.close();
        stmt.close();
        con.close();

    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}

From source file:com.intelius.iap4.TigerLineHit.java

public static void main(String[] args) {

    String _tigerDs = "jdbc:h2:/home/sxu/playground/tiger";
    ResultSet rs = null;
    PreparedStatement ps = null;//from   w ww . j  a v  a  2s .  c o m
    List<TigerLineHit> ret = new ArrayList<TigerLineHit>();
    try {
        //      if (_tigerDs instanceof JdbcDataSource) {
        //        JdbcDataSource ds = (JdbcDataSource) _tigerDs;
        //        conn = ds.getPooledConnection().getConnection();
        //      }else{
        //        conn = _tigerDs.getConnection();
        //      }

        //try address "540 westerly parkway, state college, pa 16801"

        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection(_tigerDs, "sa", "");
        ps = conn.prepareStatement(generateSelectQuery("PA"));
        int i = 1;
        String streetNum = "540";
        String zip = "16801";
        ps.setString(i++, "Westerly");
        ps.setString(i++, streetNum);
        ps.setString(i++, streetNum);
        ps.setString(i++, streetNum);
        ps.setString(i++, streetNum);
        ps.setString(i++, streetNum);
        ps.setString(i++, streetNum);
        ps.setString(i++, streetNum);
        ps.setString(i++, streetNum);
        ps.setString(i++, zip);
        ps.setString(i++, zip);
        rs = ps.executeQuery();
        while (rs.next()) {
            TigerLineHit hit = new TigerLineHit();
            hit.streetNum = streetNum;
            hit.tlid = rs.getLong("tlid");
            hit.frAddL = rs.getString("fraddl");
            hit.frAddR = rs.getString("fraddr");
            hit.toAddL = rs.getString("toaddl");
            hit.toAddR = rs.getString("toaddr");
            hit.zipL = rs.getString("zipL");
            hit.zipR = rs.getString("zipR");
            hit.toLat = rs.getFloat("tolat");
            hit.toLon = rs.getFloat("tolong");
            hit.frLat = rs.getFloat("frlat");
            hit.frLon = rs.getFloat("tolong");
            hit.lat1 = rs.getFloat("lat1");
            hit.lat2 = rs.getFloat("lat2");
            hit.lat3 = rs.getFloat("lat3");
            hit.lat4 = rs.getFloat("lat4");
            hit.lat5 = rs.getFloat("lat5");
            hit.lat6 = rs.getFloat("lat6");
            hit.lat7 = rs.getFloat("lat7");
            hit.lat8 = rs.getFloat("lat8");
            hit.lat9 = rs.getFloat("lat9");
            hit.lat10 = rs.getFloat("lat10");
            hit.lon1 = rs.getFloat("long1");
            hit.lon2 = rs.getFloat("long2");
            hit.lon3 = rs.getFloat("long3");
            hit.lon4 = rs.getFloat("long4");
            hit.lon5 = rs.getFloat("long5");
            hit.lon6 = rs.getFloat("long6");
            hit.lon7 = rs.getFloat("long7");
            hit.lon8 = rs.getFloat("long8");
            hit.lon9 = rs.getFloat("long9");
            hit.lon10 = rs.getFloat("long10");
            hit.fedirp = rs.getString("fedirp");
            hit.fetype = rs.getString("fetype");
            hit.fedirs = rs.getString("fedirs");
            ret.add(hit);

            //            
            System.out.println(ret.toString());
            //
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        //DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(ps);
    }
    //return ret;
}

From source file:dsd.dao.RawDataDAO.java

public static ArrayList<RawData> GetAllForPeriod(Calendar startDate, Calendar endDate) {
    try {// w w  w  .ja  va  2s. co m
        Connection con = DAOProvider.getDataSource().getConnection();
        ArrayList<RawData> rawDataList = new ArrayList<RawData>();
        try {
            Object[] parameters = new Object[2];
            parameters[0] = new Timestamp(startDate.getTimeInMillis());
            parameters[1] = new Timestamp(endDate.getTimeInMillis());
            ResultSet results = DAOProvider.SelectTableSecure(tableName, "*",
                    " timestamp >= ? and timestamp <= ? ", "", con, parameters);
            while (results.next()) {
                RawData dataTuple = new RawData();
                dataTuple.setRawDataID(results.getInt("ID"));
                dataTuple.setWindSpeed(results.getFloat(fields[0]));
                dataTuple.setWindDirection(results.getFloat(fields[1]));
                dataTuple.setHydrometer(results.getFloat(fields[2]));
                dataTuple.setSonar(results.getFloat(fields[3]));
                dataTuple.setSonarType(eSonarType.getSonarType(results.getInt(fields[4])));
                dataTuple.setTimestamp(results.getTimestamp(fields[5]).getTime());
                rawDataList.add(dataTuple);
            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
        con.close();
        return rawDataList;
    } catch (Exception exc) {
        exc.printStackTrace();
    }
    return null;

}

From source file:com.oracle.tutorial.jdbc.CoffeesTable.java

public static void alternateViewTable(Connection con) throws SQLException {
    Statement stmt = null;//w  w  w  .ja v  a2  s  .com
    String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES";
    try {
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String coffeeName = rs.getString(1);
            int supplierID = rs.getInt(2);
            float price = rs.getFloat(3);
            int sales = rs.getInt(4);
            int total = rs.getInt(5);
            System.out.println(coffeeName + ", " + supplierID + ", " + price + ", " + sales + ", " + total);
        }
    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
}

From source file:com.oracle.tutorial.jdbc.CoffeesTable.java

public static void viewTable(Connection con) throws SQLException {
    Statement stmt = null;//from w  w  w .j a  v a 2  s.  c o m
    String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES";
    try {
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {
            String coffeeName = rs.getString("COF_NAME");
            int supplierID = rs.getInt("SUP_ID");
            float price = rs.getFloat("PRICE");
            int sales = rs.getInt("SALES");
            int total = rs.getInt("TOTAL");
            System.out.println(coffeeName + ", " + supplierID + ", " + price + ", " + sales + ", " + total);
        }

    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
}

From source file:tds.assessment.repositories.impl.StrandQueryRepositoryImpl.java

private static Strand buildStrandFromResultSet(final ResultSet rs) throws SQLException {
    return new Strand.Builder().withName(rs.getString("name")).withKey(rs.getString("_key"))
            .withMinItems(rs.getInt("minitems")).withMaxItems(rs.getInt("maxitems"))
            // calling getObject() and casting to Float because .getFloat() defaults to 0 if null
            .withAdaptiveCut((Float) rs.getObject("adaptivecut")).withSegmentKey(rs.getString("segmentKey"))
            .withStrictMax(rs.getBoolean("isstrictmax")).withBpWeight(rs.getFloat("bpweight"))
            .withStartInfo((Float) rs.getObject("startInfo")).withScalar((Float) rs.getObject("scalar"))
            .withPrecisionTarget((Float) rs.getObject("precisionTarget"))
            .withPrecisionTargetMetWeight((Float) rs.getObject("precisionTargetMetWeight"))
            .withPrecisionTargetNotMetWeight((Float) rs.getObject("precisionTargetNotMetWeight")).build();
}

From source file:oculus.memex.rest.PreclusterDetailsResource.java

private static void getLocations(HashMap<Integer, DataRow> result, StringBuffer adstring) {
    MemexOculusDB oculusdb = MemexOculusDB.getInstance();
    Connection oculusconn = oculusdb.open();
    String sqlStr;/* w  w w  . j  a v a  2  s  .  c om*/
    Statement stmt;
    sqlStr = "SELECT ads_id,label,latitude,longitude FROM " + AdLocations.AD_LOCATIONS_TABLE
            + " WHERE ads_id IN " + adstring.toString();
    stmt = null;
    try {
        stmt = oculusconn.createStatement();
        ResultSet rs = stmt.executeQuery(sqlStr);
        while (rs.next()) {
            int adid = rs.getInt("ads_id");
            String location = rs.getString("label");
            Float latitude = rs.getFloat("latitude");
            Float longitude = rs.getFloat("longitude");
            setAttribute(adid, "latitude", "" + latitude, result);
            setAttribute(adid, "longitude", "" + longitude, result);
            setAttribute(adid, "location", location, result);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    oculusdb.close();
}

From source file:dsd.dao.WorstCaseDAO.java

public static ArrayList<WorstPylonCase> GetAllForPeriod(Calendar startDate, Calendar endDate, boolean traffic,
        boolean debris) {
    try {//from   w ww .  j ava  2s  .c  o m
        Connection con = DAOProvider.getDataSource().getConnection();
        ArrayList<WorstPylonCase> worstCaseDataList = new ArrayList<WorstPylonCase>();
        try {
            String tableName = GetTableNameForDataType(traffic, debris);
            Object[] parameters = new Object[2];
            parameters[0] = new Timestamp(startDate.getTimeInMillis());
            parameters[1] = new Timestamp(endDate.getTimeInMillis());
            ResultSet results = DAOProvider.SelectTableSecure(tableName, "*",
                    " timestamp >= ? and timestamp <= ? ", "", con, parameters);
            while (results.next()) {
                WorstPylonCase dataTuple = new WorstPylonCase(results.getInt(fields[0]));

                Pylon pylon = new Pylon(results.getInt(fields[0]));
                pylon.setN(results.getFloat(fields[1]));
                pylon.setTx(results.getFloat(fields[2]));
                pylon.setTy(results.getFloat(fields[3]));
                pylon.setMx(results.getFloat(fields[4]));
                pylon.setMy(results.getFloat(fields[5]));
                pylon.setM(results.getFloat(fields[6]));

                dataTuple.setPylon(pylon);

                dataTuple.setID(results.getLong("ID"));
                dataTuple.setComboNumber(results.getInt(fields[8]));
                dataTuple.setSafetyFactor(results.getFloat(fields[7]));
                dataTuple.setTimestamp(results.getTimestamp(fields[9]).getTime());

                worstCaseDataList.add(dataTuple);
            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
        con.close();
        return worstCaseDataList;
    } catch (Exception exc) {
        exc.printStackTrace();
    }
    return null;

}

From source file:org.arkanos.aos.api.data.Task.java

/**
 * Fetches a given task from the database and calculates extra information.
 * /*w w w .j  a v a  2 s . c  o m*/
 * @param id
 *            specifies the task to be fetched.
 * @return the task instance or null if none was found.
 */
static public Task getTask(int id) {
    try {
        ResultSet rs = Database
                .query("SELECT * " + " FROM " + Task.TABLE_NAME + " WHERE " + Task.FIELD_ID + " = " + id + ";");
        if ((rs != null) && rs.next())
            return new Task(rs.getInt(Task.FIELD_ID), rs.getInt(Task.FIELD_GOAL_ID),
                    rs.getString(Task.FIELD_NAME), rs.getFloat(Task.FIELD_INITIAL),
                    rs.getFloat(Task.FIELD_TARGET));
    } catch (SQLException e) {
        Log.error("Task", "Problems retrieving a Task.");
        e.printStackTrace();
    }
    return null;
}

From source file:dsd.dao.CalculatedDataDAO.java

public static ArrayList<CalculatedData> GetAllForPeriod(Calendar startDate, Calendar endDate,
        eCalculatedDataType dataType) {/*  www  .  ja  v a  2  s .  com*/
    try {
        Connection con = DAOProvider.getDataSource().getConnection();
        ArrayList<CalculatedData> calculatedDataList = new ArrayList<CalculatedData>();
        try {
            String tableName = GetTableNameForDataType(dataType);
            Object[] parameters = new Object[2];
            parameters[0] = new Timestamp(startDate.getTimeInMillis());
            parameters[1] = new Timestamp(endDate.getTimeInMillis());
            ResultSet results = DAOProvider.SelectTableSecure(tableName, "*",
                    " timestamp >= ? and timestamp <= ? ", "", con, parameters);
            while (results.next()) {
                CalculatedData dataTuple = new CalculatedData();
                dataTuple.setCalulcatedDataID(results.getLong("ID"));
                dataTuple.setWindSpeed(results.getFloat(fields[0]));
                dataTuple.setWindDirection(results.getFloat(fields[1]));
                dataTuple.setWindSpeedMax(results.getFloat(fields[2]));
                dataTuple.setWindDirectionMax(results.getFloat(fields[3]));
                dataTuple.setHydrometer(results.getFloat(fields[4]));
                dataTuple.setHydrometerVariance(results.getFloat(fields[5]));
                dataTuple.setSonar(results.getFloat(fields[6]));
                dataTuple.setSonarVariance(results.getFloat(fields[7]));
                dataTuple.setSonarPercCorrect(results.getFloat(fields[8]));
                dataTuple.setSonarPercWrong(results.getFloat(fields[9]));
                dataTuple.setSonarPercOutOfWater(results.getFloat(fields[10]));
                dataTuple.setSonarPercError(results.getFloat(fields[11]));
                dataTuple.setSonarPercUncertain(results.getFloat(fields[12]));
                dataTuple.setSafetyFactor00(results.getFloat(fields[13]));
                dataTuple.setSafetyFactor01(results.getFloat(fields[14]));
                dataTuple.setSafetyFactor10(results.getFloat(fields[15]));
                dataTuple.setSafetyFactor11(results.getFloat(fields[16]));
                dataTuple.setWaterSpeed(results.getFloat(fields[17]));
                dataTuple.setWaterFlowRate(results.getFloat(fields[18]));
                dataTuple.setTimestamp(results.getTimestamp(fields[19]).getTime());
                calculatedDataList.add(dataTuple);
            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
        con.close();
        return calculatedDataList;
    } catch (Exception exc) {
        exc.printStackTrace();
    }
    return null;

}