List of usage examples for java.sql ResultSet getFloat
float getFloat(String columnLabel) throws SQLException;
ResultSet
object as a float
in the Java programming language. From source file:org.arkanos.aos.api.data.Goal.java
/** * Fetches all goals but calculates extra data based on a time range. * Both dates provided should be in format YYYY-MM-DD HH:mm:ss.0 * //w ww . j a v a2s. c om * @param user_name * defines the user. * @param from * specifies the starting of the period. * @param to * specifies the end of the period. * @return */ static public Vector<Goal> getUserGoalsSnapshot(String user_name, String from, String to) { try { ResultSet rs = Database.query("SELECT * FROM " + Goal.TABLE_NAME + " WHERE " + Goal.FIELD_USER_NAME + " = \"" + user_name + "\";"); Vector<Goal> results = new Vector<Goal>(); while ((rs != null) && rs.next()) { Goal newone = new Goal(rs.getInt(Goal.FIELD_ID), rs.getString(Goal.FIELD_TITLE), rs.getInt(Goal.FIELD_TIME_PLANNED), rs.getString(Goal.FIELD_DESCRIPTION), user_name); //TODO: Can this query be optimized? ResultSet newrs = Database .query("SELECT AVG(help.progress) AS completion, SUM(help.spent) AS total_time_spent FROM (" + "SELECT IF(SUM((w." + Work.FIELD_RESULT + ")/(t." + Task.FIELD_TARGET + "-t." + Task.FIELD_INITIAL + ")) IS NULL, 0, SUM((w." + Work.FIELD_RESULT + ")/(t." + Task.FIELD_TARGET + "-t." + Task.FIELD_INITIAL + "))) AS progress, " + "SUM(" + Work.FIELD_TIME_SPENT + ") AS spent FROM goal g " + "LEFT JOIN " + Task.TABLE_NAME + " t on t." + Task.FIELD_GOAL_ID + " = g." + Goal.FIELD_ID + " " + "LEFT JOIN (SELECT * FROM " + Work.TABLE_NAME + " WHERE " + Work.FIELD_START + " >= \"" + from + "\" " + "AND " + Work.FIELD_START + " <= \"" + to + "\") " + " w ON t." + Task.FIELD_ID + " = w." + Work.FIELD_TASK_ID + " " + "WHERE g." + Goal.FIELD_ID + " = " + newone.getID() + " " + "AND g." + Goal.FIELD_USER_NAME + " = \"" + user_name + "\" " + "GROUP BY t." + Task.FIELD_ID + ",g." + Goal.FIELD_ID + ") help;"); if ((newrs != null) && newrs.next()) { newone.setCompletion(newrs.getFloat("completion")); newone.setTotalTimeSpent(newrs.getInt("total_time_spent")); } results.add(newone); } return results; } catch (SQLException e) { Log.error("Goal", "Problems retrieving goals on a given period."); e.printStackTrace(); } return null; }
From source file:org.apache.hawq.pxf.plugins.jdbc.JdbcReadResolver.java
@Override public List<OneField> getFields(OneRow row) throws Exception { ResultSet result = (ResultSet) row.getData(); LinkedList<OneField> fields = new LinkedList<>(); for (int i = 0; i < columns.size(); i++) { ColumnDescriptor column = columns.get(i); String colName = column.columnName(); Object value = null;//from ww w.j a va2 s. co m OneField oneField = new OneField(); oneField.type = column.columnTypeCode(); switch (DataType.get(oneField.type)) { case INTEGER: value = result.getInt(colName); break; case FLOAT8: value = result.getDouble(colName); break; case REAL: value = result.getFloat(colName); break; case BIGINT: value = result.getLong(colName); break; case SMALLINT: value = result.getShort(colName); break; case BOOLEAN: value = result.getBoolean(colName); break; case BYTEA: value = result.getBytes(colName); break; case VARCHAR: case BPCHAR: case TEXT: case NUMERIC: value = result.getString(colName); break; case TIMESTAMP: case DATE: value = result.getDate(colName); break; default: throw new UnsupportedOperationException("Unknwon Field Type : " + DataType.get(oneField.type).toString() + ", Column : " + column.toString()); } oneField.val = value; fields.add(oneField); } return fields; }
From source file:org.apache.cocoon.util.JDBCTypeConversions.java
/** * Get the Statement column so that the results are mapped correctly. * (this has been copied from AbstractDatabaseAction and modified slightly) *///from ww w. jav a2 s . c o m public static Object getColumn(ResultSet set, Configuration column) throws Exception { Integer type = (Integer) JDBCTypeConversions.typeConstants.get(column.getAttribute("type")); String dbcol = column.getAttribute("name"); Object value = null; switch (type.intValue()) { case Types.CLOB: case Types.CHAR: Clob dbClob = set.getClob(dbcol); int length = (int) dbClob.length(); InputStream asciiStream = new BufferedInputStream(dbClob.getAsciiStream()); byte[] buffer = new byte[length]; asciiStream.read(buffer); String str = new String(buffer); asciiStream.close(); value = str; break; case Types.BIGINT: value = set.getBigDecimal(dbcol); break; case Types.TINYINT: value = new Byte(set.getByte(dbcol)); break; case Types.VARCHAR: value = set.getString(dbcol); break; case Types.DATE: value = set.getDate(dbcol); break; case Types.DOUBLE: value = new Double(set.getDouble(dbcol)); break; case Types.FLOAT: value = new Float(set.getFloat(dbcol)); break; case Types.INTEGER: value = new Integer(set.getInt(dbcol)); break; case Types.NUMERIC: value = new Long(set.getLong(dbcol)); break; case Types.SMALLINT: value = new Short(set.getShort(dbcol)); break; case Types.TIME: value = set.getTime(dbcol); break; case Types.TIMESTAMP: value = set.getTimestamp(dbcol); break; case Types.ARRAY: value = set.getArray(dbcol); // new Integer(set.getInt(dbcol)); break; case Types.BIT: value = BooleanUtils.toBooleanObject(set.getBoolean(dbcol)); break; case Types.STRUCT: value = (Struct) set.getObject(dbcol); break; case Types.OTHER: value = set.getObject(dbcol); break; default: // The blob types have to be requested separately, via a Reader. value = ""; break; } return value; }
From source file:com.mobilewallet.common.dao.LoginDAO.java
public User login(String userId) { User user = null;/* ww w . j a v a 2 s . c om*/ Connection connection = null; PreparedStatement pstmt = null; ResultSet rs = null; try { connection = dataSource.getConnection(); pstmt = connection.prepareStatement(loginQuery); pstmt.setString(1, userId); rs = pstmt.executeQuery(); if (rs.next()) { user = new User(); user.setUserId(rs.getLong("u_id")); user.setEmail(rs.getString("u_email")); user.setName(rs.getString("u_name")); user.setMyRefCode(rs.getString("u_my_ref_code")); user.setFriendRefCode(rs.getString("u_friend_ref_code")); user.setAmount(rs.getFloat("w_amount")); log.info("Amount In DAO : " + rs.getFloat("w_amount")); } } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } } catch (Exception ex) { } try { if (pstmt != null) { pstmt.close(); } } catch (Exception ex) { } try { if (connection != null) { connection.close(); } } catch (Exception ex) { } } return user; }
From source file:org.georepublic.db.utils.ResultSetConverter.java
public static JSONArray convertGeoJson(ResultSet rs) throws SQLException, JSONException { JSONArray json = new JSONArray(); ResultSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { int numColumns = rsmd.getColumnCount(); JSONObject obj = new JSONObject(); JSONObject feat = new JSONObject(); feat.put("type", "Feature"); for (int i = 1; i < numColumns + 1; i++) { String column_name = rsmd.getColumnName(i); if (StringUtils.equals(column_name, "the_geom")) { continue; }//from w w w.ja v a 2 s . c o m if (StringUtils.equals(column_name, "geojson")) { continue; } 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.getInt(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.VARCHAR) { 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)); } } feat.put("properties", obj); try { rs.findColumn("lon"); rs.findColumn("lat"); JSONObject geo = new JSONObject(); JSONArray coord = new JSONArray(); coord.put(rs.getDouble("lon")); coord.put(rs.getDouble("lat")); geo.put("type", "point"); geo.put("coordinates", coord); feat.put("geometry", geo); } catch (Exception ex1) { ; } json.put(feat); } return json; }
From source file:org.arkanos.aos.api.data.Task.java
/** * Fetches all tasks from a user./*from ww w. ja va2 s. c o m*/ * * @param user_name * defines the user. * @return all tasks or null, in case there are connection problems. */ static public Vector<Task> getUserTasks(String user_name) { try { ResultSet rs = Database.query("SELECT t." + Task.FIELD_ID + " AS " + Task.FIELD_ID + "," + "t." + Task.FIELD_GOAL_ID + " AS " + Task.FIELD_GOAL_ID + "," + "t." + Task.FIELD_NAME + " AS " + Task.FIELD_NAME + "," + Task.FIELD_INITIAL + "," + Task.FIELD_TARGET + "," + "SUM((w." + Work.FIELD_RESULT + ")/(t." + Task.FIELD_TARGET + "-t." + Task.FIELD_INITIAL + ")) AS " + Task.EXTRA_COMPLETION + "," + "SUM(w." + Work.FIELD_TIME_SPENT + ") AS " + Task.EXTRA_TOTAL_TIME_SPENT + "," + "t." + Task.FIELD_INITIAL + " + SUM(w." + Work.FIELD_RESULT + ") AS " + Task.EXTRA_CURRENT + "," + "g." + Goal.FIELD_TITLE + " AS " + Task.EXTRA_GOAL_TITLE + " FROM goal g " + "LEFT JOIN " + Task.TABLE_NAME + " t on t." + Task.FIELD_GOAL_ID + " = g." + Goal.FIELD_ID + " " + "LEFT JOIN " + Work.TABLE_NAME + " w ON t." + Task.FIELD_ID + " = w." + Work.FIELD_TASK_ID + " " + "WHERE g." + Goal.FIELD_USER_NAME + " = \"" + user_name + "\"" + " AND t." + Task.FIELD_ID + " IS NOT NULL " + "GROUP BY t." + Task.FIELD_ID + ",g." + Goal.FIELD_ID + ";"); Vector<Task> results = new Vector<Task>(); while ((rs != null) && rs.next()) { Task newone = 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)); newone.setGoalTitle(rs.getString(Task.EXTRA_GOAL_TITLE)); newone.setCompletion(rs.getFloat(Task.EXTRA_COMPLETION)); newone.setTotalTimeSpent(rs.getInt(Task.EXTRA_TOTAL_TIME_SPENT)); /* Order is important, setCurrent must be last */ newone.setCurrent(rs.getFloat(Task.EXTRA_CURRENT)); results.add(newone); } return results; } catch (SQLException e) { Log.error("Task", "Problems retrieving all Tasks from a user."); e.printStackTrace(); } return null; }
From source file:com.mobilewallet.credits.dao.CreditsDAO.java
public List<WalletHistoryDTO> creditHistory(long userId, int begin, int end) { List<WalletHistoryDTO> list = new ArrayList<WalletHistoryDTO>(); Connection connection = null; PreparedStatement cstmt = null; ResultSet rs = null; try {//ww w. j a va 2s . c o m connection = dataSource.getConnection(); cstmt = connection.prepareStatement(creditHistorQuery); cstmt.setLong(1, userId); cstmt.setInt(2, begin); cstmt.setInt(3, end); rs = cstmt.executeQuery(); WalletHistoryDTO ch = null; while (rs.next()) { ch = new WalletHistoryDTO(); ch.setCid(rs.getLong("wuc_id")); ch.setDesc(rs.getString("wuc_desc")); ch.setAmount(rs.getFloat("wuc_amount")); ch.setcTime(rs.getString("d")); list.add(ch); } } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } } catch (Exception ex) { } try { if (cstmt != null) { cstmt.close(); } } catch (Exception ex) { } try { if (connection != null) { connection.close(); } } catch (Exception ex) { } } return list; }
From source file:org.apache.phoenix.hive.HivePhoenixStoreIT.java
/** * Datatype Test/*from w w w . j a v a 2 s. com*/ * * @throws Exception */ @Test public void dataTypeTest() throws Exception { String testName = "dataTypeTest"; // create a dummy outfile under log folder hbaseTestUtil.getTestFileSystem().createNewFile(new Path(hiveLogDir, testName + ".out")); createFile(StringUtil.EMPTY_STRING, new Path(hiveLogDir, testName + ".out").toString()); createFile(StringUtil.EMPTY_STRING, new Path(hiveOutputDir, testName + ".out").toString()); StringBuilder sb = new StringBuilder(); sb.append("CREATE TABLE phoenix_datatype(ID int, description STRING, ts TIMESTAMP, db " + "DOUBLE,fl FLOAT, us INT)" + HiveTestUtil.CRLF + " STORED BY \"org.apache.phoenix.hive.PhoenixStorageHandler\"" + HiveTestUtil.CRLF + " TBLPROPERTIES(" + HiveTestUtil.CRLF + " 'phoenix.hbase.table.name'='phoenix_datatype'," + HiveTestUtil.CRLF + " 'phoenix.zookeeper.znode.parent'='hbase'," + HiveTestUtil.CRLF + " 'phoenix.zookeeper.quorum'='localhost:" + hbaseTestUtil.getZkCluster().getClientPort() + "'," + HiveTestUtil.CRLF + " 'phoenix.rowkeys'='id');"); sb.append("INSERT INTO TABLE phoenix_datatype" + HiveTestUtil.CRLF + "VALUES (10, \"foodesc\",\"2013-01-05 01:01:01\",200,2.0,-1);" + HiveTestUtil.CRLF); String fullPath = new Path(hbaseTestUtil.getDataTestDir(), testName).toString(); createFile(sb.toString(), fullPath); runTest(testName, fullPath); String phoenixQuery = "SELECT * FROM phoenix_datatype"; PreparedStatement statement = conn.prepareStatement(phoenixQuery); ResultSet rs = statement.executeQuery(); assert (rs.getMetaData().getColumnCount() == 6); while (rs.next()) { assert (rs.getInt(1) == 10); assert (rs.getString(2).equalsIgnoreCase("foodesc")); /* Need a way how to correctly handle timestamp since Hive's implementation uses time zone information but Phoenix doesn't. */ //assert(rs.getTimestamp(3).equals(Timestamp.valueOf("2013-01-05 02:01:01"))); assert (rs.getDouble(4) == 200); assert (rs.getFloat(5) == 2.0); assert (rs.getInt(6) == -1); } }
From source file:biblivre3.acquisition.order.BuyOrderDAO.java
private final BuyOrderDTO populateDto(ResultSet rs) throws Exception { final BuyOrderDTO dto = new BuyOrderDTO(); dto.setSerial(rs.getInt("serial_order")); dto.setSerialQuotation(rs.getInt("serial_quotation")); dto.setOrderDate(rs.getDate("order_date")); dto.setResponsible(rs.getString("responsable")); dto.setObs(rs.getString("obs")); dto.setStatus(rs.getString("status")); dto.setInvoiceNumber(rs.getString("invoice_number")); dto.setReceiptDate(rs.getDate("receipt_date")); dto.setTotalValue(rs.getFloat("total_value")); dto.setDeliveredQuantity(rs.getInt("delivered_quantity")); dto.setTermsOfPayment(rs.getString("terms_of_payment")); dto.setDeadlineDate(rs.getDate("deadline_date")); return dto;//from ww w . j a v a 2 s . co m }
From source file:io.starter.datamodel.Sys.java
private Object getValueFromDB(String query, Connection connection) throws SQLException { float ret = -1; Logger.debug("Sys: Executing query [ " + query + " ] took:"); long start = System.currentTimeMillis(); java.sql.Statement st = connection.createStatement(); ResultSet rs = st.executeQuery(query); rs.next();/*from w ww . j ava 2 s. c om*/ ret = rs.getFloat(1); // limited valid queries rs.close(); st.close(); long end = System.currentTimeMillis(); long duration = (end - start); Logger.debug(duration + " milliseconds"); return ret; }