【问题标题】:Connect to jdbc mysql on openshift在 openshift 上连接到 jdbc mysql
【发布时间】:2023-04-07 17:17:02
【问题描述】:

我尝试在 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 推送项目。

【问题讨论】:

    标签: java mysql jdbc openshift


    【解决方案1】:

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

    【讨论】:

    • 非常感谢,没想到这么简单!
    猜你喜欢
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多