gpt4 book ai didi

MongoDB database name is getting displayed after connection closing(连接关闭后显示MongoDB数据库名称)

转载 作者:bug小助手 更新时间:2023-10-25 15:09:20 26 4
gpt4 key购买 nike



At method end, I have closed the connection, but it still shows the DB name -

在方法结束时,我已经关闭了连接,但它仍然显示数据库名称-


public void fncConnectToMongoDB2() throws SQLException, ClassNotFoundException {
try {

// Connect to the MongoDB server
MongoClient mongoClient = new MongoClient("localhost", 27017);

// Access a specific database
MongoDatabase database = mongoClient.getDatabase("TheDB");
System.out.println("mongo db connected");
System.out.println(database.getName());

//Closing the active MongoDB connection
mongoClient.close();
System.out.println("mongo db closed");
System.out.println(database.getName());
}

}

}


Output -

输出-


mongo db connected
TheDB
mongo db closed
TheDB

更多回答

It will show the name because you are storing the result of mongoClient.getDatabase("TheDB") in a variable database. Once something is stored in the variable it doesn't change its value unless and until someone updates the variable. Therefore, even after closing your mongo connection, your are able to see the database name.

它将显示名称,因为您将mongoClient.getDatabase(“TheDB”)的结果存储在一个变量数据库中。一旦将某些内容存储到变量中,它就不会更改其值,除非有人更新了该变量。因此,即使在关闭您的Mongo连接之后,您也能够看到数据库名称。

优秀答案推荐
更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com