package com.student;

import static com.mongodb.client.model.Accumulators.avg;
import static com.mongodb.client.model.Accumulators.max;
import static com.mongodb.client.model.Accumulators.min;
import static com.mongodb.client.model.Accumulators.sum;
import static com.mongodb.client.model.Aggregates.group;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
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.AggregateIterable;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;

public class JuHe {

	public static void Group(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("平均分");
        rowData = new Vector();
        
        //用聚合框架中的group来操作数据,进行求值
        AggregateIterable<Document> iterable = collection.aggregate(Arrays.asList(group("$academy",
				sum("number",1), max("max","$grade"),min("min","$grade"),avg("avg","$grade"))));
		//获取游标对象
        MongoCursor<Document> cursor = iterable.iterator();
		while(cursor.hasNext())
		{
			//取出每一个文档对象(行)
			Document doc = cursor.next();
			
			Vector hang = new Vector();
			hang.add(doc.getString("_id"));
			hang.add(doc.getInteger("number"));
			hang.add(doc.getInteger("max"));
			hang.add(doc.getInteger("min"));
			hang.add(doc.getDouble("avg"));
			rowData.add(hang);
			
		}
		tableModel = new DefaultTableModel(rowData, columnNames);
		jtable = new JTable(tableModel);//利用表格模型创建表格对象
		jscrollpane.setViewportView(jtable);
		
		JPanel jp = new JPanel();
		c.add(jp,BorderLayout.SOUTH);
		JButton jbback = new JButton("返回");
		jp.add(jbback);
		
		jbback.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				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  LV55 2022年4月2日
zydandzjf  LV3 2022年1月6日
希雨逍遥  LV6 2021年6月22日
xerxes_xxi  LV1 2021年6月17日
最近浏览更多
lysnb3  LV1 2024年7月2日
yqlllll  LV2 2024年6月4日
heshao  LV2 2024年6月4日
hfffff  LV1 2024年6月3日
sunrandong  LV4 2024年4月29日
xinnnnn  LV1 2024年2月26日
颜伟峰  LV1 2023年12月20日
sunshine9920  LV12 2023年10月19日
husiyu  LV3 2023年9月7日
卢本伟不开挂  LV4 2023年9月3日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友