首页>代码>运用NoSQL数据库的学生成绩管理系统,采用Java Swing>/Student_MongoDB/src/com/student/GradeSort.java
package com.student;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.bson.Document;

import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;

public class GradeSort {

	public static void SortGrade(MongoCollection collection)
	{
		Vector columnNames = null;
	    Vector rowData = null;
	    
	    JFrame jf = new JFrame();
		Container c=jf.getContentPane();
		
	    JTable jtable;
	    DefaultTableModel tableModel;
	    
		JScrollPane jscrollpane = new JScrollPane();
		c.add(jscrollpane, BorderLayout.CENTER);
		
		columnNames = new Vector();
		columnNames.add("学号");
        columnNames.add("姓名");
        columnNames.add("性别");
        columnNames.add("年龄");
        columnNames.add("学院");
        columnNames.add("成绩");
        rowData = new Vector();
        
        FindIterable<Document> it = collection.find().sort(new BasicDBObject("grade", -1));//降序排序
		//获取游标对象
		MongoCursor<Document> cursor = it.iterator();
		while(cursor.hasNext())
		{
			//取出每一个文档对象(行)
			Document doc = cursor.next();
			
			Vector hang = new Vector();
			hang.add(doc.getInteger("no"));
			hang.add(doc.getString("name"));
			hang.add(doc.getString("gender"));
			hang.add(doc.getInteger("age"));
			hang.add(doc.getString("academy"));
			hang.add(doc.getInteger("grade"));
			rowData.add(hang);
			//String name = doc.getString("name");
			//int age = doc.getInteger("age");
			//System.out.println(name+","+age);
			
		}
		tableModel = new DefaultTableModel(rowData, columnNames);
		jtable = new JTable(tableModel);//利用表格模型创建表格对象
		jscrollpane.setViewportView(jtable);
		
		JPanel jp = new JPanel();
		c.add(jp,BorderLayout.SOUTH);
		JButton jb_Pai1 = new JButton("前十名");
		JButton jb_Pai2 = new JButton("十一到二十名");
		JButton jbback = new JButton("返回");
		jp.add(jb_Pai1);jp.add(jb_Pai2);jp.add(jbback);
		
		jbback.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				jf.dispose();
			}
		});
		jb_Pai1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				new PaiMing().SortGrade(collection);
				//jf.dispose();
			}
		});
		jb_Pai2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				new PaiMing().SortGrade1(collection);
				//jf.dispose();
			}
		});
		
		
		jf.setTitle("成绩排序");
		jf.setSize(650,500);
		jf.setLocation(100, 100);
		jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		jf.setVisible(true);
	}
}
最近下载更多
husiyu  LV3 2023年9月7日
1362440352  LV1 2022年11月27日
asdfasfsf  LV1 2022年6月15日
testuser1234567  LV24 2022年5月31日
syj991023  LV2 2022年5月13日
wyx065747  LV67 2022年5月7日
wanglinddad  LV54 2022年4月2日
zydandzjf  LV3 2022年1月6日
希雨逍遥  LV6 2021年6月22日
xerxes_xxi  LV1 2021年6月17日
最近浏览更多
xinnnnn  LV1 2月26日
颜伟峰  LV1 2023年12月20日
sunshine9920  LV12 2023年10月19日
husiyu  LV3 2023年9月7日
卢本伟不开挂  LV4 2023年9月3日
Google刘总的 2023年7月5日
暂无贡献等级
hongdongdong  LV12 2023年6月30日
KAIzx11  LV7 2023年6月25日
微信网友_6184005683466240  LV11 2023年6月11日
tianyuboy111  LV3 2023年5月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友