2016-05-04 3 views
0

最後に挿入されたレコードを表示している間に、テーブルに多数のレコードを挿入しました。特定のIDを表示していますが、すべてのレコードがデータベースに挿入されています。コードを見て、どこで間違っているのかを教えてください。jspページのループで多くの結果を出力できません

DAOコード

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; 
    } 
} 

サーブレットコード

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> 

つだけのレコードが表示されますすべてのレコードがデータベースに挿入されています。あなたのstudyDAO.javaで

答えて

0

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

上記のコードは

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

今、それが動作するはずです。

関連する問題