public class studentEntity { private int StudentNo; private String LoginPwd; private String StudentName; private int Sex; private int GradeId; private String Phone; private String Address; private Date BornDate; private String Email; private String IdentityCard; //关联gradeEntity表与studentEntity表--- 一对一 private gradeEntity grade;
public class gradeEntity { private int GradeID; private String GradeName;
//关联gradeEntity表与studentEntity表--- 一对多 private List<studentEntity> listStudent;
/** * 查所有学生记录并保存到实体类中 * @throws SQLException */ @Test public void show() throws SQLException{ String sql="SELECT *FROM student INNER JOIN grade ON student.GradeId=grade.GradeID"; ResultSet rs=getAll(sql); List<studentEntity> list=new ArrayList<studentEntity>(); if (rs!=null) { while (rs.next()) { gradeEntity gs=new gradeEntity(); studentEntity ss=new studentEntity(); ss.setStudentNo(rs.getInt("studentNo")); ss.setStudentName(rs.getString("studentName")); gs.setGradeName(rs.getString("gradeName")); ss.setGrade(gs); list.add(ss); } } for (studentEntity se : list) { System.out.println("学生姓名:"+se.getStudentName()+"年纪姓名:"+se.getGrade().getGradeName()); } }
/** * 查S1下的所有学生记录并保存到实体类中 * @throws SQLException */ public void shows() throws SQLException{ String sql="SELECT *FROM student INNER JOIN grade ON student.GradeId=grade.GradeID where grade.GradeName='S1'"; ResultSet rs=getAll(sql); List<studentEntity> list=new ArrayList<studentEntity>(); gradeEntity gs=new gradeEntity(); if (rs!=null) { while (rs.next()) { studentEntity ss=new studentEntity(); ss.setStudentNo(rs.getInt("studentNo")); ss.setStudentName(rs.getString("studentName")); gs.setGradeName(rs.getString("gradeName")); ss.setGrade(gs); list.add(ss); } } gs.setListStudent(list); for (studentEntity se : list) { System.out.println("学生姓名:"+se.getStudentName()+"年纪姓名:"+se.getGrade().getGradeName()); } }