List of usage examples for com.mongodb MongoCredential MongoCredential
MongoCredential(@Nullable final AuthenticationMechanism mechanism, @Nullable final String userName, final String source, @Nullable final char[] password)
From source file:NegotiatedAuthenticationProtocolExample.java
License:Apache License
public static void main(String[] args) throws UnknownHostException, InterruptedException { String server = args[0];/*w w w . j a v a 2s . co m*/ String user = args[1]; String pwd = args[2]; String db = args[3]; MongoCredential credentials = new MongoCredential(user, pwd.toCharArray(), MongoAuthenticationProtocol.NEGOTIATE, db); MongoClient mongoClient = new MongoClient(new ServerAddress(server), Arrays.asList(credentials), new MongoClientOptions.Builder().build()); DB testDB = mongoClient.getDB(db); testDB.getCollection("test").insert(new BasicDBObject()); System.out.println("Count: " + testDB.getCollection("test").count()); }