The last chapter is over MongoDB installation and IDE tools, now start using Java to connect.
The first step: using the jar package,
Here need three packages, specifically why I do not know, anyway, because of the error, I have to follow the official documents are downloaded.
Link: https://pan.baidu.com/s/1zCUBgXA96_SGFjVJ9p16hg Password: wlp3
D:\jar\mongodb\bson-3.5.0.jar
D:\jar\mongodb\mongodb-driver-core-3.5.0.jar
D:\jar\mongodb\mongodb-driver-3.5.0.jar
The second step is the code:
Import Com.mongodb.mongoclient;import com.mongodb.client.mongodatabase;/** * @author szy * @version Created on: 2018-3-31 pm 9:48:59 * */public class Mongodbtest {/** * @param args */public static void main (string[] args) {// TODO auto-generated method Stubmongoclient mongoclient = new Mongoclient ("localhost", 27017); Mongodatabase mgdb = mongoclient.getdatabase ("test"); System.out.println ("Connect to Database successfully!"); System.out.println ("Mongodatabase inof is:" +mgdb.getname ());}}
The output is:
Connect to Database successfully! Mongodatabase inof Is:test
Ok. Test the connection successfully.
MongoDB Learning (ii) Java connection