繁体   English   中英

在openshift上连接到jdbc mysql

[英]Connect to jdbc mysql on openshift

我尝试在openshift Tomcat服务器上从Java连接到mysql。 服务器工作正常,但与MySQL的连接根本不起作用。

我在网上找到了很多东西,但是就解决我的问题我什至不了解。

public Connection getConnection() throws ClassNotFoundException, SQLException {
        Connection connection = null;
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(
                    "jdbc:mysql://999.999.999.999:9999/MyDatabase", "MyUser", "MyPassword");
        return connection;
}

public static DataSource getDataSource() throws NamingException{
        InitialContext initContext = new InitialContext();
        Context env = (Context)initContext.lookup("java:comp/env");
        DataSource ds = (DataSource)env.lookup("jdbc/MyDatabase");
        return ds;
}

Connection connection = Datasource.getDataSource().getConnection();

我已将其添加到我的web.xml中:

  <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/MyDatabase</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

我无法建立连接,我也可以看到似乎没有人尝试以此方式连接。 我究竟做错了什么?

还有一个问题:如何或在哪里看到错误消息。 我直接从Eclipse推送项目。

您应该尝试使用Tomcat墨盒随附的预定义的MySQL数据源,您可以在此处了解更多信息: https : //developers.openshift.com/en/tomcat-ds.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM