无法在jsp页面中的循环中打印许多结果

时间:2016-05-04 06:50:07

标签: mysql jsp servlets

我在表中插入了很多记录,同时显示记录最后插入的记录是针对特定ID显示的。但是所有记录都插入到数据库中。虽然它没有显示?请查看代码并告诉我哪里做错了?

DAO CODE

package org.fproject;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;


public class StudyDAO {
    private String driver="com.mysql.jdbc.Driver";
    private String url="jdbc:mysql://localhost:3306/mydb";
    private String dbuser="root";
    private String dbpassword="root123";
    private String commdetSQL="select cid,fname,lname,email,comment from comment where qid=?";
private Connection con;
    private PreparedStatement pstmtcommdisplay;
public StudyDAO()throws ClassNotFoundException,SQLException{
        Class.forName(driver);
        con=DriverManager.getConnection(url,dbuser,dbpassword);
pstmtcommdisplay=con.prepareStatement(commdetSQL);
    }
public Collection<Comment> getComment(int qid)throws SQLException{
        pstmtcommdisplay.setInt(1, qid);
        ResultSet rs=pstmtcommdisplay.executeQuery();
        ArrayList<Comment>list4=null;
        while(rs.next()){
            list4=new ArrayList<Comment>();
        Comment c=new Comment(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5));
        list4.add(c);
        }
        return list4;
    }
}

Servlet代码

package org.fproject;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class DisCommServlet
 */
@WebServlet("/DisCommServlet")
public class DisCommServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public DisCommServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String strqid=req.getParameter("qid");
        int qid=Integer.parseInt(strqid);
        try{
            StudyDAO dao=new StudyDAO();
            Collection<Comment> list4=dao.getComment(qid);
            //req.setAttribute("LIST4", list4);
            //req.setAttribute("QID", qid);
            HttpSession s=req.getSession(true);
            s.setAttribute("LIST4", list4);
            s.setAttribute("QID", qid);
            res.sendRedirect("DisComm.jsp");
        }catch(ClassNotFoundException e){
            e.printStackTrace();
            res.sendError(9999,"Error in classloading");
        }catch(SQLException e){
            e.printStackTrace();
            res.sendError(9998,"Error in sql:"+e.getMessage()+"Error code:"+e.getErrorCode()+e.getSQLState());
        }catch(Exception e){
            res.sendError(8000,"Unknown Error"+e.getMessage());
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

JSP页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
      <%@ page import="java.util.*,org.fproject.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Answers</title>
</head>
<body>
<%
    int qid=(Integer)session.getAttribute("QID");
    Collection<Comment> list4=(Collection<Comment>)session.getAttribute("LIST4");
%>
<%if(list4!=null){  %>
    <% for(Comment c:list4){ %>
    <br><hr><%=c.getFname() %> &nbsp; <%=c.getLname() %><br><%=c.getEmail() %><br><p><b><%=c.getComment() %></b></p>
    <% } %>
    <%} else { %>
    <h3>No answers are added</h3>
    <% }%>
</body>
</html>

只显示一条记录,但所有记录都插入数据库中。

1 个答案:

答案 0 :(得分:0)

在你的studyDAO.java

while(rs.next()){
 list4=new ArrayList<Comment>();

上面的代码应该是

  list4=new ArrayList<Comment>();
while(rs.next()){

现在它会起作用。