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