java 使用ArrayList从数据库中检索数据并在JSP中显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32258069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving data from database using ArrayList and displaying it in JSP
提问by Gayathri Keerthana
MySQL DB server used. Database name: library
, table name: book
.
table contains the following columns with given type and order:
使用的 MySQL 数据库服务器。数据库名称:library
,表名称:book
。表包含具有给定类型和顺序的以下列:
title(varchar),
author(varchar),
book_id(int)- primary key,
count (int),
favs (int).
title(varchar),
author(varchar),
book_id(int)- primary key,
count (int),
favs (int).
I want to view all the records of this table using ArrayList
in servlet
and JSP
.
Here are my codes:
ViewBook (servlet)
我想使用ArrayList
inservlet
和来查看这个表的所有记录JSP
。这是我的代码:ViewBook(servlet)
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import p1.*;
public class ViewBook extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
PrintWriter out=res.getWriter();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3307/library", "root", "admin");
PreparedStatement ps=con.prepareStatement("select * from book");
ResultSet rs=ps.executeQuery();
ArrayList<Book> books=new ArrayList<Book>();
while(rs.next())
{
Book b= new Book();
b.bookID=rs.getInt(3);
b.bookTitle=rs.getString(1);
b.bookAuthor=rs.getString(2);
b.bookCopies=rs.getInt(4);
b.bookFavs=rs.getInt(5);
books.add(b);
}
req.setAttribute("bookslist",books);
con.close();
RequestDispatcher rd=req.getRequestDispatcher("/view_book.jsp");
rd.forward(req,res);
}
catch(Exception e)
{
out.println(e);
}
}
}
}
Package p1 contains the following class:
包 p1 包含以下类:
Book.java
书.java
package p1;
public class Book
{
public int bookFavs,bookID, bookCopies;
public String bookTitle;
public String bookAuthor;
}
The JSP to which the request is dispatched- view_book.jsp:
请求被分派到的 JSP-view_book.jsp:
<html>
<body>
<head>
<title>
View Books
</title>
</head>
<body>
<table border=2>
<tr>
<th>Book ID</th>
<th>Title</th>
<th>Author</th>
<th>No. of copies AVAILABLE</th>
<th>Number of favourites</th>
</tr>
<%
ArrayList<Book> dbooks=(ArrayList)request.getAttribute("bookslist");
Iterator it=dbooks.iterator();
while(it.hasNext())
{
Book b=(Book)it.next();
%>
<tr>
<td><%=b.bookID%></td>
<td><%=b.bookTitle%></td>
<td><%=b.bookAuthor%></td>
<td><%=b.bookCopies%></td>
<td><%=b.bookFavs%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
The servlet and JSP aren't working. I know it's a vague question. Please point the blunders made. And solutions if possible!
servlet 和 JSP 不工作。我知道这是一个模糊的问题。请指出所犯的错误。以及可能的解决方案!
回答by chenchuk
The JSP file cant compile because the ArrayList needs an import directive :
JSP 文件无法编译,因为 ArrayList 需要导入指令:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>
回答by Srinu
You have to import your Book
and ArrayList
and Iterator
in your JSP
. Otherwise your JSP
file will not compile.
你必须导入Book
并ArrayList
和Iterator
你的JSP
。否则您的JSP
文件将无法编译。
you can import multiple classes by using ,
instead of ;
like in the java. If you specify ;
then you JSP
will throw Exception.
您可以通过使用,
而不是;
像在java.ini 中导入多个类。如果您指定,;
那么您JSP
将抛出异常。
<%@ page import="java.util.ArrayList,java.util.Iterator, p1.Book" %>
回答by Mwangi Thiga
Import java.util.*;
to import the ArrayList
class in the jsp. Then, also import the required classes e.g. the Book
class too. It should work.
Import java.util.*;
ArrayList
在jsp中导入类。然后,也导入所需的类,例如Book
类。它应该工作。
回答by ElCalefactordeManchuri
The problem is quite simple: you don't execute the servlet anytime. You must to invoke the servlet from another .jsp then, inside that servlet, call the other jsp.
问题很简单:您不会在任何时候执行 servlet。您必须从另一个 .jsp 调用 servlet,然后在该 servlet 内调用另一个 jsp。