Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    public static void main(String args[]) {
        try {
            MongoClient mongoClient = new MongoClient("localhost", 27017);
            DB db = mongoClient.getDB("test");
            System.out.println("Connect to database successfully");
            boolean auth = db.authenticate("myUserName", "myPassword".toCharArray());
            System.out.println("Authentication: " + auth);
            DBCollection coll = db.createCollection("mycol", null);
            System.out.println("Collection created successfully");
            coll = db.getCollection("mycol");
            System.out.println("Collection mycol selected successfully");
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }
}