Java connection Mongodb detailed resolution

First: installed Java environment + MongoDB JDBC driver

: You can put the downloaded mongo-java-driver-3.8.1.jar anywhere on your computer. Just import it in the java project.

Import method:

Create a new Java project NewMongoDB in Eclipse, right click: Build Path -> Add external Archives ... -> Import the jar package you downloaded into

The situation is as follows:

Java The simple test procedure for connecting MongoDB is as follows:

The specific code is as follows:

package com.mongodb.test01;

Import com.mongodb.DB;
Import com.mongodb.MongoClient;

Public class MongoDemoA {
Public static void main(String[] args) throws Exception {

/ / Set the host name and port number of the database to be connected
MongoClient client = new MongoClient("localhost" , 27017);

DB db = client.getDB("test");//Connect to the database

For(String name : db.getCollectionNames()) {
System.out.println("collection name"+name);

where the code is set to access the database "test" (can be arbitrary), showing the name of the collection in the database.

Results are as follows: