java JDBC 无法连接到 openshift 上的 mysql 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16985771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:37:45  来源:igfitidea点击:

JDBC can't connect to mysql database on openshift

javamysqltomcatjdbcopenshift

提问by W.K.S

I managed to set up MySQL database on OpenShift with phpMyAdmin and all. I was told the host name and port my for my database are $OPENSHIFT_MYSQL_DB_HOST and $OPENSHIFT_MYSQL_DB_PORT respectively, which I put in my context.xml file like this:

我设法使用 phpMyAdmin 在 OpenShift 上设置了 MySQL 数据库。有人告诉我,我的数据库的主机名和端口分别是 $OPENSHIFT_MYSQL_DB_HOST 和 $OPENSHIFT_MYSQL_DB_PORT,我将它们放入我的 context.xml 文件中,如下所示:

<context-param>
        <param-name>driver</param-name>
        <param-value>com.mysql.jdbc.Driver</param-value>
    </context-param>
    <context-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/burgerjoint</param-value>
    </context-param>
    <context-param>
        <param-name>user</param-name>
        <param-value>admin******</param-value>
    </context-param>
    <context-param>
        <param-name>password</param-name>
        <param-value>*********</param-value>
    </context-param>

The code to set up the connection is:

建立连接的代码是:

public void contextInitialized(ServletContextEvent event) {
    // Connect
    String driver = event.getServletContext().getInitParameter(PARAM_DRIVER);
    String url = event.getServletContext().getInitParameter(PARAM_URL);
    String username = event.getServletContext().getInitParameter(PARAM_USERNAME);
    String password = event.getServletContext()
        .getInitParameter(PARAM_PASSWORD);

    try {
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, username,
            password);

        event.getServletContext().setAttribute(ATTR_CONNECTION, connection);


    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    }

but the problem is that the connection is null on the server and I don't understand why. Did I do something wrong? The code works when I try it on localhost. And as far as I can tell, I have all necessary libraries:

但问题是服务器上的连接为空,我不明白为什么。我做错什么了吗?当我在本地主机上尝试时,该代码有效。据我所知,我拥有所有必要的库:

enter image description here

在此处输入图片说明

Thanks for the help :)

谢谢您的帮助 :)

Update

更新

I've modified the Connection code as follows:

我修改了连接代码如下:

{
    if (connection != null) return connection;

    try
    {
        Properties dbProperties = new Properties();
        InputStream input = DatabaseUtil.class.getClassLoader().getResourceAsStream(DB_PROPERTIES_FILE);
        dbProperties.load(input);
        String url = "";
        if (appIsDeployed)
        {
        String host = System.getenv("$OPENSHIFT_MYSQL_DB_HOST");
        String port = System.getenv("$OPENSHIFT_MYSQL_DB_PORT");
        String name = "burgerjoint";
        url = "jdbc:mysql://" + host + ":" + port + "/" + name;
        } else
        {
        url = dbProperties.getProperty(PARAM_URL);
        }
        String driver = dbProperties.getProperty(PARAM_DRIVER);
        String username = dbProperties.getProperty(PARAM_USERNAME);
        String password = dbProperties.getProperty(PARAM_PASSWORD);

        Class.forName(driver);
        connection = DriverManager.getConnection(url, username, password);

but it still gives null connection. The values of System.getenv("$OPENSHIFT_MYSQL_DB_HOST")and System.getenv("$OPENSHIFT_MYSQL_DB_PORT")are null.

但它仍然提供空连接。的值System.getenv("$OPENSHIFT_MYSQL_DB_HOST")System.getenv("$OPENSHIFT_MYSQL_DB_PORT")空。

采纳答案by duffymo

This is incorrect:

这是不正确的:

jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/burgerjoint

That dollar sign suggests that you think a proper host and database name will be substituted, but that's not the case.

该美元符号表明您认为将替换正确的主机和数据库名称,但事实并非如此。

Code the host and database name to see that it works, then learn about .properties files to externalize it.

编码主机和数据库名称以查看它是否有效,然后了解 .properties 文件以将其外部化。

回答by JAN

Remove the Dollar signs :

删除美元符号:

   String host = System.getenv("OPENSHIFT_MYSQL_DB_HOST");
   String port = System.getenv("OPENSHIFT_MYSQL_DB_PORT");

And it will work great (I tested it on my machine and on my OpenShift) .

它会很好用(我在我的机器和我的 OpenShift 上测试过)。

回答by Paul Vargas

If you are using the MySQL cartridge, the information to connect to the database are in the environment variablesof the application. If you are using JDBC pure , you need to first retrieve the values??. e.g.:

如果您使用的是 MySQL 卡夹,则连接到数据库的信息在应用程序的环境变量中。如果您使用 JDBC pure ,您需要首先检索值??。例如:

String host = System.getenv("OPENSHIFT_MYSQL_DB_HOST");
String port = System.getenv("OPENSHIFT_MYSQL_DB_PORT");
String username = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME");
String password = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD");

String url = String.format(":mysql://%s:%s/jbossas", host, port);
Connection conn = DriverManager.getConnection(url, username, password);

The jbossasin the string url is the schema. Change with your schema (burgerjoint).

jbossas字符串中URL是架构。随您的架构 ( burgerjoint) 进行更改。

回答by Ashish Chougule

Can you please try to start port-forwarding first.

您能否先尝试启动端口转发。

rhc port-forward -a your-app-name

rhc port-forward -a your-app-name

Once started don't kill this cmd & just use the generated local addresses/ports.

启动后不要终止此 cmd,只需使用生成的本地地址/端口即可。