从数据库中检索项目时出现问题,以下是我的代码:
UserDAO.java
public class UserDAO {
public static Connection getConnection() {
Connection con = null;
try {
Class.forName("org.hsqldb.jdbcDriver");
con = DriverManager.getConnection(
"jdbc:hsqldb:hsql://localhost/oneDB", "sa", "");
} catch(Exception e) {
e.printStackTrace();
}
return con;
}
public static List<User> list() {
try{
Connection connection = getConnection();
List<User> users = new ArrayList<User>();
try {
PreparedStatement statement = connection.prepareStatement("SELECT * FROM user");
ResultSet rs = statement.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String address = rs.getString("address");
User user = new User(id, name, address);
System.out.println("user:" + user.getTheName());
users.add(user);
}
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
return users;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
在我的servlet中执行:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<User> users = UserDAO.list();
request.setAttribute("users", users);
request.getRequestDispatcher("showUser.jsp").forward(request, response);
}
showUser.jsp
<c:forEach items="${users}" var="user">
Name: <c:out value="${users.theName}" /> <br>
Address: <c:out value="${users.theAddress}" /><br>
</c:forEach>
堆栈追踪:
root cause
java.lang.NumberFormatException: For input string: "name"
java.lang.NumberFormatException.forInputString(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
javax.el.ListELResolver.coerce(ListELResolver.java:157)
javax.el.ListELResolver.getValue(ListELResolver.java:70)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:110)
org.apache.el.parser.AstValue.getValue(AstValue.java:169)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:943)
org.apache.jsp.showUser_jsp._jspx_meth_c_005fout_005f0(showUser_jsp.java:217)
org.apache.jsp.showUser_jsp._jspx_meth_c_005fforEach_005f0(showUser_jsp.java:178)
org.apache.jsp.showUser_jsp._jspService(showUser_jsp.java:134)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
controllers.GetUserServlet.doGet(GetUserServlet.java:37)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
我是jsp和servlet的新手,所以我不确定我哪里出错了。任何帮助将不胜感激。
答案 0 :(得分:0)
在foreach
片段中,您应该将元素称为user
,这是var
属性中提供的名称。同样,User
类的字段很可能是name
和address
,或theName
和theAddress
。
<c:forEach items="${users}" var="user">
Name: <c:out value="${user.name}" /> <br>
Address: <c:out value="${user.address}" /><br>
</c:forEach>