Example usage for java.sql ResultSet getBigDecimal

List of usage examples for java.sql ResultSet getBigDecimal

Introduction

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

Prototype

BigDecimal getBigDecimal(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a java.math.BigDecimal with full precision.

Usage

From source file:com.khartec.waltz.jobs.ServerHarness.java

public static void main(String[] args) {

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);
    ServerInformationService serverInfoService = ctx.getBean(ServerInformationService.class);
    ServerInformationDao serverInfoDao = ctx.getBean(ServerInformationDao.class);
    DSLContext dsl = ctx.getBean(DSLContext.class);

    IdSelectionOptions options = ImmutableIdSelectionOptions.builder()
            .entityReference(ImmutableEntityReference.builder().kind(EntityKind.ORG_UNIT).id(10).build())
            .scope(HierarchyQueryScope.CHILDREN).build();

    for (int i = 0; i < 5; i++) {
        HarnessUtilities.time("stats", () -> serverInfoService.findStatsForAppSelector(options));
    }//from   w ww. j a  va2s .  c  o m

    String sql = "\n" + "select \n" + "  coalesce(\n"
            + "    sum(case [server_information].[is_virtual] when 1 then 1\n"
            + "                                               else 0\n" + "        end), \n"
            + "    0) [virtual_count], \n" + "  coalesce(\n"
            + "    sum(case [server_information].[is_virtual] when 1 then 0\n"
            + "                                               else 1\n" + "        end), \n"
            + "    0) [physical_count]\n" + "from [server_information]\n"
            + "where [server_information].[asset_code] in (\n" + "  select [application].[asset_code]\n"
            + "  from [application]\n" + "  where [application].[id] in (\n" + "    select [application].[id]\n"
            + "    from [application]\n" + "    where [application].[organisational_unit_id] in (\n"
            + "      130, 260, 70, 200, 10, 140, 270, 80, 210, 20, 150, 280, 90, 220, 30, 160, \n"
            + "      290, 100, 230, 40, 170, 300, 110, 240, 50, 180, 120, 250, 60, 190\n" + "    )\n" + "  )\n"
            + ");\n";

    FunctionUtilities.time("raw q", () -> {
        dsl.connection(conn -> {
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getBigDecimal(1) + " - " + rs.getBigDecimal(2));
            }
        });
        return null;
    });

}

From source file:com.nortal.petit.converter.util.ResultSetHelper.java

public static BigDecimal getBigDecimal(ResultSet rs, ColumnPosition column) throws SQLException {
    return column.isNamed ? rs.getBigDecimal(column.getName()) : rs.getBigDecimal(column.getIndex());
}

From source file:RSMetaData.java

public static void getData(ResultSet rs, int type, int colIdx) throws SQLException {
    switch (type) {
    case java.sql.Types.CHAR:
    case java.sql.Types.VARCHAR:
        System.out.println(rs.getString(colIdx));
        break;//  ww  w . j a  v a2 s  . com

    case java.sql.Types.INTEGER:
        int i = rs.getInt(colIdx);
        System.out.println(i);
        break;

    case java.sql.Types.NUMERIC:
        BigDecimal bd = rs.getBigDecimal(colIdx);
        System.out.println(bd.toString());
        break;

    case java.sql.Types.TIMESTAMP:
    case java.sql.Types.DATE:
        java.sql.Date d = rs.getDate(colIdx);
        System.out.println(d.toString());
        break;

    }
}

From source file:com.acme.spring.jdbc.JdbcTestHelper.java

/**
 * <p>Retrieves all the stocks from database, using passed {@link JdbcTemplate}.</p>
 *
 * @param jdbcTemplate the jdbc template to use
 *
 * @return list of stocks retrieved from database
 *//*w  ww.  j a  v a2s. com*/
public static List<Stock> retrieveAllStocks(JdbcTemplate jdbcTemplate) {

    return jdbcTemplate.query("select id, name, symbol, value, date from Stock order by name",
            new RowMapper<Stock>() {
                public Stock mapRow(ResultSet rs, int rowNum) throws SQLException {

                    int index = 1;

                    Stock result = new Stock();
                    result.setId(rs.getLong(index++));
                    result.setName(rs.getString(index++));
                    result.setSymbol(rs.getString(index++));
                    result.setValue(rs.getBigDecimal(index++));
                    result.setDate(rs.getDate(index++));

                    return result;
                }
            });
}

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

