Jefe infiltrado
Jefe infiltrado

Reputation: 382

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.

Upvotes: 1

Views: 548

Answers (1)

user2879327
user2879327

Reputation:

You should try using the pre-defined MySQL Data Source that comes with the Tomcat cartridges, you can learn more about it here: https://developers.openshift.com/en/tomcat-ds.html

Upvotes: 2

Related Questions