【问题标题】:Why this code don't show all values in the database?为什么这段代码不显示数据库中的所有值?
【发布时间】:2014-04-22 16:59:18
【问题描述】:

为什么这段代码没有显示数据库中的所有值?

public Article searchAllArticles(){

    try {

        DbCnx cnx = new DbCnx();
        conn = cnx.connection_a_postgresql();
        String sql = "SELECT * FROM my_caisse.articles";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ResultSet resultat = preparedStatement.executeQuery();

        while (resultat.next()) {
            Article article =new Article();
            article.setId_article(resultat.getLong("id_article"));
            article.setDesi_article(resultat.getString("desi_article"));
            article.setPrix_unitaire(resultat.getDouble("prix_unitaire"));
            article.setStock_en_cours(resultat.getInt("stock_en_cours"));
            article.setImg_article(resultat.getString("image_article"));
            afficherArticles(article); //show all the values using System.out.println
            return article;
            //i have 5 lines in the table "my_caisse.articles" and i only have one result (only the first line)
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

【问题讨论】:

  • 因为您在while 循环内调用return。在我所知道的大多数编程语言中,这将在第一个之后停止读取结果。
  • 因为这就是return 语句的作用:它返回一个值。如果您返回一个值,则该方法内不会发生任何其他事情。如果你想返回多个结果,你需要返回一个集合。
  • 又一个例子说明为什么正确的代码格式很重要。
  • @GordonLinoff 你知道哪些语言那样工作?
  • @AnthonyGrist 。 . .在某些面向对象语言中定义迭代器时,可以返回一个值,然后继续执行。

标签: java sql database select


【解决方案1】:

因为你在找到第一篇文章时用return article 退出循环。

【讨论】:

    【解决方案2】:

    return article; 在 while 循环内是问题所在。将其移至return null; 所在方法的最后一条语句,否则,这将导致在while 循环本身的第一次迭代期间执行返回到调用方法。请看下面的代码

     Article article = null;
      while (resultat.next()) {
            article =new Article();
            article.setId_article(resultat.getLong("id_article"));
            article.setDesi_article(resultat.getString("desi_article"));
            article.setPrix_unitaire(resultat.getDouble("prix_unitaire"));
            article.setStock_en_cours(resultat.getInt("stock_en_cours"));
            article.setImg_article(resultat.getString("image_article"));
            afficherArticles(article); //show all the values using System.out.println
           // return article; remove this line (commented)
            //i have 5 lines in the table "my_caisse.articles" and i only have one result (only the first line)
            }
        }
        catch (Exception e) {
            e.printStackTrace();
    
        }
        return article;
    

    我认为您还需要将Article 的新实例添加到list。但是,你只是把它们放在空中。您还需要将方法的返回类型更改为List<Article>

    【讨论】:

    • 您的代码将无法工作,因为article 在循环外不可见。
    • 不客气!请通过接受任何对您有很大帮助的答案来结束问题:) 单击答案左侧的勾号!这有助于路人知道解决问题的答案
    【解决方案3】:

    因为您在 while 循环内返回。并且您的代码期望单个对象(文章)返回。您应该创建一个列表,填充数据并返回它。还将返回类型更改为对象列表(文章列表)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      相关资源
      最近更新 更多