我在Java eclipse中有一条错误消息。我在MySql中有一个数据库,它有colomns String user_name,int id_time,int id_desk,int user_password。我希望使用
获得一行的列数据public ArrayList showReservation(String user_name)抛出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;
}
但是当我运行此代码时,我收到一个错误:在开始结果集之前。 我该如何解决这个错误? 谢谢
答案 0 :(得分:2)
您永远不会前进到ResultSet
中的第一个结果。使用the next()
method前进到下一条记录,当没有更多记录时返回false
。
rs = stmt.executeQuery(getReservationSql);
while (rs.next())
{
// Make your calls to getString and getInt here
}
答案 1 :(得分:2)
在尝试调用任何ResultSet
getter方法之前使用next
rs = stmt.executeQuery(getReservationSql);
if (rs.next()) {
...
}
除此之外:考虑Using Prepared Statements