Java Connection MongoDB

Source: Internet
Author: User
Tags mongodb driver

1. Create a connected user

> MONGO Ip:port

> Use test

< Span style= "font-family: Microsoft Ya-hei, song body, Arial; font-size:15px; line-height:24px; White-space:pre "> > db.adduser (" root "," 123456 ")

>db.auth ("root", "123456") (Login verification)

2. Java connection to MongoDB driver ,:https://github.com/mongodb/mongo-java-driver/downloads.

3. Test code

Import Java.net.unknownhostexception;import Com.mongodb.db;import Com.mongodb.mongo;import Com.mongodb.mongoexception;public class Util {/** * @param args * @throws mongoexception  * @throws Unknownhostexcepti On  */public static void Main (string[] args) {Mongo m;try {m = new Mongo ("192.168.86.1", 27027);//<span style= "Font -family:arial; " >192.168.86.1 for ip,27027mongodb port </span>db DB = M.getdb ("test");        if (db.authenticate ("root", "123456". ToCharArray ())) {            System.out.println ("success");;        }        Else System.out.println ("false");} catch (Unknownhostexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (Mongoexception e) {//TODO Auto-generated catch Blocke.printstacktrace ();}}}        



Java Connection MongoDB

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.