我有一个由4个字符串和2个整数组成的简单表(其中一个是ID)。
我使用一个特殊函数来获取该表中的所有行:
public Booking[] displayAllBookings() throws ClassNotFoundException, SQLException{
Connection con = connect.getConnection();
PreparedStatement counter = (PreparedStatement) con.prepareStatement("select count(*) from bookings");
ResultSet count = counter.executeQuery();
Booking[] array = new Booking[count.getInt(1)];
String name = null,surname = null,begindate = null,enddate = null;
int persons = 0,i=0;
PreparedStatement posted = (PreparedStatement) con.prepareStatement("SELECT * FROM bookings");
result = posted.executeQuery();
while (result.next()){
begindate = result.getString("begindate");
enddate = result.getString("enddate");
name = result.getString("Name");
surname = result.getString("Surname");
persons = result.getInt("persons");
Booking temp = new Booking(begindate,enddate,name,surname,persons);
array[i++]=temp;
}
return array;
}
但是当我执行它时,我得到了这个例外:
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:790)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2472)
at DataBase.DataCommunicatorBooking.displayAllBookings(DataCommunicatorBooking.java:59)
at DataBase.main.displayAll(main.java:125)
at DataBase.main.main(main.java:71)
我已经尝试查找此异常,但找不到与我的问题相对应的任何内容。
答案 0 :(得分:1)
您正在尝试获取 maxPersons 列的值,该列甚至未在表格中定义...您肯定是 人员 强>
name = result.getString("Name");
maxPersons = result.getInt("persons");
Room temp = new Room(name,maxPersons);
答案 1 :(得分:1)
问题在于
PreparedStatement counter = (PreparedStatement) con.prepareStatement("select count(*) from bookings");
ResultSet count = counter.executeQuery();
所以我使用ArrayList
就像这样:
public Booking[] displayAllBookings() throws ClassNotFoundException, SQLException{
Connection con = connect.getConnection();
ArrayList<Booking> bookings = new ArrayList<Booking>();
String name = null,surname = null,begindate = null,enddate = null;
int persons = 0,i=0;
PreparedStatement posted = (PreparedStatement) con.prepareStatement("SELECT * FROM bookings");
result = posted.executeQuery();
while (result.next()){
begindate = result.getString("begindate");
enddate = result.getString("enddate");
name = result.getString("Name");
surname = result.getString("Surname");
persons = result.getInt("persons");
Booking temp = new Booking(begindate,enddate,name,surname,persons);
bookings.add(temp);
}
return bookings.toArray(new Booking[bookings.size()]);
}
这解决了问题;
答案 2 :(得分:0)
这是因为“maxPersons”栏不存在,你可能会选择“人”