有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Getting“将nvarchar值转换为JDBC数据类型DOUBLE时出错。”

我试图运行getAllCouples()方法,该方法读取优惠券表并将其发送回angular服务,但我收到了以下消息 将nvarchar值转换为JDBC数据类型DOUBLE时出错。 我看不出任何领域是错误的

我最初认为日期是个问题,所以我做了一个评论,但我还是遇到了一个问题

我的DBDAO代码:

public ArrayList<Coupon> getAllCoupon() throws CouponException {
        ArrayList<Coupon> coupons = new ArrayList<>();
        Connection con = pool.getConnection();


        try {
            PreparedStatement st = con.prepareStatement("select * from coupons");

            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                long id = rs.getLong(1);
                String title = rs.getString(2);
                String message = rs.getString(3);
                String image = rs.getString(4);
            //  Date startDate = rs.getDate(5);
            //  Date endDate = rs.getDate(6);
                int amount = rs.getInt(5);
                double price = rs.getDouble(6);
                Coupontypes type = Coupontypes.valueOf(rs.getString(7));
                coupons.add(new Coupon(id, title, message, image, amount, price, type));

            }

            return coupons;

        } catch (SQLException e) {
            throw new CouponException(e.getMessage());

        } finally {
            pool.returnConnection(con);
        }
        }

我的桌子:

CouponID bigint
标题nvarchar(50)
开始日期
结束日期
金额整数 输入nvarchar(50)
消息nvarchar(最大值)
未经检查的价格浮动 图像nvarchar(50)

我希望得到一个优惠券对象回来,这样我就可以用*ngFor在一个有角度的网站上显示它


共 (1) 个答案

  1. # 1 楼答案

    您正在尝试将java.lang.String强制转换为java.lang.Double。如果将double存储为字符串,则可以执行double price = new Double(rs.getString(6))。确保您的列不会为null,并且它将始终返回双精度值以避免异常