Java 如何克服这个错误;com.mongodb.diagnostics.logging.JULLogger log“日志”;

Java 如何克服这个错误;com.mongodb.diagnostics.logging.JULLogger log“日志”;,java,database,mongodb,jdbc,nosql,Java,Database,Mongodb,Jdbc,Nosql,我正在使用与MongoDB连接的java程序,当我运行该程序时,它将显示一个错误,但代码正在工作。 MongoDB有一个名为MongoDB的数据库,其中有一个名为seatbooking的集合,有两列(名称、seatnumber)。 这是我的代码: MongoClient mongoClient = new MongoClient("localhost", 27017); System.out.println("connection is established"); MongoDatabase

我正在使用与MongoDB连接的java程序,当我运行该程序时,它将显示一个错误,但代码正在工作。 MongoDB有一个名为MongoDB的数据库,其中有一个名为seatbooking的集合,有两列(名称、seatnumber)。 这是我的代码:

MongoClient mongoClient = new MongoClient("localhost", 27017);
System.out.println("connection is established");

MongoDatabase mongoDatabase = mongoClient.getDatabase("MongoDB");
MongoCollection mongoCollection = mongoDatabase.getCollection("seatbooking");

Document document = new Document("name","shenal");
document.append("seatnumber",20);
mongoCollection.insertOne(document);
运行此代码时,我的输出为:

> Mar 09, 2020 12:41:36 PM
> com.mongodb.diagnostics.logging.JULLogger log INFO: Cluster created
> with settings {hosts=[localhost:27017], mode=SINGLE,
> requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms',
> maxWaitQueueSize=500}
> **connection is established** Mar 09, 2020 12:41:36 PM com.mongodb.diagnostics.logging.JULLogger log INFO: Cluster
> description not yet available. Waiting for 30000 ms before timing out
> Mar 09, 2020 12:41:36 PM com.mongodb.diagnostics.logging.JULLogger log
> INFO: Opened connection [connectionId{localValue:1, serverValue:309}]
> to localhost:27017 Mar 09, 2020 12:41:36 PM
> com.mongodb.diagnostics.logging.JULLogger log INFO: Monitor thread
> successfully connected to server with description
> ServerDescription{address=localhost:27017, type=STANDALONE,
> state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 2,
> 2]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216,
> logicalSessionTimeoutMinutes=30, roundTripTimeNanos=5168100} Mar 09,
> 2020 12:41:36 PM com.mongodb.diagnostics.logging.JULLogger log INFO:
> Opened connection [connectionId{localValue:2, serverValue:310}] to
> localhost:27017

日志输出不会显示
insertOne
操作的任何结果

如果您使用的是4.0之前的MongoDB Java驱动程序版本,则不会返回确认对象。但是,版本4的
insertOne
方法返回一个对象。这是4.0中的一个新特性(在以前版本的中,该方法返回了一个
void

您可以使用以下代码检查插入的结果,如下所示(版本4.0):


这里怎么了?这里是写错误的地方。这似乎是如何避免这个错误我不明白的错误是什么
try {
    InsertOneResult insertResult = collection.insertOne(document);
    System.out.println("Document inserted with ID: " + insertResult.getInsertedId());
}
catch(MongoWriteException e) {
    // write failure happened, handle it here...
}