将数据插入数据库(mongodb)

时间:2014-11-25 16:01:18

标签: java swing mongodb

我已经使用以下代码将数据插入到mongodb数据库中......这个问题我必须明确指定要输入的数据但是我需要动态地执行...在某种意义上使用一个GUI,无论在文本框中输入的内容都必须放入数据库中。

 private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 512, 355);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JLabel lblNewLabel = new JLabel("Name");
    lblNewLabel.setBounds(42, 33, 95, 30);
    frame.getContentPane().add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("Manufacturer");
    lblNewLabel_1.setBounds(42, 74, 80, 30);
    frame.getContentPane().add(lblNewLabel_1);

    textField = new JTextField();
    textField.setBounds(147, 33, 122, 25);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.setBounds(147, 79, 122, 25);
    frame.getContentPane().add(textField_1);
    textField_1.setColumns(10);

    JButton btnInsert = new JButton("Insert");
    btnInsert.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

             MongoClient mongoClient = null;
                DBCursor cursor = null;
                try {
                    mongoClient = new MongoClient( "localhost" , 27017 );
                    DB db = mongoClient.getDB( "sample" );
                    DBCollection coll = db.getCollection("sample");

                    BasicDBObject doc = new BasicDBObject("title", "MongoDB").
                            append("name","a" ).
                            append("manufacturer", "b").
                            append("colour", "c").
                            append("price", "d");
                            coll.insert(doc);

                }catch(Exception e){
                    System.err.println( e.getClass().getName() + ": " + e.getMessage() );}

        }
    });
    btnInsert.setBounds(148, 223, 89, 23);
    frame.getContentPane().add(btnInsert);

    JLabel lblNewLabel_2 = new JLabel("Colour");
    lblNewLabel_2.setBounds(42, 127, 65, 25);
    frame.getContentPane().add(lblNewLabel_2);

    textField_2 = new JTextField();
    textField_2.setBounds(147, 129, 122, 25);
    frame.getContentPane().add(textField_2);
    textField_2.setColumns(10);

    JLabel lblNewLabel_3 = new JLabel("Price");
    lblNewLabel_3.setBounds(37, 175, 70, 25);
    frame.getContentPane().add(lblNewLabel_3);

    textField_3 = new JTextField();
    textField_3.setBounds(147, 177, 122, 25);
    frame.getContentPane().add(textField_3);
    textField_3.setColumns(10);
    }
}

1 个答案:

答案 0 :(得分:0)

  

"我有一个前端,有4个文本框说"名称","制造商","颜色"和"价格" ....所以无论我在文本框中输入什么,都应该放入数据库....但是在上面的代码插入只能通过明确指定值来完成....所以我需要为一般化的方法更改这个"

只需将其更改为:

String name = nameField.getText();
String manufacturer = manufacturerField.getText();
String color = colorField.getText();
String price = priceField.getText();

BasicDBObject doc = new BasicDBObject("title", "MongoDB").
        append("name", name ).
        append("manufacturer", manufacturer).
        append("colour", color).
        append("price", price);
        coll.insert(doc);

这就是你要找的东西吗?