JDBC-ResultSet is closed in while-Loop

后端 未结 3 580
执笔经年
执笔经年 2020-12-12 07:29

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

相关标签:
3条回答
  • 2020-12-12 07:56

    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 one ResultSet per Statement.

    See the java docs.

    0 讨论(0)
  • 2020-12-12 07:57

    Your problem's answer comes from the javadoc:

    By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects.

    0 讨论(0)
  • 2020-12-12 08:04

    The problem is that the Statement can only maintain a single group of ResultSets per executed statement. Since you share the same Statement stmt for your two methods, in alleAbrufen the Statement executes another statement, which will break the reference to the prior ResultSet.

    The best solution for this case is to create a Statement per statement execution. This is, every method should contain its unique Statement and related ResultSets.

    public LinkedList<Athlet> alleAbrufen () throws SQLException {
        LinkedList<Athlet> alleAthleten = new LinkedList<Athlet>();
        String abrufenAthleten = "SELECT * FROM Athlet ORDER BY athlet_id";
        //here
        Statement stmtAlleAbrufen = con.createStatement();
        ResultSet athleten_rs = stmtAlleAbrufen.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>();
        //here again, but since you need to use parameters in your query
        //use PreparedStatement instead
        //note that I commented the current query
        //String selectLeistungen = "SELECT * FROM Leistung WHERE athlet_id="+athlet_id;
        //this is how a query with parameters look like
        String selectLeistungen = "SELECT * FROM Leistung WHERE athlet_id=?";
        //the connection prepares the statement
        PreparedStatement pstmt = con.prepareStatement(selectLeistungen);
        //then we pass the parameters
        pstmt.setInt(1, athlet_id);
        ResultSet rs = pstmt.executeQuery();
        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;
    }
    

    Don't forget to close the resources, Statement and ResultSet, after using them.

    0 讨论(0)
提交回复
热议问题