The record from database can't be retrieved
I want to take record from database to my table in jsp page, but when run it, i have an error that say java.sql.SQLException: After end of result set, here is my code :
public String viewDataRegistrasi() throws SQLException{
String address="";
String query = "select*from pegawai";
System.out.println(query);
ResultSet rs = database.getInstance().executeSelectQuery(query);
ArrayList DataList = new ArrayList();
pegawai viewdata;
String idpeg = "";
while(rs.next()){
viewdata = new pegawai();
viewdata.setNik(rs.getString(1));
viewdata.setNama(rs.getString(2));
viewdata.setTempatlahir(rs.getString(3));
viewdata.setTanggallahir(rs.getString(4));
viewdata.setEmail(rs.getString(5));
viewdata.setAlamat(rs.getString(6));
viewdata.setPosisi(rs.getString(7));
DataList.add(viewdata);
}
request.setAttribute("viewRegistrasi", DataList);
idpeg = rs.getString(1) ;
address = "ViewPegawai.jsp";
return address;
}
can somebody help me please?? :'(
Re: The record from database can't be retrieved
Thread moved out of 'What's wrong with my code', and for future reference please wrap code in the code tags.
Read the API for ResultSet, especially the next() method. If it returns false (eg the cursor is positioned after the last row), any attempt to get a value will throw an SQLException. In your case, you loop over the set, then try to call getString on the ResultSet after it has returned false (eg the cursor is after the last row)
Re: The record from database can't be retrieved
Hi, the result pointer is pointing to "after last record " after executing the while(rs.next()). But you are calling the idpeg = rs.getString(1) after while loop. There is no record in result set at idpeg = rs.getString(1) . That's why it raising SqlException.