1

Hello I keep getting this error while using API URL to get specific parking spot number list of reservations, this is the error:

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement cannot be cast to java.sql.ResultSet

here is my method that sends a query for specific parking space ID:

    public List<Book> getSpecBook(int id) throws Exception {
    List<Book> books = new ArrayList<>();

    //int idInt = Integer.parseInt(id);

    Connection myConn = null;
    PreparedStatement myStmt = null;
    ResultSet myRs = null;

    try {
        myConn = dataSource.getConnection();

        // create sql statement
        String sql = "SELECT * FROM `rezerwacje_miejsc` WHERE `NR_MIEJSCA`=?";

        myStmt = myConn.prepareStatement(sql);
        myStmt.setInt(1, id);

        myRs = myStmt.executeQuery();

        while (myRs.next()) {
            // retrive data from result set row
            int placeNo = ((ResultSet) myStmt).getInt("NR_MIEJSCA");
            Date start = ((ResultSet) myStmt).getDate("START");
            Date end = ((ResultSet) myStmt).getDate("KONIEC");
            String userName = ((ResultSet) myStmt).getString("IMIE_NAZWISKO");
            int phone = ((ResultSet) myStmt).getInt("TELEFON");

            // create new temporary Book object
            Book tempBook = new Book(placeNo, start, end, userName, phone);

            // add it to our list of Books
            books.add(tempBook);
        }

        return books;

    } finally {
        // clean up JDBC objects
        close(myConn, myStmt, myRs);
    }
}

and here is my API class:

package com.pbs.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pbs.web.jdbc.ParkingBookSystem.Book;
import com.pbs.web.jdbc.ParkingBookSystem.BookDbUtil;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

@Path("books")
@Produces(MediaType.APPLICATION_JSON)
public class BookingRestController {
    private BookDbUtil bookDbUtil = new BookDbUtil();

    @GET
    @Path("/")
    public Response getAllBooks() throws Exception {
        List<Book> books = bookDbUtil.getBooks();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(books);
        return Response.ok().entity(json).build();
    }

    @GET
    @Path("/{id}")
    public Response getSpecBook(@PathParam("id") int id) throws Exception {
        List<Book> books = bookDbUtil.getSpecBook(id);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(books);
        return Response.ok().entity(json).build();
    }
}

I'm not able to find the issue.

1 Answer 1

1

You're writing lines like

       int placeNo = ((ResultSet) myStmt).getInt("NR_MIEJSCA");

This is evidently where you are getting the error. The error says that a statement cannot be cast to a result set, and here you are trying to cast a statement to a result set.

I cannot fathom why you would think you'd access the result set this way, especially given that you've already done the work to get the result set from the statement. It's in your variable myRs.

You probably want

       int placeNo = myRs.getInt("NR_MIEJSCA");

(and similar for other lines) instead.

Sign up to request clarification or add additional context in comments.

2 Comments

I've made those changes and get the following error in IDE: The Method getiInt(String) is undefined for the type PreparedStatement for each line myRs.get line
@Luke_Nuke: if you're getting errors like that then it looks like you've written something like int placeNo = myStmt.getInt("NR_MIEJSCA"). That isn't what I wrote. Call getInt on the result-set, not the statement. However, you have since accepted my answer, so does that mean you've solved this problem yourself?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.