Luke_Nuke
Luke_Nuke

Reputation: 471

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

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.

Upvotes: 1

Views: 1246

Answers (1)

Luke Woodward
Luke Woodward

Reputation: 64949

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.

Upvotes: 1

Related Questions