1
我使用MVC在Java中開發Web項目。
我的問題是我需要從一個類中返回三個不同的bean。所有三個bean都有多個對象,所以我現在在列表中添加每個相同的bean對象並返回三個不同的列表。
好吧,讓它更清晰我需要從存儲評論的表中檢索所有內容。因此,所有評論文本都存儲在一個名爲comment的bean中,並添加到名爲listcomment的列表中。發表評論的成員的名稱被添加到另一個名爲member的bean中,並再次添加到名爲listmember的列表中。
那麼是否有任何可能的方式,使這兩個豆可以添加在同一個列表中?在同一個列表中返回兩個不同的bean
public class TeleCommentView {
int qid;
TeleComment comment;
TeleHospital hospital;
doctorperson doctor;
ConnectionFile connection = new ConnectionFile();
List<TeleComment> listcomment = new ArrayList<TeleComment>();
List<doctorperson> listdoctor = new ArrayList<doctorperson>();
List<TeleHospital> listhospital = new ArrayList<TeleHospital>();
public TeleCommentView(int qid)
{
this.qid = qid;
process();
}
public void process()
{
int count=0;
try
{
Connection con = connection.connectionfile();
PreparedStatement pstmt = con.prepareStatement("select TeleHospital.HospitalName,DoctorDetail.Name,TeleComment.Comment,TeleComment.SDate from"
+ "((TeleComment left join TeleHospital on TeleHospital.HospitalId=TeleComment.Hid) "
+ "left join DoctorDetail on DoctorDetail.DoctorId = TeleComment.Did) "
+ "where TeleComment.Qid=?");
ResultSet rs = pstmt.executeQuery();
while(rs.next())
{
comment = new TeleComment();
comment.setComment(rs.getString("Comment"));
comment.setSdate(rs.getDate("SDate"));
listcomment.add(count,comment) ;
/******End of comment**************/
//Add doctor or hospital name as required
doctor = new doctorperson();
hospital = new TeleHospital();
if(rs.getString("HospitalName").equals(null))
{
doctor.setName(rs.getString("Name"));
listdoctor.add(count,doctor);
}
else
{
hospital.setHospitalname(rs.getString("HospitalName"));
listhospital.add(count,hospital);
}
count++;
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public List getCommentList()
{
return listcomment;
}
public List getDoctorList()
{
return listdoctor;
}
public List getHospitalList()
{
return listhospital;
}
}
首先編寫代碼。 –