【问题标题】:Can MongoDB connection similar to SQL connection in Java servlet?MongoDB 连接可以类似于 Java servlet 中的 SQL 连接吗?
【发布时间】:2020-12-29 12:39:17
【问题描述】:

我已经创建了以下 MongoDB 连接方法和 mLab 并想做类似使用 MySQL 连接数据库的方式,但我不知道下一步该怎么做。我在网上搜索了许多关于 MVC 文件夹结构的文章,但提供如何连接外部数据库的资源有限。

    public class MongoDBTest {
    
        private MongoClientURI mongoURI;
        private MongoClient mongoClient;
        private String authorization;
        private List<Document> users = new ArrayList();
        private String owner = "owner";
        private String password = "password";
        private String connectionStringPostfix = "ds011288.mlab.com:11288/heroku_xxx";
        protected MongoCredential credential;
        protected MongoDatabase database; //MongoDB super-class initializes and shares the MongoDatabase
    
    
        //Specify the connection
        public MongoDatabase getMongoDB() {
            MongoClientURI uri = new MongoClientURI("mongodb://" + this.owner + ":" + this.password + connectionStringPostfix);
            MongoDatabase db;
            try (MongoClient client = new MongoClient(uri)) {
                db = client.getDatabase(uri.getDatabase());
    
            }
            return db;
        }
    
        public MongoDBTest(String owner, String password) throws UnknownHostException {
            this.owner = owner;
            this.password = password;
        }
// addUser method below

我找到的最接近的资源是this 站点。我应该在 web.xml 上声明它

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <display-name>MongoDB Tutorial</display-name>
    <context-param>
        <param-name>MONGODB_HOST</param-name>
        <param-value>localhost</param-value>
    </context-param>
    <context-param>
        <param-name>MONGODB_PORT</param-name>
        <param-value>27017</param-value>
    </context-param>
    <welcome-file-list>
        <welcome-file>productlist.jsp</welcome-file>
    </welcome-file-list>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

我尝试使用类似的方式在 DAO 中连接 mLab,如下所示,但失败了。

public UserDao(MongoClient mc) {
    MongoClientURI uri = new MongoClientURI("mongodb://" + this.owner + ":" + this.password + connectionStringPostfix);
    this.col = mc.getDatabase(uri.getDatabase()).getCollection("User");
}

【问题讨论】:

    标签: java mysql mongodb jsp servlets


    【解决方案1】:

    最后,我发现我在声明连接外部服务器的conntionStringPostfix 中缺少@。但是,我仍然无法连接到云数据库。

    【讨论】:

      【解决方案2】:

      试着把它做成配置文件,项目一启动就运行,这样能解决你的问题吗?

      【讨论】:

      • 如果您能给我举个例子,我们将不胜感激。我是这种语言和 MongoDB 的新手。谢谢。
      • 你的意思是在web.xml中配置吗?
      • 并非总是如此。只要项目在运行,就可以进行mongodb配置。例如,我使用@configuration 注解。然后使用 mongotemplate 来操作 mongodb。你的 mongodbtest 非常好。你可以连接到mongodb。我认为新的 mongoclienturi() 连接路径可以更详细。比如我要连接AA数据库,我新的mongocrienturi(“mongodb://127.0.0.1:27017/AA”)大家知道,每个企业使用不同的框架和不同的解决方案,都得自己摸索解决方案。
      • 你的意思是使用 MongoUri 而不是 MongoClient 对象?
      • 不,我的意思是,连接路径可以详细到数据库
      猜你喜欢
      • 2020-12-17
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 2015-12-12
      • 2013-09-28
      • 1970-01-01
      • 2016-08-28
      • 2014-01-20
      相关资源
      最近更新 更多