查询不返回数据库 [英] Query returns nothing from database

查看:229
本文介绍了查询不返回数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用值 FK_adId 从我的数据库中获取汽车。我已经尝试使用 FK_adId 值52来调用该方法,并且我检查了一辆带有 FK_adId 的汽车值52存在于我的数据库中。为什么不归还给我?

I'm trying to get a car from my database using the value FK_adId. I've tried calling the method with the FK_adId value 52, and I've checked that a car with the FK_adId value 52 exists in my database. Why doesn't it get returned to me?

public Car getCar(int adId) {
    Car car = null;
    try {
        Class.forName("org.postgresql.Driver");
        if (con != null) {
            ps = con.prepareStatement("SELECT * FROM \"car\" WHERE \"FK_adId\" = ?;");
            ps.setInt(1, adId);
            rs = ps.executeQuery();
            rs.next();
            if (rs.next()) {
                car = new Car(rs.getString("brand"), rs.getString("vin"), rs.getString("condition"), rs.getInt("distanceTraveled"), rs.getInt("age"), rs.getInt("price"), rs.getInt("FK_adId"));
            }
        }
    } catch (Exception ex) {
        System.out.println(ex);
    }
    return car;
}


推荐答案


        rs.next();
   if (rs.next()) {


拨打 .next()一次,而不是两次。您前进到结果的第一行,然后前进到第二行。由于查询没有返回两行,因此不会获得第二行。你跳过了你想要的那一行。

Call .next() once, not twice. You advance to the first row of the results, then you advance to the second. Since the query did not return two rows, you do not get the second row. You skipped the row you wanted.

这篇关于查询不返回数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