我在mysql数据库中有3条记录,这些是
mysql> select name,dob,gender,profession,relationship,dependent from family;
+------+------------+--------+------------+--------------+-----------+
| name | dob | gender | profession | relationship | dependent |
+------+------------+--------+------------+--------------+-----------+
| XYZ | 1991-02-10 | MALE | DEVELOPER | BROTHER | TRUE |
| ABC | 1991-02-10 | FEMALE | DEVELOPER | SISTER | TRUE |
| DEF | 1991-02-10 | FEMALE | DEVELOPER | SISTER | TRUE |
+------+------------+--------+------------+--------------+-----------+
3 rows in set (0.00 sec)
当我试图在我的jsp页面中显示这些记录时,下面的代码总是只显示三次第一条记录,
Family fa=null;
List members = session.createQuery("FROM Family").list();
for (Iterator iterator = members.iterator(); iterator.hasNext();){
fa= (Family) iterator.next();
f[i]=new Family(
fa.getname(),
fa.getdob(),
fa.getgender(),
fa.getprofession(),
fa.getrelationship(),
fa.getdependent()
);
f[i].setname(fa.getname());
System.out.println(f[i].getname());
i++;
fa=null;
}
HttpSession ses=request.getSession();
ses.setAttribute("f",f);
ses.setAttribute("i", String.valueOf(i));
当我运行此代码时,我得到以下输出
XYZ
XYZ
XYZ
请告诉我这段代码出了什么问题。
我在这里展示了&family;家庭。'代码
package com.lms.model;
import org.apache.struts.action.ActionForm;
public class Family extends ActionForm
{
int id;
String employee_no;
String name;
String nominee;
String dob;
int age;
String nationality;
String blood_group;
String gender;
String profession;
String relationship;
String dependent;
String medi_claim;
String medi_history;
public Family(){}
public Family(
String name,
String dob,
String gender,
String profession,
String relationship,
String dependent){
this.name=name;
this.dob=dob;
this.gender=gender;
this.profession=profession;
this.relationship=relationship;
this.dependent=dependent;
}
public String getemployee_no()
{
return employee_no;
}
public void setemployee_no(String str)
{
employee_no=str;
}
public int getid()
{
return id;
}
public void setid(int str)
{
id=str;
}
public String getname()
{
return name;
}
public void setname(String str)
{
name=str;
}
public String getnominee()
{
return nominee;
}
public void setnominee(String str)
{
nominee=str;
}public String getdob()
{
return dob;
}
public void setdob(String str)
{
dob=str;
}public int getage()
{
return age;
}
public void setage(int str)
{
age=str;
}public String getnationality()
{
return nationality;
}
public void setnationality(String str)
{
nationality=str;
}
public String getblood_group()
{
return blood_group;
}
public void setblood_group(String str)
{
blood_group=str;
}public String getgender()
{
return gender;
}
public void setgender(String str)
{
gender=str;
}public String getprofession()
{
return profession;
}
public void setprofession(String str)
{
profession=str;
}public String getrelationship()
{
return relationship;
}
public void setrelationship(String str)
{
relationship=str;
}public String getdependent()
{
return dependent;
}
public void setdependent(String str)
{
dependent=str;
}public String getmedi_claim()
{
return medi_claim;
}
public void setmedi_claim(String str)
{
medi_claim=str;
}public String getmedi_history()
{
return medi_history;
}
public void setmedi_history(String str)
{
medi_history=str;
}
}
jsp代码是
<%
HttpSession ss=request.getSession();
int i=Integer.parseInt(ss.getAttribute("i").toString());
Family f[]=(Family[])(ss.getAttribute("f"));
for(int j=0;j<i;j++)
{
%>
<tr>
<td><%=f[j].getname()%></td>
<td><%=f[j].getdob()%></td>
<td><%=f[j].getgender()%></td>
<td><%=f[j].getprofession()%></td>
<td><%=f[j].getrelationship()%></td>
<td><%=f[j].getdependent()%></td>
<td>edit</td>
<td>delete</td>
</tr>
<%} %>
仅为家庭pojo的映射代码是:
<class name="com.lms.model.Family" table="family">
<meta attribute="class-description">
This class contains the details of employee family.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="employee_no" column="empno" type="string" />
<property name="name" column="name" type="string" />
<property name="nominee" column="nominated" type="string" />
<property name="dob" column="dob" type="string" />
<property name="age" column="age" type="int" />
<property name="nationality" column="nationality" type="string" />
<property name="blood_group" column="blood_group" type="string" />
<property name="gender" column="gender" type="string" />
<property name="profession" column="profession" type="string" />
<property name="relationship" column="relationship" type="string" />
<property name="dependent" column="dependent" type="string" />
<property name="medi_claim" column="medi_claim" type="string" />
<property name="medi_history" column="medi_history" type="string" />
</class>
答案 0 :(得分:0)
最后我得到了结果。 在代码中我忘记在会话中保存id,我更改了代码,当我添加下面的代码
Family fm = new Family(f.getname(),f.getnominee(),f.getdob(),f.getage (),f.getnationality(), f.getblood_group(),f.getgender(),f.getprofession(),f.getrelationship(), f.getdependent(),f.getmedi_claim(),f.getmedi_history());
fmID = (Integer) session.save(fm);
List members = session.createQuery("FROM Family").list();
for (Iterator iterator = members.iterator(); iterator.hasNext();){
fa= (Family) iterator.next();
System.out.println(fa.getname());
f[i]=fa;
i++;
}
我得到了结果。 非常感谢你的朋友们的指导。