JDBC错误:在开始结果集之前 [英] JDBC Error: Before start of result set
问题描述
我在Java eclipse中有一条错误消息。我在MySql中有一个数据库,它有colomns String user_name,int id_time,int id_desk,int user_password。我希望使用
I have an error message in Java eclipse. I have a database in MySql and it has colomns String user_name, int id_time, int id_desk, int user_password. I want to one row's columns data with using
一行的列数据public ArrayList showReservation(String user_name)抛出SQLException {
public ArrayList showReservation(String user_name) throws 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;
}
但是当我运行此代码时出现错误:在开始结果集之前。
如何修复此错误?
谢谢
but when i run this code i get an error: Before start of result set. how can i fix this error? thanks
推荐答案
使用 next
$ c> ResultSet getter方法
Use next
before attempting to call any of the ResultSet
getter methods
rs = stmt.executeQuery(getReservationSql);
if (rs.next()) {
...
}
除此之外:考虑使用准备好的陈述
这篇关于JDBC错误:在开始结果集之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!