Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLException - invalid cursor state

I am coding a simple CRUD Application in java and I have a Method to select Produkts that go with bills.

Here is my code:

public Rechnung selectProdukte(int target){
    int tempProdukt;
    int tempAnzahl;
    try {
        PreparedStatement ps1=hsqlmanager.getConnection().prepareStatement("SELECT produkt, anzahl from gekauftes_produkt " +
                "WHERE rechnung= " + target + ";");
        //ps1.setInt(1, target);
        //Query 1wird executiert

        PreparedStatement ps2 = hsqlmanager.getConnection().prepareStatement("SELECT * FROM rechnung WHERE id= " + target + ";");
        //ps2.setInt(1, target);
        ResultSet rs1 = ps1.executeQuery();
        ResultSet rs2 = ps2.executeQuery();
        Rechnung erg=new Rechnung(); 
        erg.setId(rs2.getInt(1));
        erg.setDatum(rs2.getDate(2));
        erg.setSumme(rs2.getDouble(3));
        while(rs1.next()){
            tempProdukt=rs1.getInt(1);
            tempAnzahl=rs1.getInt(2);
            erg.addGekauftTupel(tempProdukt, tempAnzahl);
        }
        ps1.close();
        ps2.close();
        return erg;
    } catch(Exception e) {
        log.error("Fehler in DAO Rechnung  - selectProdukte: " + e);
    }
    return null;
}

When I press the Button to execute the code I get:

java.sql.SQLException: invalid cursor state: identifier cursor not positioned on row in UPDATE, DELETE, SET, or GET statement: ; ResultSet is positioned before first row

I checked the db and all the tables and entities exist. So my question is:

What does that mean?

I appreciate your answer!!!

PS.: I am using hsql db!

like image 814
maximus Avatar asked Dec 27 '22 16:12

maximus


1 Answers

you have not called rs2.next() before accessing erg.setId(rs2.getInt(1));

like image 182
shyam Avatar answered Jan 13 '23 04:01

shyam