简体   繁体   中英

Connect to jdbc mysql on openshift

I try to connect from java to mysql on an openshift tomcat server. The server is working fine, but the connection to MySQL does not work at all.

I found a lot on this on the web, but nothing I could understand as far as to solve my problem.

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();

I have added this to my 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>

I could not establish a connection and I also can see that noone seems to try to connect in this way. What am I doing wrong?

And one additional question: how or where can I see error messages. I am pushing the project directly from Eclipse.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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