java - 获取 SQL 异常 'NaN' 不是有效的数值或近似数值

标签 java sql json nan

我在尝试解析 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]。所以你需要以某种方式处理它。例如。您可以将其替换为 0Double.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/

相关文章:

java - 我的 XML 文件在 Java Apache Tomcat v7.0 中出现问题

java - 是否在列表上运行 remove(int index) 方法以删除对象实例调用垃圾收集?

java - 如何在java中使用反射扩展基类的多个类

sql - 创建具有日期/时间范围约束的表以防止重叠

php - 在mysql中如何使用增量i=1,i++?

c++ - 通过 HttpOpenRequest 发送 JSON 数据

c++ - 如何使用 QT 创建 JSON 数组

java - 为什么 array[d] 的值为 5?

MySQL 让它更快,将成员计数添加到家庭表中

javascript - AJAX:检查字符串是否为 JSON?