protected <V> V getScalarFromResultSet(ResultSet rs, Class<V> returnType) throws SQLException { Object value = null; if (Integer.class == returnType) { value = rs.getInt(1); } else if (Long.class == returnType) { value = rs.getLong(1); } else if (String.class == returnType) { value = rs.getString(1); } else if (Boolean.class == returnType) { value = rs.getBoolean(1); } else if (Double.class == returnType) { value = rs.getDouble(1); } else if (Date.class == returnType) { value = rs.getDate(1); } else if (Timestamp.class == returnType) { value = rs.getTimestamp(1); } else { value = rs.getObject(1); } if (null == value) { throw new NullPointerException("Cannot get value from ResultSet of type " + returnType.getName()); } return returnType.cast(value); }
private void testUnsignedNumberSpec(Connection conn, double data) throws Exception { updateUnsignedTable(conn, data); ResultSet rs = conn.createStatement().executeQuery("SELECT SQRT(doub),SQRT(fl),SQRT(inte),SQRT(lon),SQRT(smalli),SQRT(tinyi) FROM " + testUnsignedTable ); assertTrue(rs.next()); Double d = Double.valueOf(data); assertTrue(Math.abs(rs.getDouble(1) - Math.sqrt(d.doubleValue())) < ZERO); assertTrue(Math.abs(rs.getDouble(2) - Math.sqrt(d.floatValue())) < ZERO); assertTrue(Math.abs(rs.getDouble(3) - Math.sqrt(d.intValue())) < ZERO); assertTrue(Math.abs(rs.getDouble(4) - Math.sqrt(d.longValue())) < ZERO); assertTrue(Math.abs(rs.getDouble(5) - Math.sqrt(d.shortValue())) < ZERO); assertTrue(Math.abs(rs.getDouble(6) - Math.sqrt(d.byteValue())) < ZERO); assertTrue(!rs.next()); PreparedStatement stmt = conn.prepareStatement("SELECT k FROM " + testUnsignedTable + " WHERE SQRT(doub)>0 AND SQRT(fl)>0 AND SQRT(inte)>0 AND SQRT(lon)>0 AND SQRT(smalli)>0 AND SQRT(tinyi)>0"); rs = stmt.executeQuery(); if (data > 0) { assertTrue(rs.next()); assertEquals(KEY, rs.getString(1)); } assertTrue(!rs.next()); }
@Override public void run(Connection conn) throws Exception { try (PreparedStatement stmt = conn.prepareStatement("SELECT 'zzz'")) { ResultSet rs = stmt.executeQuery(); rs.next(); rs.getDouble(1); } } }, "0700B", "Cannot convert to double");
@Override public Pair<List<Long>, Map<Long, Double>> listClusterIdsInZoneByVmCount(long zoneId, long accountId) { TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List<Long> result = new ArrayList<Long>(); Map<Long, Double> clusterVmCountMap = new HashMap<Long, Double>(); StringBuilder sql = new StringBuilder(ORDER_CLUSTERS_NUMBER_OF_VMS_FOR_ACCOUNT_PART1); sql.append("host.data_center_id = ?"); sql.append(ORDER_CLUSTERS_NUMBER_OF_VMS_FOR_ACCOUNT_PART2); try { pstmt = txn.prepareAutoCloseStatement(sql.toString()); pstmt.setLong(1, accountId); pstmt.setLong(2, zoneId); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { Long clusterId = rs.getLong(1); result.add(clusterId); clusterVmCountMap.put(clusterId, rs.getDouble(2)); } return new Pair<List<Long>, Map<Long, Double>>(result, clusterVmCountMap); } catch (SQLException e) { throw new CloudRuntimeException("DB Exception on: " + sql, e); } catch (Throwable e) { throw new CloudRuntimeException("Caught: " + sql, e); } }
System.out.print("\n"); if (rs.next()) { int i = 0, cc = 11; String cntRows = rs.getString(1); "Min", "Max", "StdDev", "p05", "p25", "p50", "p75", "p95")); for(Column c : row.getColumns()) { String avg = String.format("%.2f", rs.getDouble(4 + i*cc)); if (rs.wasNull()) avg = "null"; String stddev = String.format("%.2f", rs.getDouble(7 + i*cc)); if (rs.wasNull()) stddev = "null"; String p05 = String.format("%.2f", rs.getDouble(8 + i*cc)); if (rs.wasNull()) p05 = "null"; String p25 = String.format("%.2f", rs.getDouble(9 + i*cc)); String p50 = String.format("%.2f", rs.getDouble(10 + i*cc)); String p75 = String.format("%.2f", rs.getDouble(11 + i*cc)); String p95 = String.format("%.2f", rs.getDouble(12 + i*cc));
final int nrOfColumns = meta.getColumnCount(); long nrOfRows = 0; while (rs.next()) { if (callback != null) { callback.processRow(rs); value = rs.getFloat(i); } else if (javaSqlType == DOUBLE) { value = rs.getDouble(i); } else if (javaSqlType == INTEGER || javaSqlType == TINYINT || javaSqlType == SMALLINT) { value = rs.getInt(i);
@Override public void processRow(ResultSet rs) throws SQLException { ParameterType type = ParameterType.valueOf(rs.getString(3)); JobParameter value = null; if (type == ParameterType.STRING) { value = new JobParameter(rs.getString(4), rs.getString(8).equalsIgnoreCase("Y")); } else if (type == ParameterType.LONG) { value = new JobParameter(rs.getLong(6), rs.getString(8).equalsIgnoreCase("Y")); } else if (type == ParameterType.DOUBLE) { value = new JobParameter(rs.getDouble(7), rs.getString(8).equalsIgnoreCase("Y")); } else if (type == ParameterType.DATE) { value = new JobParameter(rs.getTimestamp(5), rs.getString(8).equalsIgnoreCase("Y")); } // No need to assert that value is not null because it's an enum map.put(rs.getString(2), value); } };
case Types.DECIMAL: case Types.NUMERIC: double dbl = rs.getDouble(colIndex); if (rs.wasNull()) { cell.setCellValue(NULL_VALUE); } else { case Types.VARCHAR: case Types.LONGNVARCHAR: String val = rs.getString(colIndex); if (rs.wasNull()) { cell.setCellValue(NULL_VALUE); } else { if (rs.wasNull()) { cell.setCellValue(NULL_VALUE); } else { cell.setCellValue(rs.getString(colIndex));
private void testSignedNumberSpec(Connection conn, double data) throws Exception { updateSignedTable(conn, data); ResultSet rs = conn.createStatement().executeQuery("SELECT SQRT(doub),SQRT(fl),SQRT(inte),SQRT(lon),SQRT(smalli),SQRT(tinyi) FROM " + testSignedTable ); assertTrue(rs.next()); Double d = Double.valueOf(data); assertTrue(Math.abs(rs.getDouble(1) - Math.sqrt(d.doubleValue())) < ZERO); assertTrue(Math.abs(rs.getDouble(2) - Math.sqrt(d.floatValue())) < ZERO); assertTrue(Math.abs(rs.getDouble(3) - Math.sqrt(d.intValue())) < ZERO); assertTrue(Math.abs(rs.getDouble(4) - Math.sqrt(d.longValue())) < ZERO); assertTrue(Math.abs(rs.getDouble(5) - Math.sqrt(d.shortValue())) < ZERO); assertTrue(Math.abs(rs.getDouble(6) - Math.sqrt(d.byteValue())) < ZERO); assertTrue(!rs.next()); PreparedStatement stmt = conn.prepareStatement("SELECT k FROM " + testSignedTable + " WHERE SQRT(doub)>0 AND SQRT(fl)>0 AND SQRT(inte)>0 AND SQRT(lon)>0 AND SQRT(smalli)>0 AND SQRT(tinyi)>0"); rs = stmt.executeQuery(); if (data > 0) { assertTrue(rs.next()); assertEquals(KEY, rs.getString(1)); } assertTrue(!rs.next()); }
/** * Find a <code>Node</code> by its ID. */ LatLong findNodeByID(long id) { try { this.pStmtNodesR.setLong(1, id); ResultSet rs = this.pStmtNodesR.executeQuery(); if (rs.next()) { double lat = rs.getDouble(1); double lon = rs.getDouble(2); return new LatLong(lat, lon); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } return null; }
@Override public Pair<List<Long>, Map<Long, Double>> listClusterIdsInPodByVmCount(long podId, long accountId) { TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List<Long> result = new ArrayList<Long>(); Map<Long, Double> clusterVmCountMap = new HashMap<Long, Double>(); StringBuilder sql = new StringBuilder(ORDER_CLUSTERS_NUMBER_OF_VMS_FOR_ACCOUNT_PART1); sql.append("host.pod_id = ?"); sql.append(ORDER_CLUSTERS_NUMBER_OF_VMS_FOR_ACCOUNT_PART2); try { pstmt = txn.prepareAutoCloseStatement(sql.toString()); pstmt.setLong(1, accountId); pstmt.setLong(2, podId); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { Long clusterId = rs.getLong(1); result.add(clusterId); clusterVmCountMap.put(clusterId, rs.getDouble(2)); } return new Pair<List<Long>, Map<Long, Double>>(result, clusterVmCountMap); } catch (SQLException e) { throw new CloudRuntimeException("DB Exception on: " + sql, e); } catch (Throwable e) { throw new CloudRuntimeException("Caught: " + sql, e); } }
final int nrOfColumns = meta.getColumnCount(); long nrOfRows = 0; while (rs.next()) { if (callback != null) { callback.processRow(rs); value = rs.getFloat(i); } else if (javaSqlType == DOUBLE) { value = rs.getDouble(i); } else if (javaSqlType == INTEGER || javaSqlType == TINYINT || javaSqlType == SMALLINT) { value = rs.getInt(i);
/** * Set the new value from the result set */ public Var setValue(ResultSet rs, ResultSetMetaData rsm, int idx) throws SQLException { int type = rsm.getColumnType(idx); if (type == java.sql.Types.CHAR || type == java.sql.Types.VARCHAR) { cast(new Var(rs.getString(idx))); } else if (type == java.sql.Types.INTEGER || type == java.sql.Types.BIGINT || type == java.sql.Types.SMALLINT || type == java.sql.Types.TINYINT) { cast(new Var(new Long(rs.getLong(idx)))); } else if (type == java.sql.Types.DECIMAL || type == java.sql.Types.NUMERIC) { cast(new Var(rs.getBigDecimal(idx))); } else if (type == java.sql.Types.FLOAT || type == java.sql.Types.DOUBLE) { cast(new Var(new Double(rs.getDouble(idx)))); } return this; }