日期:2014-05-16 浏览次数:20843 次
String sql = "select sum(number) as sumNumberOfOneDay from tableName"; List<Map> rows = getJdbcTemplate().queryForList(sql); for (Map row : rows) { SomeBean item = new SomeBean(); item.setSumNumberOfOneDay(objectToInt(row.get("sumNumberOfOneDay"))); } private int objectToInt(Object obj) { return Integer.parseInt("" + obj); }
Connection conn = getJdbcTemplate().getDataSource().getConnection(); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { String name = rsmd.getColumnName(i); String type = rsmd.getColumnTypeName(i); System.out.println(name + ", " + type); }
The SUM() and AVG() functions return a DECIMAL value for exact-value arguments (integer or DECIMAL), and a DOUBLE value for approximate-value arguments (FLOAT or DOUBLE). (Before MySQL 5.0.3, SUM() and AVG() return DOUBLE for all numeric arguments.)
if (obj instanceof Double) { return ((Double)obj).intValue(); } if (obj instanceof BigDecimal) { return ((BigDecimal)obj).intValue(); } return Integer.parseInt(obj.toString());
List<SomeBean> list = getJdbcTemplate().query(sql, new BeanPropertyRowMapper(SomeBean.class));
val = getString(columnIndex); if ((val != null) && (val.length() != 0)) { if ((val.indexOf("e") == -1) && (val.indexOf("E") == -1) && (val.indexOf(".") == -1)) { return Integer.parseInt(val); } else { // Convert floating point return (int) (Double.parseDouble(val)); } } else { return 0; }