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

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:

driver

com.mysql.jdbc.Driver

url

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

user

admin******

password

*********

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:

K6Sh9.png

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.

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值