新版跟旧版差别很大,很多函数已经过时,下面的代码使用的是:
mongodb-driver-core-3.12.10.jar
mongo-java-driver-3.12.10.jar
1。连接数据库
ServerAddress serverAddress = new ServerAddress("192.168.1.1", 27017);
// 用户名 数据库名称 密码
MongoCredential mongoCredential = MongoCredential.createScramSha1Credential("test", "admin", "abc".toCharArray());
MongoClientOptions.Builder options = new MongoClientOptions.Builder();
options.cursorFinalizerEnabled(true);
options.connectionsPerHost(300);// 连接池设置为300个连接,默认为100
options.connectTimeout(30000);// 连接超时,推荐>3000毫秒
options.maxWaitTime(5000); //
options.socketTimeout(0);// 套接字超时时间,0无限制
MongoClientOptions mongoClientOptions = options.build();
mongoClient = new MongoClient(serverAddress, mongoCredential, mongoClientO