【问题标题】:Newbie pointer rs.next新手指针 rs.next
【发布时间】:2013-04-22 03:21:59
【问题描述】:

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

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

【问题讨论】:

    标签: java sql pointers conditional-statements resultset


    【解决方案1】:

    首先,您的方法容易受到 SQL 注入的攻击。请前往 PreparedStatement。
    Look at this simple example for using PreparedStatement

    你应该这样做:

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

    【讨论】:

    • 谢谢您,我将阅读它,但它仍在从我的数据库中删除所有内容(数量)。如果该条件不存在,它将很好地从数量中只删除我一个(或仅删除我想要多少),但没有条件用户将不知道是否有该 ID。
    【解决方案2】:

    在你的情况下,你可以去PreparedStatement 避免SQL-Injection 问题。

      PreparedStatement prodsQuery= con.prepareStatement("SELECT * FROM products where ID=?");
      prodsQuery.setInt(1,removeName);
      ResultSet rs = prodsQuery.executeQuery();
      if(!rs.next())
      {
            m = "ID not found.";
            return m;
       }
    

    【讨论】:

    • 我试过这样,但仍然存在同样的问题,它会在该条件后跳过其余代码。谢谢!无论如何,我会越来越多地寻找它。
    • 我尝试使用preparedStatement(你的代码)不起作用,而且我也用prepared statement做了一些不同但仍然不起作用:/.
    • 它不会...如果可能的话把代码发给我...我会建议你前进的好方法... :)
    • 非常感谢您,我会先阅读,最后发布我的问题的答案,耐心等待,如果我无法解决,我会发送给您。谢谢。
    • 去看看if(rs.next()){ //do here }else{ m="Id not found"; return m; }
    【解决方案3】:

    问题是您正在阅读第一个结果以了解是否至少有一个结果,然后尝试使用下一个结果并错过第一个结果(改编自您的问题描述)。我解释了这是如何工作的here

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

    代码改编自 Naveen's answer 以显示建议的解决方案

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

      //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;
      }
      
    2. 假设有一个要获取的结果列表:

      //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;
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      相关资源
      最近更新 更多