public static void raisePrice(String coffeeName, double maximumPercentage, BigDecimal[] newPrice)
        throws SQLException {

    Connection con = DriverManager.getConnection("jdbc:default:connection");
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    BigDecimal oldPrice;/*ww w .  j a  v a 2 s .c  om*/

    String queryGetCurrentCoffeePrice = "select COFFEES.PRICE " + "from COFFEES "
            + "where COFFEES.COF_NAME = ?";

    pstmt = con.prepareStatement(queryGetCurrentCoffeePrice);
    pstmt.setString(1, coffeeName);
    rs = pstmt.executeQuery();

    if (rs.next()) {
        oldPrice = rs.getBigDecimal(1);
    } else {
        return;
    }

    BigDecimal maximumNewPrice = oldPrice.multiply(new BigDecimal(1 + maximumPercentage));

    // Test if newPrice[0] > maximumNewPrice

    if (newPrice[0].compareTo(maximumNewPrice) == 1) {
        newPrice[0] = maximumNewPrice;
    }

    // Test if newPrice[0] <= oldPrice

    if (newPrice[0].compareTo(oldPrice) < 1) {
        newPrice[0] = oldPrice;
        return;
    }

    String queryUpdatePrice = "update COFFEES " + "set COFFEES.PRICE = ? " + "where COFFEES.COF_NAME = ?";

    pstmt = con.prepareStatement(queryUpdatePrice);
    pstmt.setBigDecimal(1, newPrice[0]);
    pstmt.setString(2, coffeeName);
    pstmt.executeUpdate();
}

From source file:org.castor.jdo.engine.SQLTypeInfos.java

/**
 * Get value from given ResultSet at given index with given SQL type.
 * /*from w ww .ja v a2 s.  com*/
 * @param rs The ResultSet to get the value from.
 * @param index The index of the value in the ResultSet.
 * @param sqlType The SQL type of the value.
 * @return The value.
 * @throws SQLException If a database access error occurs.
 */
public static Object getValue(final ResultSet rs, final int index, final int sqlType) throws SQLException {
    switch (sqlType) {
    case Types.CHAR:
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
        return rs.getString(index);
    case Types.DECIMAL:
    case Types.NUMERIC:
        return rs.getBigDecimal(index);
    case Types.INTEGER:
        int intVal = rs.getInt(index);
        return (rs.wasNull() ? null : new Integer(intVal));
    case Types.TIME:
        return rs.getTime(index, getCalendar());
    case Types.DATE:
        return rs.getDate(index);
    case Types.TIMESTAMP:
        return rs.getTimestamp(index, getCalendar());
    case Types.FLOAT:
    case Types.DOUBLE:
        double doubleVal = rs.getDouble(index);
        return (rs.wasNull() ? null : new Double(doubleVal));
    case Types.REAL:
        float floatVal = rs.getFloat(index);
        return (rs.wasNull() ? null : new Float(floatVal));
    case Types.SMALLINT:
        short shortVal = rs.getShort(index);
        return (rs.wasNull() ? null : new Short(shortVal));
    case Types.TINYINT:
        byte byteVal = rs.getByte(index);
        return (rs.wasNull() ? null : new Byte(byteVal));
    case Types.LONGVARBINARY:
    case Types.VARBINARY:
    case Types.BINARY:
        return rs.getBytes(index);
    case Types.BLOB:
        Blob blob = rs.getBlob(index);
        return (blob == null ? null : blob.getBinaryStream());
    case Types.CLOB:
        return rs.getClob(index);
    case Types.BIGINT:
        long longVal = rs.getLong(index);
        return (rs.wasNull() ? null : new Long(longVal));
    case Types.BIT:
        boolean boolVal = rs.getBoolean(index);
        return (rs.wasNull() ? null : new Boolean(boolVal));
    default:
        Object value = rs.getObject(index);
        return (rs.wasNull() ? null : value);
    }
}

From source file:com.wabacus.system.datatype.BigdecimalType.java

public Object getColumnValue(ResultSet rs, int iindex, AbsDatabaseType dbtype) throws SQLException {
    return rs.getBigDecimal(iindex);
}

From source file:com.wabacus.system.datatype.BigdecimalType.java

public Object getColumnValue(ResultSet rs, String column, AbsDatabaseType dbtype) throws SQLException {
    return rs.getBigDecimal(column);
}

From source file:org.apache.phoenix.util.TestUtil.java

public static void validateRowKeyColumns(ResultSet rs, int i) throws SQLException {
    assertTrue(rs.next());//from   w  w w. jav a 2  s  . c  om
    assertEquals(rs.getString(1), "varchar" + String.valueOf(i));
    assertEquals(rs.getString(2), "char" + String.valueOf(i));
    assertEquals(rs.getInt(3), i);
    assertEquals(rs.getInt(4), i);
    assertEquals(rs.getBigDecimal(5), new BigDecimal(i * 0.5d));
    Date date = new Date(DateUtil.parseDate("2015-01-01 00:00:00").getTime() + (i - 1) * MILLIS_IN_DAY);
    assertEquals(rs.getDate(6), date);
}

From source file:org.openmrs.module.bahmniexports.example.domain.trade.internal.CustomerDebitRowMapper.java

@Override
public CustomerDebit mapRow(ResultSet rs, int ignoredRowNumber) throws SQLException {
    CustomerDebit customerDebit = new CustomerDebit();

    customerDebit.setName(rs.getString(CUSTOMER_COLUMN));
    customerDebit.setDebit(rs.getBigDecimal(PRICE_COLUMN));

    return customerDebit;
}