mysql 查询不返回结果,查询不返回数据库

在尝试通过FK_adId值为52从数据库中获取汽车时遇到问题。已确认存在对应ID的车辆,但查询方法未能返回。错误在于rs.next()被调用了两次,导致错过了实际要查找的记录。只需调用一次rs.next()即可解决此问题。
摘要由CSDN通过智能技术生成

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()) {

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值