java.lang.ClassCastException:org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement无法转换为java.sql.ResultSet

时间:2018-09-03 20:44:36

标签: java rest api jdbc prepared-statement

您好,在使用API​​ URL获取预订的特定停车位编号列表时,我一直收到此错误,这是错误消息:

java.lang.ClassCastException:org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement无法转换为java.sql.ResultSet

这是我的方法,用于发送有关特定停车位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);
    }
}

这是我的API类:

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();
    }
}

我找不到问题。

1 个答案:

答案 0 :(得分:1)

您正在编写

之类的行
       int placeNo = ((ResultSet) myStmt).getInt("NR_MIEJSCA");

这显然是您得到错误的地方。该错误表明无法将语句强制转换为结果集,而您正在此处尝试将语句强制转换为结果集。

我无法理解为什么您会认为您将以这种方式访问​​结果集,特别是考虑到您已经完成了从语句中获取结果集的工作。它在您的变量myRs中。

您可能想要

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

(其他行类似)。