Mario Verstraeten
Mario Verstraeten

Reputation: 11

SQLException in java: No value specified for parameter 2

I'm having trouble with solving this problem :/. When I run my project I encounter the error code: no value specified for parameter 2, even though all parameters should be filled in.

public Field geefVeld(int x, int y, String spelname, int spelbordnr) {
    Field field = null;
    //connection.close();
    try (Connection connectie = DriverManager.getConnection(Connectie.JDBC_URL)) {
        PreparedStatement query = connectie.prepareStatement("SELECT symbool FROM field WHERE XCoord = ? and YCoord = ? and Spelbord_spel_Spelname = ? and Spelbord_VolgordeID = ?");
        query.setInt(1, x);
        query.setInt(2, y); 
        query.setString(3, spelname); 
        query.setInt(4, spelbordnr);

        try (ResultSet rs = query.executeQuery()) {
            field = new Field(rs.getString(3).charAt(0));
        }
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return field;
}    

When debugging i can see that x = 0, y = 0, spelname = "Soraka", spelbordnr = 1 (Wich is all totally fine, because these values excist in my database)

The project stops at "query.setInt(2, y);" when debugging.

I apoligize if I'm overlooking something silly, but I've been searching for hours and I can't seem to find it :(

Any response is welcome :p

Upvotes: 1

Views: 1290

Answers (2)

Mario Verstraeten
Mario Verstraeten

Reputation: 11

Thx for the response, apparently there was nothing wrong wit the code ( or maybe there still is and I'm not aware of it :/), but there was a problem with NetBeans. The "clean build" function doesn't really work for some reason, so I needed to manually delete the build ( I hope I'm making sense :p )

Upvotes: 0

O. Jones
O. Jones

Reputation: 108839

You are not retrieving your resultset correctly in several respects. First, you need to use the next() method to fetch the row or rows in the result. Second, you are calling getString(3) where you only have one column (symbol) in your resultset. Third, don't forget to close your resultset. Finally, there's something strange about the way you wrote your try.

try {
    ResultSet rs = query.executeQuery();
    while(rs.next()) {
         field = new Field(rs.getString(1).charAt(0));
    }
    rs.close();
}

I don't know if that will fix the parameter-setting issue but it is worth a try.

Upvotes: 1

Related Questions