Python社区  »  MongoDB

在JTable组件中显示MongoDB数据库中的数据

d.haythem10 • 5 月前 • 100 次点击  

最近几天我一直在和MongoDB和JTable打交道,所以我的问题是我想在表中显示MongoDB数据库中的数据,但是出了问题,我搞不清楚。我的代码是:

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;


public class MongoJTable {
    int i=0;


    public static void main(String[] args) {
        final JFrame frame = new JFrame("JTable Demo");
        int y=0;
        String [] columns1 = null;
        List<String> animals = new ArrayList<>();

        Mongo mongo = new Mongo("localhost",27017);
        DB db = mongo.getDB("user");
        DBCollection DBCollection = db.getCollection("pfe");


        DBCursor cursor = DBCollection.find();
        /*String[] temps = new String [cursor.size()+1];
        for (String key: cursor.next().keySet()) {
            // do whatever with the key name here, f.ex.
            System.out.println(key);
           animals.add(key.toString());

        }*/
        String[] nameList = new String[cursor.size()];

        DBObject obj = cursor.next(); 
        Set<String> keys = obj.keySet(); 
        Iterator iterator = keys.iterator();

        /*while(iterator.hasNext()){

              String key = (String) iterator.next().toString();
              nameList[y] =key;
              y++;
              /* Do whatever you want 
            }*/
        for(int i = 0; i <cursor.size(); i++){
            nameList[i] = cursor.next().keySet().toString();
            System.out.println(nameList[i]);
            break;
        }

        //String[] columns = {"Code", "Name", "High", "Low", "Close", "Volume", "Change","Change %"};
        //System.out.println(Arrays.deepToString(columns));

        DefaultTableModel model = new DefaultTableModel(nameList, 0);
        Object[][] data = {
//                {"MBF", "CITYGROUP", 10.16, 10.16, 10.16, 200, 0.08,0.79},
                //{"MBL", "BANK OF AMERICA", 12.66, 12.66, 12.66, 6600, 0.13,1.04},
                //{"MJP", "Morgan Stanley Dean Witter & Co.", 24.97, 24.97, 24.97, 1000, -0.04,-0.16}
            };


        JTable table = new JTable(data,nameList);
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        JLabel lblHeading = new JLabel("Stock Quotes");
        lblHeading.setFont(new Font("Arial",Font.TRUETYPE_FONT,24)); 
        frame.getContentPane().setLayout(new BorderLayout()); 
        frame.getContentPane().add(lblHeading,BorderLayout.PAGE_START);
        frame.getContentPane().add(scrollPane,BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(550, 200);
        frame.setVisible(true);
    }
}

我设法将mongo DB中的密钥集保存在数组字符串列表“nameslist”中,但是当我将其传递给defaulttableModel时,它将不起作用,尽管当我使用预定义的列表“columns my code works”更改数组字符串列表时 enter image description here 你们能帮帮忙吗,任何建议都可能有用,谢谢你们抽出时间。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/52880
 
100 次点击  
分享到微博