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

我设法用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>

设置连接的代码是:

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上尝试时,代码可以正常工作.据我所知,我有所有必要的库:

谢谢您的帮助 :)

更新

修改了连接代码如下:

{
    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”)的值为空.

解决方法:

这是不正确的:

jdbc:MysqL://$OPENSHIFT_MysqL_DB_HOST:$OPENSHIFT_MysqL_DB_PORT/burgerjoint

那个美元符号表明你认为一个合适的主机和数据库名称将被替换,但事实并非如此.

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

相关文章

连接数据库的方式:第一种方式:ODBC:开放数据库连接是微软...
JDBCRequest 使用VariableNamesmysql:数据库连接池对象var...
 1.JDBCDBC(JavaDataBaseConnectivity):Java数据库连接技术...
1.需要jar包的支持:java.sqljavax.sqlmysql-conneter-java....
1.简介Activiti是一个业务流程管理(BPM)框架,它是覆盖了业务...
1.JDBC体系系统一组规范:接口JDBC接口(API)包括两个层次:...