Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB连接能否与Javaservlet中的SQL连接相似?_Java_Mysql_Mongodb_Jsp_Servlets - Fatal编程技术网

MongoDB连接能否与Javaservlet中的SQL连接相似?

MongoDB连接能否与Javaservlet中的SQL连接相似?,java,mysql,mongodb,jsp,servlets,Java,Mysql,Mongodb,Jsp,Servlets,我在下面创建了一个带有mLab的MongoDB连接方法,我想用MySQL连接数据库,但我不知道下一步怎么做。我在网上搜索了很多关于MVC文件夹结构的文章,但提供如何连接外部数据库的资料有限 public class MongoDBTest { private MongoClientURI mongoURI; private MongoClient mongoClient; private String authorization;

我在下面创建了一个带有mLab的MongoDB连接方法,我想用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

尝试将其设置为一个配置文件,项目一开始就运行,这能解决您的问题吗?

最后,我发现我声明连接外部服务器的
连接字符串后缀中缺少@。但是,我仍然无法连接到cloud db。

如果您能给我举个例子,我将不胜感激。我不熟悉这种语言和MongoDB。谢谢。你是说在web.xml中配置吗?不一定。只要项目正在运行,就可以执行mongodb配置。例如,我使用@configuration注释。然后使用mongotemplate操作mongodb。你的mongodbtest很好。您可以连接到mongodb。我认为新的mongoclienturi()连接路径可以更详细。例如,我想连接到AA数据库,我的新MongoClientUri(“mongodb://127.0.0.1 :27017/AA)正如您所知,每个企业使用不同的框架和不同的解决方案,他们必须探索自己的解决方案。您的意思是使用MongoUri而不是MongoClient obejct?不,我的意思是,连接路径可以详细到数据库
<?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>
public UserDao(MongoClient mc) {
    MongoClientURI uri = new MongoClientURI("mongodb://" + this.owner + ":" + this.password + connectionStringPostfix);
    this.col = mc.getDatabase(uri.getDatabase()).getCollection("User");
}