Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我无法将数据从MySQL传输到Jsp_Java_Mysql_Jsp_Model View Controller_Database Connection - Fatal编程技术网

Java 我无法将数据从MySQL传输到Jsp

Java 我无法将数据从MySQL传输到Jsp,java,mysql,jsp,model-view-controller,database-connection,Java,Mysql,Jsp,Model View Controller,Database Connection,我试图使用Tomcat服务器将数据从Mysql传输到Jsp,但我无法提取数据,我的代码也没有显示任何错误。 当我将其作为Java应用程序运行时,我可以从控制器中看到mysql数据。如果你能在这件事上帮忙,我会很高兴的 你可以从这里看到我的数据库 模范班 public class Book{ protected int id; protected String title; protected String author; protected float price; public Book(

我试图使用Tomcat服务器将数据从Mysql传输到Jsp,但我无法提取数据,我的代码也没有显示任何错误。 当我将其作为Java应用程序运行时,我可以从控制器中看到mysql数据。如果你能在这件事上帮忙,我会很高兴的

你可以从这里看到我的数据库

模范班

public class Book{
protected int id;
protected String title;
protected String author;
protected float price;

public Book() {
}

public Book(int id) {
    this.id = id;
}

public Book(int id, String title, String author, float price) {
    this(title, author, price);
    this.id = id;
}

public Book(String title, String author, float price) {
    this.title = title;
    this.author = author;
    this.price = price;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public float getPrice() {
    return price;
}

public void setPrice(float price) {
    this.price = price;
}
查看Jsp

书店应用程序 书单 身份证件 标题 著者 价格 行动 用于数据库操作的BookDAO类

public class BookDAO {
private String jdbcURL;
private String jdbcUsername;
private String jdbcPassword;
private Connection jdbcConnection;

public BookDAO(String jdbcURL, String jdbcUsername, String jdbcPassword) {
    this.jdbcURL = jdbcURL;
    this.jdbcUsername = jdbcUsername;
    this.jdbcPassword = jdbcPassword;
}

protected void connect() throws SQLException {
    if (jdbcConnection == null || jdbcConnection.isClosed()) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new SQLException(e);
        }
        jdbcConnection = DriverManager.getConnection(
                                    jdbcURL, jdbcUsername, jdbcPassword);
    }
}

protected void disconnect() throws SQLException {
    if (jdbcConnection != null && !jdbcConnection.isClosed()) {
        jdbcConnection.close();
    }
}

public List<Book> listAllBooks() throws SQLException {
    List<Book> listBook = new ArrayList<>();

    String sql = "SELECT * FROM book";

    connect();

    Statement statement = jdbcConnection.createStatement();
    ResultSet resultSet = statement.executeQuery(sql);
    while (resultSet.next()) {
        int id = resultSet.getInt("id");
        String title = resultSet.getString("title");
        String author = resultSet.getString("author");
        float price = resultSet.getFloat("price");

        Book book = new Book(id, title, author, price);
        listBook.add(book);
    }

    resultSet.close();
    statement.close();

    disconnect();
    return listBook;
}

public Book getBook(int id) throws SQLException {
    Book book = null;
    String sql = "SELECT * FROM book WHERE id = ?";

    connect();

    PreparedStatement statement = jdbcConnection.prepareStatement(sql);
    statement.setInt(1, id);

    ResultSet resultSet = statement.executeQuery();

    if (resultSet.next()) {
        String title = resultSet.getString("title");
        String author = resultSet.getString("author");
        float price = resultSet.getFloat("price");

        book = new Book(id, title, author, price);
    }

    resultSet.close();
    statement.close();

    return book;
}
控制器类

public class BookController extends HttpServlet {
private static final long serialVersionUID = 1L;
private BookDAO bookDAO;

public void init() {

    String jdbcURL = getServletContext().getInitParameter("jdbc:mysql://localhost:3306/bookstore");
    String jdbcUsername = getServletContext().getInitParameter("root");
    String jdbcPassword = getServletContext().getInitParameter("root");

    bookDAO = new BookDAO(jdbcURL, jdbcUsername, jdbcPassword);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doGet(request, response);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String action = request.getServletPath();

    listBook(request, response);
}

private void listBook(HttpServletRequest request, HttpServletResponse response)
{

    List<Book> listBook = null;
    try {
        listBook = bookDAO.listAllBooks();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    request.setAttribute("listBook", listBook);
    RequestDispatcher dispatcher = request.getRequestDispatcher("BookList.jsp");
    try {
        dispatcher.forward(request, response);
    } catch (ServletException | IOException e) {
        e.printStackTrace();
    }
}

private void showNewForm(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    RequestDispatcher dispatcher = request.getRequestDispatcher("BookForm.jsp");
    dispatcher.forward(request, response);
}

private void showEditForm(HttpServletRequest request, HttpServletResponse response)
        throws SQLException, ServletException, IOException {
    int id = Integer.parseInt(request.getParameter("id"));
    Book existingBook = bookDAO.getBook(id);
    RequestDispatcher dispatcher = request.getRequestDispatcher("BookForm.jsp");
    request.setAttribute("book", existingBook);
    dispatcher.forward(request, response);

}

没有明显的错误。request.setAttributelistBook,listBook;列表中有明确的数据作为属性集?是的,我可以看到控制器上的数据。@Arda,代码没有问题,但我可以想到一种情况,即数据不可用;直接访问jsp页面,在这种情况下控制器将不会执行,您可以确认您的请求流。