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

前文

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

正文

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 stu = new Vector();
                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);

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

【数据库课设进行时】多表查询_第1张图片
结果

你可能感兴趣的:(【数据库课设进行时】多表查询)