Java JDBC错误:在结果集开始之前

Java JDBC错误:在结果集开始之前,java,mysql,jdbc,Java,Mysql,Jdbc,我在JavaEclipse中有一条错误消息。我在MySql中有一个数据库,它有字符串user\u name、int id\u time、int id\u desk、int user\u password。我想使用 公共ArrayList showReservation(字符串用户名)引发SQLException{ // array list keeps the information ArrayList<String> showReservation = new Ar

我在JavaEclipse中有一条错误消息。我在MySql中有一个数据库,它有字符串user\u name、int id\u time、int id\u desk、int user\u password。我想使用

公共ArrayList showReservation(字符串用户名)引发SQLException{

    // array list keeps the information
    ArrayList<String> showReservation = new ArrayList<String>();
    try{

        String getReservationSql = "SELECT * FROM reservation " 
                +"WHERE user_name ='" + user_name + "';";
        rs = stmt.executeQuery(getReservationSql);

        // first element of the array list keeps user name
        showReservation.add("" + rs.getString("user_name")); 

        // second element of the array list keeps id of the desk which is selected by user
        showReservation.add("" + rs.getInt("id_time")); 

        // third element of the array list keeps id of the time interval which is selected by user
        showReservation.add("" + rs.getInt("id_desk"));  

        // forth element of the array list keeps user's password which is generated automatically in a controller class
        showReservation.add("" + rs.getInt("user_password")); 

    }catch (SQLException ex) {
        System.out.println("Error: " + ex.getMessage());
    }
    return showReservation;
}
//数组列表保存信息
ArrayList showReservation=新建ArrayList();
试一试{
String getReservationSql=“从保留中选择*
+“其中user_name='”+user_name+“;”;
rs=stmt.executeQuery(getReservationSql);
//数组列表的第一个元素保留用户名
showReservation.add(“+rs.getString(“用户名”));
//数组列表的第二个元素保存由用户选择的桌子的id
showReservation.add(“+rs.getInt(“id_time”));
//数组列表的第三个元素保留用户选择的时间间隔id
showReservation.add(“+rs.getInt(“id_desk”));
//数组列表的第四个元素保留在控制器类中自动生成的用户密码
showReservation.add(“+rs.getInt(“用户密码”));
}catch(SQLException-ex){
System.out.println(“错误:+ex.getMessage());
}
退房预订;
}
但当我运行这段代码时,我得到一个错误:在结果集开始之前。 如何修复此错误?
谢谢

您永远不会前进到
结果集中的第一个结果。用于前进到下一条记录,当没有更多记录时,该记录返回
false

rs = stmt.executeQuery(getReservationSql);
while (rs.next())
{
   // Make your calls to getString and getInt here
}

您永远不会前进到
ResultSet
中的第一个结果。用于前进到下一条记录,当没有更多记录时,该记录返回
false

rs = stmt.executeQuery(getReservationSql);
while (rs.next())
{
   // Make your calls to getString and getInt here
}
在尝试调用任何
ResultSet
getter方法之前使用

rs = stmt.executeQuery(getReservationSql);
if (rs.next()) {
   ...
}

旁白:在尝试调用< <代码>结果集> /COD>吸气剂方法

之前,请考虑< /P> < P>使用。
rs = stmt.executeQuery(getReservationSql);
if (rs.next()) {
   ...
}
撇下:考虑

检查这个帖子检查这个帖子