public List list() {
String sql="select * from news";
conn conn=new conn();
ArrayList list=new ArrayList();
news news=new news();
ResultSet rs=conn.executeQuery(sql);
try {
while(rs.next())
{
news.setNewsid(rs.getInt("newsid"));
list.add(news);这边就是取出数据库中的数据,放到LIST中去 }
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
conn.closeall();
}
return list;将LIST返回 }
}
action中:
                            listimpl impl=new listimpl();
request.getSession().setAttribute("list", impl.list());
return mapping.findForward("success");
这边就是将结果集传过去;
这边是用来显示的页面:
<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head><body>
<table width="882" border="2" cellpadding="0" cellspacing="0" bordercolor="#B5D9F2">
  <tr>
    <td height="23" colspan="6" class="head2">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="6" class="zhi">你好,欢迎光临 ! </td>
  </tr>
  <tr>
    <td height="28" colspan="6" class="head"><table width="780" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="93" height="19"><div align="center">序号</div></td>
        <td width="109"><div align="center">标题</div></td>
        <td width="499"><div align="center">添加时间</div></td>
        <td width="10">&nbsp;</td>
        <td width="60"><div align="center">操作</div></td>
        <td width="12">&nbsp;</td>
      </tr>
    </table></td>
  </tr>
   <logic:iterate id="mylist" name="list" scope="session"> 
  <tr>
    <td width="90"><bean:write name="mylist" property="newsid"/> </td>
    <td width="108">&nbsp;</td>
    <td width="505">&nbsp;</td>
    <td width="34">&nbsp;</td>
    <td width="33">&nbsp;</td>
    <td width="98">&nbsp;</td>
  </tr>
  </logic:iterate>  <tr>
    <td height="20" colspan="6">&nbsp;</td>
  </tr>
</table>
</body>
</html>

照理说应该是取出ID,然后丛1到22条记录全部取出来,显示在JSP页面中,但是奇怪的事,就是显示出来22条数据,全部都是一样的,而且都是最后一条数据,就是一直22,22,22,22,22,22,22,22,

解决方案 »

  1.   

    把 news news=new news();放到
    while(rs.next())
    {
    news news=new news();
    news.setNewsid(rs.getInt("newsid"));
    list.add(news);这边就是取出数据库中的数据,放到LIST中去 }
      

  2.   

    你把news news=new news();
    放在循环外面时,那么news引用会被循环里的语句反复重写,最后引用news 指向的就是最后一笔数据的值了
      

  3.   

    News news=null;
    ResultSet rs=conn.executeQuery(sql);
    try {
    while(rs.next())
    {
    news=new News();
    news.setNewsid(rs.getInt("newsid"));
    list.add(news);这边就是取出数据库中的数据,放到LIST中去 }
      

  4.   


    news news=new news(); 放到循环里面
      

  5.   

    恩,这是入门人经常犯的错误,注重理解
    news=new News();
    list.add(news);
    下面两句话真正的含义
      

  6.   

    因为你操作的是一个对象的引用news news=null;ResultSet rs=conn.executeQuery(sql);
    try {
    while(rs.next())
    {
    news=new news();
    news.setNewsid(rs.getInt("newsid"));
    list.add(news);
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }