user41912 user41912 - 1 year ago 177
Java Question

OpenShift webapps mysql java connection

I have a JBoss EWS Tomcat 7 Java application on openshift, installed with the MySQL 5.5 and PhpMyAdmin 4.0 cartridges. I have copied the repository locally, and deleted the source folder. Instead, I simply have a compiled Java application, a WAR file, that has been copied into the webapps directory and git pushed to the OpenShift servers.

If I have some database, how would I be able to access it in the webapp compiled war file. Would standard MySQL JDBC work? According to some posts I have read, OpenShift blocks the accessing of external requests to the database, and in this case, the webapp directory's war file may be making an "external request". The source for that is here: (as answered by reputable OpenShift developers).

I actually tried JDBC in the past and it did not work, but that may have been due to incorrect code. If someone wants to write some code that shows how this would be done, I would appreciate it. And if you could test it that would be even nicer :)

By the way, in the answer please do not include port forwarding. I know that works, I have tried it before, and port forwarding works. But it is often a little insecure, and has to be started each time from a computer.

Answer Source

I have resolved the issue by myself and was extremely excited. Although I did not receive help myself, I hope someone else comes along this thread and is able to get it working!

So I have a Vaadin application, which has been compiled into a WAR file. I deployed it to OpenShift servers by the following steps:

Deployment of compiled webapp to OpenShift

  1. Open OpenShift in your web browser. Log in to Openshift. Navigate to the application in question.
  2. Get the ssh code of that application (it should be right of screen), located to the right of the cartridges. Copy that code using Command-C or Ctrl-C.
  3. Open Terminal and type git clone ssh:\\xxxxxxxxxxxxxxxx...
  4. If you are on Mac, like I am, it should create a project directory at Users/Username/Appname. Inside that directory, delete the source folder and pom.xml. Take your compiled WAR file and copy it into the webapps directory .
  5. Go to Terminal. Type cd Appname, and then git add ., git commit -m "Deployment", and finally git push.
  6. Your application should now fully function at

MySQL access

  1. Install the cartridges for MySQL and phpMyAdmin. This should be available through Add Cartidge at your app hub.
  2. Note your username and password to the MySQL database that OpenShift automatically generates for you. Go to, enter the authentication credentials.
  3. Inside phpMyAdmin there should be the server IP address; it looks something like 127.x.y.z:3306. x, y, and z can be single digit to three digit numbers.
  4. Quickly create a new database named whatever you want. I am going to name it test and then consequently produce a new table in there named testtable.
  5. So remember your deployed WAR application? Well, if you are using MySQL, I bet you have already included it in your application. The basic steps to establish a connection to MySQL is as such.

Java Code

Go the open IDE project that you compiled into your WAR file. Go to the pom.xml inside your project if it is a Maven project, and add the dependency:

  1. Then use the following code.

String s = "jdbc:mysql://" + host + ":" + port + "/" + name", where host is the server IP address, the port is 3306 and name is the database name, in my case, test.

try {
} catch (ClassNotFoundException e) {

try {
    Connection con = DriverManager.getConnection(s, username, password);
catch (SQLException e) {

Now if (con == null) it did not work. And if it is not null, it did.

To test, you should recompile your WAR file (after putting some way to visually test it). If you need further help, please leave a comment. It should work when you compile WAR file and redo steps 4-6 in the first section: Deployment of compiled webapp to OpenShift. Thanks!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download