0

I have a Collection named school and i wanted to insert document using java.I used the below code

import com.mongodb.MongoClient;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;

public class MongoDBJDBC{
public static void main( String args[] ){
  try{   
 // To connect to mongodb server
     MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
     // Now connect to your databases
     DB db = mongoClient.getDB( "cms" );
 System.out.println("Connect to database successfully");
     boolean auth = db.authenticate(myUserName, myPassword);
System.out.println("Authentication: "+auth);         
     DBCollection school = db.getCollection("school");
     System.out.println("Collection mycol selected successfully");
     BasicDBObject doc = new BasicDBObject("title", "name").
        append("description", "about the school").
        append("likes", 1000).
        append("url", "http://www.tutorialspoint.com/mongodb/").
        append("by", "mytutorials point");
     school.insert(doc);
     System.out.println("Document inserted successfully");
  }catch(Exception e){
     System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  }

} }

and its working fine.

In the above program the document values are hardcoded in which in application environment the user cant see the source code.Now i want to insert document values as user entry from UI.Please Help me

5
  • i knw its a silly question ..since im new to mongodb i cant unserstand the conecpt..dont mistake me Commented Oct 6, 2014 at 8:37
  • friends please help me in this Commented Oct 6, 2014 at 9:06
  • What kind of UI are you referring to? is it web based or swing based? Commented Oct 6, 2014 at 10:15
  • i want to use a web based UI.for example spring Commented Oct 6, 2014 at 10:25
  • You could try the below answer from Shastry's post Commented Oct 6, 2014 at 11:38

1 Answer 1

1

Consider You have a form as below:

<form action="/test/dbOperation" method="POST">
    <input type="text" id="title" name="title">
    <input type="text" id="description" name="description">
    <input type="text" id="likes" name="likes">
    <input type="text" id="url" name="url">
    <input type="text" id="by" name="by">
    <button type="submit" value="Submit">
</form>

Now in Spring you can use annotation to make a class as Controller where you need to handle the request

@Controller
@RequestMapping("/test")
public class formCntrl {
    @RequestMapping(value = "/dbOperation", method = RequestMethod.POST)
    public @ResponseBody String (HttpServletResponse resp,
        @RequestParam("title") String title,
        @RequestParam("description") String description,
        @RequestParam("likes") int likes,
        @RequestParam("url") String url,
        @RequestParam("by") String by) {
            try{   
                // To connect to mongodb server
                MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
               // Now connect to your databases
               DB db = mongoClient.getDB( "cms" );
               System.out.println("Connect to database successfully");
               boolean auth = db.authenticate(myUserName, myPassword);
               System.out.println("Authentication: "+auth);         
               DBCollection school = db.getCollection("school");
               System.out.println("Collection mycol selected successfully");
               BasicDBObject doc = new BasicDBObject("title", title).
                   append("description", description).
                   append("likes", likes).
                   append("url", url).
                   append("by", by);
               school.insert(doc);
               System.out.println("Document inserted successfully");
             }catch(Exception e){
                 System.err.println( e.getClass().getName() + ": " + e.getMessage() );
             }
    }
}

Generally its not a good deal to mix up the database logic in controller. Use Spring MVC where in you can have DAO layer which provides separation of database logic with business logic.

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

thats awesome.its really helped me ..great job shastry

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.