-1

你好我是java编程新手

这里我尝试使用 servlet 从数据库中检索数据,并显示在 jsp 页面上。

请建议我

这是代码

用户.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;
}

}

检索.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);

}

显示.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
4

1 回答 1

0

示例代码 见这里

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();
            }
        }
    }
于 2013-05-16T06:32:04.110 回答