ITPub博客

首页 > IT职业 > IT生活 > 【数据库课设进行时】多表查询

【数据库课设进行时】多表查询

IT生活 作者:wzzhujun 时间:2021-09-09 10:22:00 0 删除 编辑

前文

以为很早就搞完了,但老王说还没完,需求没有满足——查询学生信息的时候,惩罚信息、奖励信息、学籍变更信息没有显示!为了解决这个问题,也费尽了周折。但最后还是解决了,这里码一下,做个纪念。

正文

student表:studentID、class、sex、name、department、native_place、birthday;
class表:code、name、monitor
department表:code、name
reward表:ID、levels、studentID、rec_time、description
reward_levels:code、description
punishment:ID、levels、studentID、rec_time、description
punish_leves: code、description


图片描述

student


刚开始学生信息的显示只是将上面图片的class、department的编号用名称(name)表示,建立三表查询。

String sql = "select student.studentID ,student.name,sex,class.name,department.name,birthday,native_place from student ,class,department where student.class = class.Id AND student.department =  department.Id";

后来需要加上其他信息(如上),就遇到了几个问题:

  • 其他表可能没有存在相应的记录,所以查询失败,以至于学生信息都没查询到。

  • 多条记录(如奖励多条)会显示多条学生记录。

走了一段弯路之后,回过头来,想到既然多个表一起查询会失败,要不就将学生信息查询和其它表分开来查询,然后根据学号再到其他表中去找,有记录就显示,没记录就显示无。
于是整个查询代码如下:

String sql = "select student.studentID ,student.name,sex,class.name,department.name,birthday,native_place from student ,class,department where student.class = class.Id AND student.department =  department.Id";
        dbProcess.connect();
        ResultSet rs = dbProcess.executeQuery(sql);
        studentvector.clear();        
        try {            while(rs.next()) {
                Vector<String> stu = new Vector<String>();
                stu.add(rs.getString(1));
                stu.add(rs.getString(2));
                stu.add(rs.getString(3));
                stu.add(rs.getString(4));
                stu.add(rs.getString(5));
                stu.add(rs.getString(6));
                stu.add(rs.getString(7));                String sqlforpunish = "select punish_levels.description from punishment,punish_levels where punish_levels.code = punishment.levels and punishment.studentID = "+ "'" + rs.getString(1)+"'";                String sqlforreward = "select reward_levels.description from reward,reward_levels where reward.levels = reward_levels.code  and reward.studentID = "+ "'" + rs.getString(1)+"'";                String sqlchange = "select change_code.description from change_code ,changement where changement.change = change_code.code and changement.studentID = "+"'" + rs.getString(1)+"'";
                ResultSet rsforpunish = dbProcess.executeQuery(sqlforpunish);
                ResultSet rsforreward = dbProcess.executeQuery(sqlforreward);
                ResultSet rsforchange = dbProcess.executeQuery(sqlchange);                String punishstrs = " ";                while(rsforpunish.next()) {
                    punishstrs+= rsforpunish.getString(1) + " ";
                }                String rewardstrs = " ";                while(rsforreward.next()) {
                    rewardstrs+=rsforreward.getString(1)+ " ";
                }                String changestrs = " ";                while(rsforchange.next()) {
                    changestrs+=rsforchange.getString(1)+" ";
                }
                punishstrs=punishstrs.equals(" ")?"无":punishstrs;
                rewardstrs=rewardstrs.equals(" ")?"无":rewardstrs;
                changestrs=changestrs.equals(" ")?"无":changestrs;
                stu.add(punishstrs);
                stu.add(rewardstrs);
                stu.add(changestrs);
                studentvector.add(stu);

最后的一点小处理,真的不能再爱了!!!

图片描述

结果



作者:蒋子默
链接:https://www.jianshu.com/p/39985614f22f


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3370/viewspace-2821077/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-14

  • 博文量
    83
  • 访问量
    732