你好我是java编程新手
这里尝试使用servlet从数据库中检索数据,并在jsp页面上显示。
请建议我
这是代码
User.java
public class User {
private String EventName;
private String Location;
public User(){}
public User(String EventName,String Location){
this.EventName=EventName;
this.Location=Location;
}
public String getEventName()
{
return (this.EventName);
}
public String getLocation()
{
return (this.Location);
}
public void setName(String EventName)
{
this.EventName=EventName;
}
public void setLoc(String Location)
{
this.Location=Location;
}
}
retrieve.java
try{
ArrayList<User> users = new ArrayList<User>();
//List<String> users = new ArrayList<String>();
while(rs.next()){
User user = new User();
String Name=rs.getString(9);
String Loc=rs.getString(7);
user.setName(Name);
user.setLoc(Loc);
users.add(user);
}
request.setAttribute("users", users);
RequestDispatcher view = request.getRequestDispatcher("Display.jsp");
view.forward(request, response);
}
display.jsp
<table>
<c:forEach var="user" items="${users}">
<tr>
<td><c:out value="${users.EventName}"/> </td>
<td><c:out value="${users.Location}"/> </td>
</tr>
</c:forEach>
</table>
错误如下
例外
org.apache.jasper.JasperException: java.lang.NumberFormatException: For input
string: "EventName"
org.apache.jasper.servlet.JspServletWrapper.handleJspException
(JspServletWrapper.java:548)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:471)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter
(MonitorFilter.java:393)
TicketPkg.Daily.processRequest(Daily.java:77)
TicketPkg.Daily.doGet(Daily.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter
(MonitorFilter.java:393)
root cause
java.lang.NumberFormatException: For input string: "EventName"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Integer.parseInt(Integer.java:492)
java.lang.Integer.parseInt(Integer.java:527)
javax.el.ListELResolver.coerce(ListELResolver.java:173)
javax.el.ListELResolver.getValue(ListELResolver.java:52)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
org.apache.el.parser.AstValue.getValue(AstValue.java:169)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate
(PageContextImpl.java:985)
org.apache.jsp.Daily_jsp._jspx_meth_c_005fout_005f0(Daily_jsp.java:266)
org.apache.jsp.Daily_jsp._jspx_meth_c_005fforEach_005f0(Daily_jsp.java:229)
org.apache.jsp.Daily_jsp._jspService(Daily_jsp.java:184)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter
(MonitorFilter.java:393)
TicketPkg.Daily.processRequest(Daily.java:77)
TicketPkg.Daily.doGet(Daily.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter
(MonitorFilter.java:393)
Thanks in advance
答案 0 :(得分:0)
示例代码SEE HERE
private void doProcess(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException , SQLException{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
PrintWriter out = response.getWriter();
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Set connection : URL : jdbc:mysql://localhost:3306/mydatabase");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase","root","root");
System.out.println("Create statement.");
stmt = conn.createStatement();
......
}catch(Exception e){
e.printStackTrace();
}finally{
if(stmt != null){
stmt.close();
}
if(conn != null){
conn.close();
}
}
}