mardi 22 mai 2018

Exceptions at while(rs.next())

I have this piece of code in the backend of a web application. It sometimes throws a NullPointerException at while(rs.next()), sometimes it throws SQLException Operation not allowed after ResultSet closed also at while(rs.next()), but sometimes it doesn't throw any error. Anyone knows what is the problem?

public HashSet<LocalDate> getAvailableDates(long listingNumber) throws SQLException{
    Statement statement = null;
    String getDatesAvail = "SELECT AVAILDATE FROM AVAILABILITY WHERE LISTINGNUM = " +listingNumber +";";
    HashSet<LocalDate> avail = new HashSet<LocalDate>();
    ResultSet rs = null;
    try {
        connect(); // Open dbConnection
        statement = dbConnection.createStatement();
        rs = statement.executeQuery(getDatesAvail);
        System.out.println(getDatesAvail);
        if (rs == null) {
            System.out.println("Result set is null for avail");
        }
        while(rs.next()) {
            avail.add(LocalDate.parse(rs.getString("AVAILDATE"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
    } catch (SQLException e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
        throw new SQLException();
    } finally {
        if (rs != null) {
            rs.close();
        }
        if (statement != null) {
            statement.close();
        }
        if (dbConnection != null) {
            dbConnection.close();
        }
    }
    return avail;
}




Aucun commentaire:

Enregistrer un commentaire