• 实体类之间实现一对一或一对多的关联


    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());
            }
        }
  • 相关阅读:
    go语言基础之闭包捕获外部变量特点
    go语言基础之基匿名函数本语法和闭包
    go语言基础之回调函数
    go语言基础之函数类型
    go语言基础之递归实现数字累加
    go语言基础之递归函数的调用流程
    go语言基础之普通函数的调用流程
    maven install 打包 报错 Cannot run program "gpg.exe": CreateProcess error
    ELK&H3c监控篇
    机房监控中的网络设备监控到底是怎样的?
  • 原文地址:https://www.cnblogs.com/liu13-B/p/10945083.html
Copyright © 2020-2023  润新知