java - ResultSet 只返回一行,而数据库有更多行 java/sql
问题描述
我面临的问题是我的结果集只返回一个行值,而有三个。我找不到问题出在哪里:
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();
}
}
数据库表,如 在此处输入图像描述
输出看起来像 在此处输入图像描述
任何人都可以帮助我吗?
解决方案
你不应该感到惊讶
if(s.next())
不是循环而是条件。如果要连续访问结果集,则必须使用循环
while (s.next())
推荐阅读
- reactjs - 为什么 Reactjs 必须重用 HOC 中的代码?
- xaml - UWP xaml 逻辑:
- spring - Spring Security Java 配置:如何通过逻辑或组合需求?
- java - windows TAPI api通过java
- database - 如何为使用 JPA 嵌入的 Apache Derby 提供数据库凭据?
- r - 将缺少的 XML 标记读取为 0 或 NA?
- json - 使用 Outlook rest api 更新事件时出错
- javascript - 根据不同的数据列更新悬停工具提示
- android - 如何在Xamarin Android中更改seekbar的细线及其圆圈的颜色?
- r - 计算数据框中元素的数量