仅从 table 中获取第一行 jdbc

Only getting first row from table in jdbc

我只从 table 的 JDBC 调用中得到第一行。

下面是我的代码

import java.util.*;
import java.sql.*;

public class TrainManagementSystem {
    
    public ArrayList <Train> viewTrain (String coachType, String source, String destination){
        
        Connection myCon = null;
        PreparedStatement myStat = null;
        ResultSet myRes = null;
        ArrayList<Train> result = new ArrayList<>();
        try{
            myCon = DB.getConnection();
            myStat = myCon.prepareStatement("select * from train where source = ? and destination = ? and ? > 0");
            myStat.setString(1, source);
            myStat.setString(2, destination);
            myStat.setString(3, coachType);
            
            myRes = myStat.executeQuery();
            
            while(myRes.next()){
                int train_number = myRes.getInt("train_number");
                String train_name = myRes.getString("train_name");
                String source1 = myRes.getString("source");
                String destination1 = myRes.getString("destination");
                int ac1 = myRes.getInt("ac1");
                int ac2 = myRes.getInt("ac2");
                int ac3 = myRes.getInt("ac3");
                int sleeper = myRes.getInt("sleeper");
                int seater = myRes.getInt("seater");
                System.out.println(myRes.getString("train_name"));
                Train train = new Train(train_number, train_name, source1, destination1, ac1, ac2, ac3, sleeper, seater);
                result.add(train);
                return result;
            }
        }catch(Exception e){
            System.out.println(e);
        }
        return result;
    }

}

我在 JDBC 中只得到结果集中的第一行。查询未检索第二行。 我已将我的条目附在 table 中。 当输入为

为什么不转到下一行?当我尝试删除 coachtype 并仅搜索 Howrah 和 Dehradun 之间的火车时,它会给我结果 Dehradun Mail 而结果也应该添加 Doon Express。

因为您的 while 循环中有一个 return result;。 将它向下移动到循环外