我在尝试解析 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,但仍然给出相同的错误消息。我也尝试传递一个整数和一个小数,但仍然出现相同的错误。请帮助我现在被困了一段时间。谢谢!
最佳答案
您将在 jsonObject.getDouble("amount")
中获取 javascript
[NaN][1]
。所以你需要以某种方式处理它。例如。您可以将其替换为 0
或 Double.NaN
:
String amt = jsonObject.getString("amount");
Double amount = "NaN".equals(amt) ? Double.NaN : Double.parseDouble(amt);
callableStatement.setDouble(7, amount);
关于java - 获取 SQL 异常 'NaN' 不是有效的数值或近似数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39992410/