我正在尝试解析JSON对象时遇到SQL异常。这是我的代码片段。
public JSONArray paymentMode(String stringObjects) throws SQLException {
JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
//JSONOBJECT RETURN
JSONObject jsonObjectReturn = JSONFactoryUtil.createJSONObject();
JSONObject obj = JSONFactoryUtil.createJSONObject();
JSONArray array = JSONFactoryUtil.createJSONArray();
Session session = null;
Connection conn = null;
CallableStatement callableStatement = null;
try {
// test
BeanLocator beanLocator = PortletBeanLocatorUtil
.getBeanLocator("Mrcos-services-portlet");
BasicDataSource bds = (BasicDataSource) beanLocator
.locate("mrcosDataSourceTarget");
conn = (Connection) bds.getConnection();
jsonObject = JSONFactoryUtil.createJSONObject(stringObjects);
JSONArray jsonArray = jsonObject.getJSONArray("dataArray");
for (int i = 0; i < jsonArray.length(); i++) {
obj = jsonArray.getJSONObject(i);
callableStatement = (CallableStatement) conn
.prepareCall("{call PaymentModeSPv2(?,?,?,?,?,?,?,?,?,?,?,?,?)}");
callableStatement.setString(1, jsonObject.getString("mode"));
callableStatement.setString(2, jsonObject.getString("num1"));
callableStatement.setString(3, jsonObject.getString("date1"));
callableStatement.setString(4, jsonObject.getString("num2"));
callableStatement.setString(5, jsonObject.getString("date2"));
callableStatement.setString(6, jsonObject.getString("remarks"));
callableStatement.setDouble(7, jsonObject.getDouble("amount"));
callableStatement.setString(8, jsonObject.getString("receipt"));
callableStatement.setString(9, jsonObject.getString("algo"));
callableStatement.setString(10, jsonObject.getString("code"));
callableStatement.setString(11, jsonObject.getString("address"));
callableStatement.setString(12, jsonObject.getString("status"));
callableStatement.registerOutParameter(13, Types.INTEGER);
callableStatement.executeQuery();
String xreturn = callableStatement.getString(13);
jsonObjectReturn = JSONFactoryUtil.createJSONObject();
jsonObjectReturn.put("xreturn", xreturn);
array.put(jsonObjectReturn);
System.out.println("jsonObjectReturn : " + jsonObjectReturn);
}
return array;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (callableStatement != null) {
callableStatement.close();
}
if (conn != null) {
conn.close();
}
closeSession(session);
}
jsonObjectReturn = JSONFactoryUtil.createJSONObject();
jsonObjectReturn.put("result", "NO RESULTS FOUND!");
return array;
}
我在 callableStatement.setDouble(7,jsonObject.getDouble(“amount”))时遇到错误; 说 java.sql.SQLException:'NaN'不是有效的数字或近似数值。
金额在我的数据库中是十进制的。我也尝试使用float而不是double但仍然提供相同的错误消息。我也尝试传递一个整数和一个十进制数,但仍然发生相同的错误。请帮助我暂时停留一段时间。谢谢!
答案 0 :(得分:0)
您在javascript
中收到了[NaN][1]
jsonObject.getDouble("amount")
。所以你需要以某种方式处理它。例如。您可以将其替换为0
或Double.NaN
:
String amt = jsonObject.getString("amount");
Double amount = "NaN".equals(amt) ? Double.NaN : Double.parseDouble(amt);
callableStatement.setDouble(7, amount);