oracle number 类型 用Db.query() 查询时返回BigDecimal,有时需要返回int、double、long 等,经过群主和群友山东小木的指导,核心代码是山东小木分享,在此把全部代码分享给大家
/**
* oracle数据库类型为number默认返回BigDecimal
* 此扩展可返回Integer、Double、Long 类型的结合
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public abstract class MyDbKit {
protected Logger logger = Logger.getLogger(getClass());
/**
* 数据返回类型
*
*/
public static final class RESULT_TYPE {
/**
* 返回Integer类型
*/
public static final String INTEGER="Integer";
/**
* 返回Double类型
*/
public static final String DOUBLE="Double";
/**
* 返回Long类型
*/
public static final String LONG="Long";
}
/**
* 返回 Integer 类型的集合
* @param sql
* @return
*/
public static List<Integer> queryIntList(String sql) {
return queryList(sql,RESULT_TYPE.INTEGER, new Object[0]);
}
/**
* 返回 Integer 类型的集合
* @param sql
* @param paras
* @return
*/
public static List<Integer> queryIntList(String sql,Object... paras) {
return queryList(sql,RESULT_TYPE.INTEGER, paras);
}
/**
* 返回 Double 类型的集合
* @param sql
* @return
*/
public static List<Double> queryDoubleList(String sql) {
return queryList(sql,RESULT_TYPE.DOUBLE, new Object[0]);
}
/**
* 返回 Double 类型的集合
* @param sql
* @return
*/
public static List<Double> queryDoubleList(String sql,Object... paras) {
return queryList(sql,RESULT_TYPE.DOUBLE, paras);
}
/**
* 返回 Long 类型的集合
* @param sql
* @return
*/
public static List<Long> queryLongList(String sql) {
return queryList(sql,RESULT_TYPE.LONG, new Object[0]);
}
/**
* 返回 Long 类型的集合
* @param sql
* @return
*/
public static List<Long> queryLongList(String sql,Object... paras) {
return queryList(sql,RESULT_TYPE.LONG, paras);
}
/***
* 返回 @param resType 类型的集合
* @param sql
* @param resType
* @param paras
* @return List<retunType>
*/
public static List queryList(String sql, String resType,Object... paras) {
List result = new ArrayList();
Config config = DbKit.getConfig();
PreparedStatement pst = null;
ResultSet rs = null;
Connection conn=null;
try {
conn = config.getConnection();
pst = conn.prepareStatement(sql);
config.getDialect().fillStatement(pst, paras);
rs = pst.executeQuery();
int colAmount = rs.getMetaData().getColumnCount();
if (colAmount > 1) {
while (rs.next()) {
Object[] temp = new Object[colAmount];
for (int i = 0; i < colAmount; i++) {
temp[i] = rs.getObject(i + 1);
}
addObjValue(result, resType, temp.toString());
}
} else if (colAmount == 1) {
while (rs.next()) {
addObjValue(result, resType, rs.getObject(1).toString());
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (pst != null) pst.close();
if (conn != null) config.close(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
public static void addObjValue(List result,String resType,String value) {
switch (resType) {
case RESULT_TYPE.INTEGER:
result.add(Integer.valueOf(value));
break;
case RESULT_TYPE.DOUBLE:
result.add(Double.valueOf(value));
break;
case RESULT_TYPE.LONG:
result.add(Long.valueOf(value));
break;
}
}
}