JAVA语言中数据表的建立
小标 2018-07-19 来源 : 阅读 618 评论 0

摘要:本文主要向大家介绍了JAVA语言中数据表的建立,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言中数据表的建立,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

class Emp{

    private int empno;//职工编号

    private String ename;//姓名

    private String job;//职位

    private double sal;//基本工资

    private double comm;

    private Emp mgr;//所属领导

    private  Dept dept;//所在部门

 

    public Emp(){}//无参构造

 

    public Emp(int empno,String ename,String job,double sal,double comm){//有参构造

        this.empno = empno;

        this.ename = ename;

        this.job = job;

        this.sal = sal;

        this.comm = comm;

    }

 

    public void setMgr(Emp mgr) {//传递领导信息

        this.mgr = mgr;

    }

    public Emp getMgr(){//获取领导信息

        return this.mgr;

    }

 

    public void setDept(Dept dept) {//设置部门信息

        this.dept = dept;

    }

 

    public Dept getDept() {//读取部门信息

        return dept;

    }

 

    public String getInfo(){

        return "职工编号:"+this.empno +",职工姓名:" + this.ename + ",职工职位:" + this.job + ",基本工资:" + this.sal

                +",提成:" + this.comm;

    }

}class Dept{

    private int deptno;

    private String dname;//部门名称

    private String loc;//部门地址

    private Emp[] emps;//所有员工

    public Dept(){ }

    public Dept(int deptno,String dname,String loc){

        this.deptno = deptno;

        this.dname = dname;

        this.loc = loc;

    }

 

    public void setEmps(Emp[] emps) {

        this.emps = emps;

    }

 

    public Emp[] getEmps() {

        return emps;

    }

 

    public String getInfo(){

        return "部门编号:" + this.deptno +",部门名称:" + this.dname +",所在地址:" + this.loc;

    }

}public class Main {

    public static void main(String[] args) {

        // 创建各自的实例化对象

        Dept dept = new Dept(10,"ACCOUNTING","NEW YORK");

        Emp ea = new Emp(7369,"SMITH","CLERK",800.0,0.0);

        Emp eb = new Emp(7466,"ALLEN","MANAGER",2450.0,0.0);

        Emp ec = new Emp(7839,"KING","PRESIDENT",5000.0,0.0);

        //设置领导关系        ea.setMgr(eb);

        eb.setMgr(ec);//ec对象没有领导,因为他是头

        //设置员工和部门关系        ea.setDept(dept);

        eb.setDept(dept);

        ec.setDept(dept);

        //设置部门和员工的关系

        dept.setEmps(new Emp[]{ea,eb,ec});

 

        //读取数据

        System.out.println(dept.getInfo());//输出部门信息

        for(int x=0;x<dept.getEmps().length;x++){

            System.out.println("\t"+dept.getEmps()[x].getInfo());

            if(dept.getEmps()[x].getMgr()!=null){ //若有领导

                System.out.println("\t"+dept.getEmps()[x].getMgr().getInfo());

            }

        }

        System.out.println("************************");

        //根据员工找到对应的领导信息和部门信息        System.out.println(eb.getInfo());

        if(eb.getMgr()!=null)

            System.out.println("\t"+eb.getMgr().getInfo());

        if(eb.getDept()!=null)

            System.out.println("\t"+eb.getDept().getInfo());

    }

}

 

上面代码是实现一对多的数据表,基于公司人事管理的例子(根据员工可以输出其基本信息及所在部门信息和所属领导信息,根据部门可以输出所有员工及领导),在Java一对多的数据关系中,需要遵循以下设计原则:

简单Java类设计原则:Java类与数据表的关系

  1、Java的名称 = 实体表的名称

  2、Java类的属性 = 实体表的字段

  3、Java类的一个对象 = 表的一行记录

  4、对象数组 = 表的多行记录

  5、外键关系 = 引用配置

 

