MongoDB Learning (ii) Java connection

Source: Internet
Author: User
Tags mongoclient

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.