I'm having a really bad time with a ResultSet, which is closed within a while-Loop for iterating this ResultSet. I have know the exact line in which the ResultSet is closed, but i have no idea why.
public LinkedList<Athlet> alleAbrufen () throws SQLException {
LinkedList<Athlet> alleAthleten = new LinkedList<Athlet>();
String abrufenAthleten = "SELECT * FROM Athlet ORDER BY athlet_id";
ResultSet athleten_rs = stmt.executeQuery(abrufenAthleten);
while (athleten_rs.next()) {
long id = athleten_rs.getInt(1);
String name = athleten_rs.getString(2);
LinkedList<Leistung> alleLeistungen = alleAbrufen((int) (id)); //after this line the ResultSet gets closed
alleAthleten.add(new Athlet(id, name, alleLeistungen));
}
return alleAthleten;
}
public LinkedList<Leistung> alleAbrufen(int athlet_id) throws SQLException {
LinkedList<Leistung> alleLeistungen = new LinkedList<Leistung>();
String selectLeistungen = "SELECT * FROM Leistung WHERE athlet_id="+athlet_id;
ResultSet rs = stmt.executeQuery(selectLeistungen);
while (rs.next()) {
long id = rs.getInt(1);
String bezeichnung = rs.getString(2);
String datum = rs.getString(3);
double geschwindigkeit = rs.getDouble(4);
boolean selectedForSlopeFaktor = rs.getBoolean(5);
int strecke_id = rs.getInt(7);
long longAthlet_id = (long) athlet_id;
Leistung leistung = new Leistung(strecke_id, longAthlet_id, bezeichnung, datum, geschwindigkeit);
leistung.setLeistungID(id);
leistung.setIsUsedForSlopeFaktor(selectedForSlopeFaktor);
alleLeistungen.add(leistung);
}
return alleLeistungen;
}
I marked the line after which the ResultSet
is closed with a comment. Alle
other methods, constructors, etc used in the above example are tested an working properly. Does anyone have a clue why calling the second method closes the ResultSet
in the first method?
Your problem's answer comes from the javadoc:
The problem is that the
Statement
can only maintain a single group ofResultSet
s per executed statement. Since you share the sameStatement stmt
for your two methods, inalleAbrufen
theStatement
executes another statement, which will break the reference to the priorResultSet
.The best solution for this case is to create a
Statement
per statement execution. This is, every method should contain its uniqueStatement
and relatedResultSet
s.Don't forget to close the resources,
Statement
andResultSet
, after using them.Is your
Statement
a class variable and you are using the same for both the queries? Is yes, it's wrong. You can have only oneResultSet
perStatement
.See the java docs.