insert data into database(mongodb)

darshan chandra

I have used the following code to insert data into the mongodb database.....the problem with this i have to explicitly specify the data to be entered but however I need to do it dynamically...in the sense using a GUI, whatever has been entered int the text box must be put into the database.

 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);
    }
}
Paul Samsotha

"i have a front end which has 4 text boxes saying "name","manufacturer","colour" and "price"....so whatever i enter in the text box should be put into the database....however in the above code insertion can be done only by explicitly specifying the values....so i need to change this for a generlized method"

Just change it to something like:

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);

Is that what you're looking for?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related