Java 新手指针rs.next

Java 新手指针rs.next,java,sql,pointers,conditional-statements,resultset,Java,Sql,Pointers,Conditional Statements,Resultset,我只希望看到产品用户正在寻找它们,但当执行第二个if时,它会将(指针或任何东西)推到下一个ID(我的ID是唯一的,所以它不会推到任何地方),结果为null。我希望你能理解我的问题:) 首先,您的方法容易受到SQL注入的影响。请查看准备好的报表。 你应该这样做: ResultSet rs = stmt.executeQuery("SELECT * FROM products where ID=" + removeName); if (!rs.next()) { m = "ID not

我只希望看到产品用户正在寻找它们,但当执行第二个if时,它会将(指针或任何东西)推到下一个ID(我的ID是唯一的,所以它不会推到任何地方),结果为null。我希望你能理解我的问题:)


首先,您的方法容易受到SQL注入的影响。请查看准备好的报表。

你应该这样做:

ResultSet rs = stmt.executeQuery("SELECT * FROM products where ID=" + removeName);
if (!rs.next()) {
      m = "ID not found.";
      return m;
}

在你的情况下,你可以努力避免这个问题


问题在于,您阅读第一个结果是为了知道是否至少有一个结果,然后尝试使用下一个结果,却错过了第一个结果(根据您的问题描述改编)。我解释了它是如何工作的

此问题的一个可能解决方案是假设执行查询时没有问题,并且您有结果,然后检索数据(或数据的
列表
),最后一步是验证数据是否为空或数据的
列表
是否为空

改编自的代码,用于显示建议的解决方案

PreparedStatement prodsQuery =
    con.prepareStatement("SELECT * FROM products where ID=?");
prodsQuery.setInt(1,removeName);
ResultSet rs = prodsQuery.executeQuery();
  • 假设只有一个结果可以得到:

    //also assuming you will set the results in a Data class (yes, this can be replaced)
    Data data = null;
    if (rs.next()) {
        //logic to retrieve data...
        data = new Data();
        data.setSomething(rs.get(1));
        //more and more code to fill the data...
    
        //because it looks that you need it as String (wonder why you return a String as well)
        return data.toString();
    }
    //note: I use an else statement to check if indeed there were no results at all
    //else statement added using a line separator for code explanation purposes
    else {
        m = "ID not found.";
        return m;
    }
    
  • 假设有一个要获得的结果列表:

    //also assuming you will set the results in a Data class (yes, this can be replaced)
    List<Data> dataList = new ArrayList<Data>();
    while (rs.next()) {
        //logic to retrieve data...
        Data data = new Data();
        data.setSomething(rs.get(1));
        //more and more code to fill the data...
    
        //because it looks that you need it as String (wonder why you return a String as well)
        dataList.add(data);
    }
    //in this case, there's no validation in order to know if there's any result
    //the validation must be in the client of this class and method checking if
    //the result list is empty using if(!List#isEmpty) { some logic... }
    return dataList;
    
    //还假设您将在数据类中设置结果(是的,可以替换)
    List dataList=new ArrayList();
    while(rs.next()){
    //检索数据的逻辑。。。
    数据=新数据();
    数据集(rs.get(1));
    //越来越多的代码填充数据。。。
    //因为看起来您需要它作为字符串(不知道为什么还要返回字符串)
    dataList.add(数据);
    }
    //在这种情况下,没有验证来知道是否有任何结果
    //验证必须在此类和方法的客户端中检查
    //使用if(!list#isEmpty){some logic…}时,结果列表为空
    返回数据列表;
    

  • 我试过这样做,但仍然存在同样的问题,在这种情况下,它将跳过代码的其余部分。非常感谢。无论如何,我会越来越多地寻找它。我试图使用preparedStatement(您的代码)不起作用,而且我也使它与preparedStatement有所不同,但仍然不起作用:/。它不会。。。如果可能的话,给我发代码。。。我会给你推荐一条好的道路……:)非常感谢你们,我会做的只是第一次我会读,最后张贴我的问题的答案,耐心,如果我即使不会解决它,我会发送给你们。Thank.go gor检查像
    if(rs.next()){//do here}else{m=“Id not found”return m;}
    谢谢,我将阅读有关它的内容,但它仍将从我的数据库中删除所有内容(数量)。若那个条件不存在,它将很好地从数量中只删除我一个(或者只删除我想要的数量),但若并没有条件,用户将不知道是否有那个ID。
    //also assuming you will set the results in a Data class (yes, this can be replaced)
    Data data = null;
    if (rs.next()) {
        //logic to retrieve data...
        data = new Data();
        data.setSomething(rs.get(1));
        //more and more code to fill the data...
    
        //because it looks that you need it as String (wonder why you return a String as well)
        return data.toString();
    }
    //note: I use an else statement to check if indeed there were no results at all
    //else statement added using a line separator for code explanation purposes
    else {
        m = "ID not found.";
        return m;
    }
    
    //also assuming you will set the results in a Data class (yes, this can be replaced)
    List<Data> dataList = new ArrayList<Data>();
    while (rs.next()) {
        //logic to retrieve data...
        Data data = new Data();
        data.setSomething(rs.get(1));
        //more and more code to fill the data...
    
        //because it looks that you need it as String (wonder why you return a String as well)
        dataList.add(data);
    }
    //in this case, there's no validation in order to know if there's any result
    //the validation must be in the client of this class and method checking if
    //the result list is empty using if(!List#isEmpty) { some logic... }
    return dataList;