多对多的数据表(学生成绩管理):根据学生可以输出所修课程信息及成绩,根据课程可以输出学习该课程的学生信息及成绩

 

class Student{//学生表

    private int stuid;

    private String sname;

    private int age;

    private StudentCourse studentCourse[];//学生成绩信息

    public Student(){}

    public Student(int stuid,String sname,int age){

        this.stuid = stuid;

        this.sname = sname;

        this.age = age;

    }

    public void setStudentCourse(StudentCourse[] studentCourse) {

        this.studentCourse = studentCourse;

    }

 

    public StudentCourse[] getStudentCourse() {

        return studentCourse;

    }

 

    public String getInfo(){

        return "学号:" + this.stuid + ",学生姓名:" + this.sname + ",年龄:" + this.age;

    }

}class Course{//课程表

    private String name;

    private int cid;

    private int credit;

    private StudentCourse studentCourse[];

    public Course(){}

    public Course(String name,int cid,int credit){

        this.cid = cid;

        this.name = name;

        this.credit = credit;

    }

 

    public StudentCourse[] getStudentCourse() {

        return studentCourse;

    }

 

    public void setStudentCourse(StudentCourse[] studentCourse) {

        this.studentCourse = studentCourse;

    }

 

    public String getInfo(){

        return "课号:" + this.credit + ",名称:" + this.name + ",学分:" + this.credit;

    }

}class StudentCourse{//学生选课表

    private Student student;

    private Course course;

    private double score;//成绩

    public StudentCourse(){

    }

    public StudentCourse(Student student,Course course,double score){

        this.course = course;

        this.score = score;

        this.student = student;

    }

 

    public Course getCourse() {

        return course;

    }

 

    public Student getStudent() {

        return student;

    }

 

    public double getScore() {

        return score;

    }

}public class Main {

 

    public static void main(String[] args) {

        //创建各自的独立对象

        Student stu1 = new Student(1,"张三",18);

        Student stu2 = new Student(1,"李四",20);

        Student stu3 = new Student(1,"王五",19);

        Course ca = new Course("高等数学",1001,5);

        Course cb = new Course("线性代数",1002,4);

        //设置各自的关系

        //设置学生和课程的关系

        stu1.setStudentCourse(new StudentCourse[]{

                new StudentCourse(stu1,ca,92.5),

                new StudentCourse(stu1,cb,94.0)

        });

        stu2.setStudentCourse(new StudentCourse[]{

                new StudentCourse(stu2,ca,89.0)

        });

        stu3.setStudentCourse(new StudentCourse[]{

                new StudentCourse(stu3,cb,95.0),

                new StudentCourse(stu3,ca,90.5)

        });

        //设置课程和学生的关系

        ca.setStudentCourse(new StudentCourse[]{

                new StudentCourse(stu1,ca,92.5),

                new StudentCourse(stu2,ca,89.0)

                new StudentCourse(stu3,ca,90.5)

        });

        cb.setStudentCourse(new StudentCourse[]{

                new StudentCourse(stu1,cb,94.0),

                new StudentCourse(stu3,cb,95.0)

        });

        //找到一门课程,并且输出学习此课程的所有学生的信息及成绩        System.out.println(ca.getInfo());

        for(int x = 0;x < ca.getStudentCourse().length;x++){

            System.out.println("\t"+ca.getStudentCourse()[x].getStudent().getInfo()

                    +",成绩:"+ca.getStudentCourse()[x].getScore());

        };

        System.out.println("***********");

        //根据学生输出其信息及选修的课程信息和所得成绩        System.out.println(stu1.getInfo());

        for(int x = 0;x < stu1.getStudentCourse().length;x++){

            System.out.println("\t选修课程:" + stu1.getStudentCourse()[x].getCourse().getInfo()

                    + ",所得成绩:" + stu1.getStudentCourse()[x].getScore());

        }

 

    }

}

希望对JAVA有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言JAVA频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程