【问题标题】:Getting an SQL Exception 'NaN' is not a valid numeric or approximate numeric value获取 SQL 异常“NaN”不是有效的数值或近似数值
【发布时间】:2017-02-20 21:21:39
【问题描述】:

我在尝试解析 JSON 对象时遇到 SQL 异常。这是我的代码sn-p。

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' is not a valid数值或近似数值

金额在我的数据库中是十进制的。我也尝试使用 float 而不是 double 但仍然给出相同的错误消息。我也尝试传递一个整数和一个十进制数,但仍然发生相同的错误。请帮助我现在被困了一段时间。谢谢!

【问题讨论】:

  • 看起来“金额”属性不包含数字。你能用调试器找出来吗?
  • 听起来好像jsonObject.getDouble("amount") 返回字符串"NaN" 而您的DBMS 无法将其解析为“数字”
  • 大家好。是的,每次我输入一个数字时,我的 jsonObject.getDouble("amount") 都会返回 NaN。我该怎么办?
  • 在下面的cmets中使用@tamas Rev的代码
  • 我尝试使用下面的 Tamas Rev 代码,但仍然给我一个错误..

标签: java sql json nan


【解决方案1】:

您将在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);

【讨论】:

  • 有一个特殊的方法比如 Double.isNaN
  • 我不知道JSONObject 是如何处理 NaN-s 的。可能Double.isNaN(jsonObject.getDouble("amount")) ? 0 : jsonObject.getDouble("amount") 也可以。
  • 嗨@TamasRev,我尝试了您的解决方案,但仍然出现错误。 java.lang.NumberFormatException:空字符串在 sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1020) at java.lang.Double.parseDouble(Double.java:540)
  • 你的第二个解决方案给了我类型不匹配:无法从双精度转换为布尔值
  • @chiradee 你对jsonObject.getDouble("amount") 有什么好处?另外,jsonObject.getString("amount") 有什么用?这些将帮助我编写更准确的解决方案。
猜你喜欢
  • 1970-01-01
  • 2016-08-09
  • 2017-08-10
  • 2014-08-16
  • 2012-08-10
  • 2017-07-04
  • 2016-05-12
  • 2015-11-19
  • 2011-04-08
相关资源
最近更新 更多