Dompet Dompet - 1 month ago 6
Java Question

Always getting error "after end Result set" and all result set already close

I want to make a Java application that, when I click button, it can open Google Maps in browser. Location, latitude, and longitude I can take from a MySQL database. But if I am running that class, "After end of result" always appears even though all results set already close. This is my code:

while( {
String getKodeLokasi = rs.getString("kode_lokasi");
if ((kode_lokasi.equals(getKodeLokasi))){
check = true;

if (check) {
String latitude = "SELECT latitude FROM data_lokasi WHERE kode_lokasi = '"+kode_lokasi+"'";
String getLat = rs.getString("latitude"); <<<<----- After end of result set

String longtitude = "SELECT longtitude FROM data_lokasi WHERE kode_lokasi = '"+kode_lokasi+"'";
String getLong = rs.getString("longtitude");

String lokasi = "SELECT lokasi FROM data_lokasi WHERE kode_lokasi = '"+kode_lokasi+"'";
String getLokasi = (rs.getString("lokasi")).replace(" ", "+");

Desktop.getDesktop().browse(new URI("'"+getLokasi+"'/@'"+getLat+"','"+getLong+"',17z/"));
} else {
JOptionPane.showMessageDialog(this, "Can't find code!");

Whats wrong with my code? Thanks before and please help me :')


You scroll all the way to the end of your ResultSet. At that point, it is no longer on a valid row, so there are no column values to read. In particular, you cannot read any values from the row for which kode_lokasi.equals(getKodeLokasi) evaluated to true.

If you expect no more than one row, then change the while loop into an if block, so that is invoked only once. If you need to accommodate multiple result rows, then you probably need to break out of the loop when you find the one you're looking for, so that is not invoked on the ResultSet again.

The theme here is that if you want to read values from the result row that satisfies your condition, then after finding that row you must not move on to another.