JDBC无法在OpenShift上连接到mysql数据库 [英] JDBC can't connect to mysql database on openshift

查看:146
本文介绍了JDBC无法在OpenShift上连接到mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

<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();
    }

    }

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

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:

感谢您的帮助:)

更新

我对连接代码进行了如下修改:

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);

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

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

推荐答案

这是错误的:

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.

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

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

这篇关于JDBC无法在OpenShift上连接到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