zoukankan      html  css  js  c++  java
  • 实体类之间实现一对一或一对多的关联

    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());
            }
        }
  • 相关阅读:
    软件工程网络15个人阅读作业1
    团队课程设计博客
    计时器(何雨柔201521123040)
    软工网络15个人作业4——alpha阶段个人总结
    软工网络15个人作业3——案例分析
    软件工程网络15结对编程作业
    软工网络15个人作业2
    软件工程网络15个人阅读作业1(201521123032 黄登峰)
    计时器(黄登峰 201521123032)
    201521123032 《Java程序设计》第14周学习总结
  • 原文地址:https://www.cnblogs.com/liu13-B/p/10945083.html
Copyright © 2011-2022 走看看