如何在OpenShift上启动与JDBC的连接?

问题描述:

在本地主机上工作时,为了启动与JDBC的连接,请执行以下操作:

When I work on my localhost , in order to initiate a connection with JDBC , I do this :

String USERNAME = "...";
String PASSWORD = "...";
String DB_NAME = "...";
String FORNAME_URL = "com.mysql.jdbc.Driver";
String URL = "jdbc:mysql://localhost";
Connection m_connection = DriverManager.getConnection(URL , USERNAME , PASSWORD);

但这在OpenShift上不起作用,没有建立连接.

But this doesn't work on OpenShift , no connection is established .

当我在OpenShift上运行它时,我看不到异常,但是我确认(我在OpenShift上检查了数据库,它的查询尚未更新)未建立连接

I can't see the exception that I'm getting when I run it on OpenShift ,but I validated (I checked the DB on OpenShift , it hasn't been updated with my queries) that the connection is not established

任何想法如何解决?

这不适用于OpenShift,因为OpenSHift公开了必须在应用程序中使用的一组环境变量.您不能使用localhost等属性.请使用以下内容:

This will not work on OpenShift because OpenSHift expose a set of environment variables that you have to use in your application. You can't use localhost, etc properties. Please use following:

String USERNAME = System.getEnv("OPENSHIFT_MYSQL_DB_USERNAME");
String PASSWORD = System.getEnv("OPENSHIFT_MYSQL_DB_PASSWORD");
String DB_NAME = System.getEnv("OPENSHIFT_APP_NAME");
String FORNAME_URL = "com.mysql.jdbc.Driver";
String URL = "jdbc:"+System.getEnv("OPENSHIFT_MYSQL_DB_URL")+ DB_NAME;
Connection m_connection = DriverManager.getConnection(URL , USERNAME , PASSWORD);