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


    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());
            }
        }
  • 相关阅读:
    Javascript异步编程的4种方法
    同步编程和异步编程
    关于js 异步回调的一些方法
    array的方法 没记住的
    阮一峰关于reduce 和transduce的博客
    CSS开发小技巧
    提升自己的一个网址
    asm.js 和 Emscripten 入门教程
    Koa -- 基于 Node.js 平台的下一代 web 开发框架
    C#中使用handsonetable的一个例子
  • 原文地址:https://www.cnblogs.com/liu13-B/p/10945083.html
Copyright © 2020-2023  润新知