java连接mongoDB《安全验证》

1. 需要jar包:

maven依赖:

		<dependency>
			<groupId>org.mongodb</groupId>
			<artifactId>mongo-java-driver</artifactId>
			<version>2.12.4</version>
		</dependency>


2.设置db数据库的安全验证机制:

  ①创建用户

 ②在开启mongoDB的时候使用安全模式:


3.java代码,实现CRUD。

 

package mongoDb;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

public class MongoDBUtil {
    static MongoClient client;
    static DB db;
    static DBCollection collection;

    static {
        try {
            // client = new MongoClient("192.168.20.114", 27017);
            System.out.println("连接服务器测试.................");
            ServerAddress serverAddress = new ServerAddress("192.168.20.114", 27017);
            List<ServerAddress> seeds = new ArrayList<ServerAddress>();
            seeds.add(serverAddress);
            MongoCredential credentials = MongoCredential.createMongoCRCredential("testAdmin", "test",
                    "123".toCharArray());
            List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();
            credentialsList.add(credentials);
            client = new MongoClient(seeds, credentialsList);
            // (List<ServerAddress> seeds, List<MongoCredential//
            db = client.getDB("test");
            // db.authenticate("testAdmin", "123".toCharArray());
            collection = db.getCollection("user");
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void add(DBObject dbObject) {
        collection.insert(dbObject);
    }

    public static void del(DBObject dbObject) {
        collection.remove(dbObject);
    }

    public static void update(DBObject dbObject1, DBObject dbObject2) {
        collection.update(dbObject1, dbObject2);
    }

    public static DBObject queryOneByKey(DBObject dbObject, DBObject keys) {
        DBCursor dbCursor = collection.find(dbObject, keys);
        if (dbCursor.hasNext()) {
            return dbCursor.next();
        }
        return null;
    }

    public static java.util.List<DBObject> query(DBObject dbObject) {
        DBCursor dbCursor = collection.find(dbObject);
        List<DBObject> ret = new ArrayList<DBObject>();
        while (dbCursor.hasNext()) {
            DBObject dbObject2 = dbCursor.next();
            ret.add(dbObject2);

        }
        return ret;
    }

    
    
    public static void main(String[] args) {
        BasicDBObject dbObject = new BasicDBObject();
        dbObject.put("num", new BasicDBObject("$gt", 0));
        List<DBObject> ret = MongoDBUtil.query(dbObject);
        for (Iterator<DBObject> it = ret.iterator(); it.hasNext();) {
            System.out.println(it.next());
        }
    }

}


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值