【问题标题】:ResultSet is only returning one row while database has more rows java/sqlResultSet 只返回一行,而数据库有更多行 java/sql
【发布时间】:2020-09-06 18:08:15
【问题描述】:

我面临的问题是我的结果集只返回一个行值,而有三个。 我找不到问题出在哪里:

  public void eventInfo() throws SQLException
    {
     openConnection();
    PreparedStatement ps1=null;

    String query1="select * from events";

    try {
        ps1 = con.prepareStatement(query1);
    } catch (SQLException ex) {
        Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
    }





            EventData e= new EventData();
            e.edata.clear();
            Events obj = new Events();

    ResultSet s=ps1.executeQuery();
    int i=0;

            System.out.println(s);

    if(s.next())
    {
//                        obj.setId(s.getInt("id"));
//          obj.setTitle(s.getString("title"));
//          obj.setDescription(s.getString("description"));
//          
//          obj.setType(s.getString("type"));
//          obj.setOrganization(s.getString("organization"));
 //         obj.setPlace(s.getString("place"));
 //         obj.setTime(s.getString("time"));
 //         obj.setDate(s.getString("date"));
 //         obj.setRepetetion(s.getString("repetetion"));
 //         obj.setParticipants(s.getInt("participants"));
 //         obj.setLimit(s.getInt("elimit"));

                    System.out.println(i);
                    System.out.println(s.getString("title"));
                    i++;

                 //   e.edata.add(obj);


    }


    if(con!=null)
    {
        con.close();
    }

    }

数据库表如 enter image description here

输出看起来像 enter image description here

谁能帮我解决这个问题?

【问题讨论】:

    标签: java sql


    【解决方案1】:

    你应该不会感到惊讶

    if(s.next())
    

    不是一个循环,而是一个条件。如果你想连续访问结果集,你必须使用循环

    while (s.next())
    

    【讨论】:

    • 非常感谢...我只是忽略了...非常感谢
    【解决方案2】:
    while (s.next()) {...}
    

    ...应该为你做。这将遍历所有行。

    【讨论】:

    • 谢谢先生...我现在得到了结果
    猜你喜欢
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 2015-09-13
    • 2018-05-10
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    相关资源
    最近更新 更多