对于类型PreparedStatement,未定义方法getInt,getString和getDate。

时间:2018-09-04 05:30:45

标签: java rest api jdbc prepared-statement

您好,我不断收到此错误消息: 未为类型PreparedStatement定义方法getInt(String / int / Date) (在IDE中为以下每个显示的错误: 以下几行:

            int placeNo = myStmt.getInt("NR_MIEJSCA");
            Date start = myStmt.getDate("START");
            Date end = myStmt.getDate("KONIEC");
            String userName = myStmt.getString("IMIE_NAZWISKO");
            int phone = myStmt.getInt("TELEFON");

这是我的方法,用于发送有关特定停车位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 = myStmt.getInt("NR_MIEJSCA");
            Date start = myStmt.getDate("START");
            Date end = myStmt.getDate("KONIEC");
            String userName = myStmt.getString("IMIE_NAZWISKO");
            int phone = 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();
    }
}

我认为我准备的陈述中有些内容,但我找不到问题所在。

2 个答案:

答案 0 :(得分:1)

您正在PreparedStatement上调用getInt()和getDate()。这些功能不存在:https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html

但是,它们确实存在于ResultSet;)中: https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html

所以代替这个:

while (myRs.next()) {
    // retrive data from result set row
    int placeNo = myStmt.getInt("NR_MIEJSCA");
    Date start = myStmt.getDate("START");
    Date end = myStmt.getDate("KONIEC");
    String userName = myStmt.getString("IMIE_NAZWISKO");
    int phone = 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);
}

尝试一下:

while (myRs.next()) {
    // retrive data from result set row
    int placeNo = myRs.getInt("NR_MIEJSCA");
    Date start = myRs.getDate("START");
    Date end = myRs.getDate("KONIEC");
    String userName = myRs.getString("IMIE_NAZWISKO");
    int phone = myRs.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);
}

答案 1 :(得分:0)

getIntgetDategetStringResultSet方法,而不是PreparedStatement方法。试试:

int placeNo = myRs.getInt("NR_MIEJSCA");
Date start = myRs.getDate("START");
Date end = myRs.getDate("KONIEC");
String userName = myRs.getString("IMIE_NAZWISKO");
int phone = myRs.getInt("TELEFON");