可以在Java Servlet中像SQL连接一样连接MongoDB吗?

huangapple go评论64阅读模式
英文:

Can MongoDB connection similar to SQL connection in Java servlet?

问题

我有以下的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;
    
    // 指定连接
    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方法
}

我找到的最接近的资源是这个网站。我应该在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");
}
英文:

I have below MongoDB connection method with mLab created and want to do the way like using MySQL to connect the database, but I don't know how to do the next step. I've searched numerous articles online about the MVC folder structure but limited sources to provide how to connect the external database.

    public class MongoDBTest {
    
        private MongoClientURI mongoURI;
        private MongoClient mongoClient;
        private String authorization;
        private List&lt;Document&gt; users = new ArrayList();
        private String owner = &quot;owner&quot;;
        private String password = &quot;password&quot;;
        private String connectionStringPostfix = &quot;ds011288.mlab.com:11288/heroku_xxx&quot;;
        protected MongoCredential credential;
        protected MongoDatabase database; //MongoDB super-class initializes and shares the MongoDatabase
    
    
        //Specify the connection
        public MongoDatabase getMongoDB() {
            MongoClientURI uri = new MongoClientURI(&quot;mongodb://&quot; + this.owner + &quot;:&quot; + 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

The closest resource I've found is this site. Should I declare it on the web.xml

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

and I tried using a similar way to connect mLab in the DAO like below, but failed.

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

答案1

得分: 1

最后,我发现我在连接外部服务器时声明的conntionStringPostfix中漏掉了@符号。然而,我仍然无法连接到云数据库。

英文:

Finally, I found that I missing @ in the conntionStringPostfix that I declared to connect the external server. However, I still can't connect to cloud db.

答案2

得分: -2

尝试将其制作为配置文件,项目一启动就运行,这样能解决你的问题吗?

英文:

Try to make it a configuration file, and the project runs as soon as it starts,Will this solve your problem?

huangapple
  • 本文由 发表于 2020年9月11日 08:58:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63839441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定