我数据库中有两条符合条件的记录,为什么只能查出来一条呢,代码如下,请朋友们帮忙!!!

tearsmo57 2010-10-27 12:57:37
package com.flight.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import com.flight.bean.flight;

public class searchflightDao {
public ArrayList<flight> findById(String start_city,String arrival_city){
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList<flight> fb= new ArrayList<flight>();


try {
conn = DBManager.getConnection();
String sql = "select * from flights where start_city = ? and arrival_city = ?";
stmt =conn.prepareStatement(sql);
stmt.setString(1, start_city);
stmt.setString(2, arrival_city);
rs = stmt.executeQuery();//此处一定不要传入sql
if(rs.next()){
flight fa=new flight();
fa = new flight();//创建对象
fa.setId(rs.getInt("id"));
fa.setFlight_id(rs.getString("flight_id"));
fa.setStart_city(rs.getString("start_city"));
fa.setArrival_city(rs.getString("arrival_city"));
fa.setStart_time(rs.getString("start_time"));
fa.setArrival_time(rs.getString("arrival_time"));
fa.setKtype(rs.getString("ktype"));
fa.setAlltime(rs.getString("alltime"));
fa.setEconomy_price(rs.getString("economy_price"));
fa.setBusiness_price(rs.getString("business_price"));
fa.setFirstclass_price(rs.getString("firstclass_price"));
fa.setEconomy_seataccount(rs.getString("economy_seataccount"));
fa.setBusiness_seataccount(rs.getString("business_seataccount"));
fa.setFirstclass_seataccount(rs.getString("firstclass_seataccount"));
fa.setDatetime(rs.getString("datetime"));
fb.add(fa);

}
}

catch (SQLException e) {
e.printStackTrace();
} finally{
DBManager.close(rs, stmt, conn);//7. 关闭数据库连接
}
//System.out.println(fb);
return fb;//返回
}

}
我在底部用输出语句做了下测试,就能查到一条结果!!
...全文
81 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
tearsmo57 2010-10-27
粗心可以改,呵呵,互相学习吧!
回复
niemengwem 2010-10-27
发现我挺粗心的 ,郁闷 还是个门外汉
回复
tearsmo57 2010-10-27
嘿嘿,谢谢朋友了,我也刚刚发现,刚改过来了,好使了,呵呵
回复
zbo 2010-10-27
因为你用if(rs.next())
就是只执行了一次。
应该用while(rs.next())
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-10-27 12:57
社区公告
暂无公告