java 如何选出成绩排前5名的学生呢
发布网友
发布时间:2024-09-05 00:13
我来回答
共2个回答
热心网友
时间:2024-09-14 12:16
???数据库?还是什么呢?详细一些 。
热心网友
时间:2024-09-14 12:12
select top 5 s.stu_id,s.stu_name,c.classname,s.score from student s,classes c where s.classes_id=c.classes_id and c.classesname='java一班' order by s.score desc
在sql server2000中这样写就是输出java一班同学成绩前五名了
在java控制台要输出的话:
把结果集取出来封装到一个javabean中,如
//Student类
public class Student {
private Classes classes;
private String stu_name;
private float EnglishScore;
/** 默认构造方法 */
public Student() {
}
/** 完全构造方法 */
public Student(Classes classes, String stu_name, float englishScore) {
this.classes = classes;
this.stu_name = stu_name;
this.EnglishScore = englishScore;
}
public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public float getEnglishScore() {
return EnglishScore;
}
public void setEnglishScore(float englishScore) {
EnglishScore = englishScore;
}
}
//Classes类 get set方法先省略,自己补上
public class Classes {
private int classesid;
private String classesname;
}
//在DAO包中结果集的循环里取出结果集封装到Classes类和Student类的完全构造方法中
List<Student> list=new LinkedList<Student>();//声明链表
while(rs.next()){
Classes classes=new Classes(classesid,classesname)
Student stu=new (classes, stu_name, classesid,englishScore)
//再把stu放进链表
list.add(stu)
}
//封装完毕
for(Student stu:list){
System.out.println("班级:"+stu.classes.classesname);
System.out.println("姓名:"+stu.stu_name);
System.out.println("分数:"+stu.EnglishScore);
... ...
}