October 10, 2014 | Written by: Milind Joshi
Share this post:
While creating a web application in IBM Bluemix using J2EE and MySQL, my colleagues and I learned a few things about database connectivity that I’d like to share on this platform to help to others.
Creating the Java application with MySQL in IBM Bluemix
In order to create a Java WebApp, first create an application in the Java Web Start boilerplate from the Bluemix catalogue. Boilerplate is a common code that can be used in any application with little or no modification; it is not part of the business logic, but is interwoven with it in order to keep data structures updated or handle secondary aspects of the application. The procedure to create the boilerplate can be found on this link.
Once the boilerplate is ready, we can move ahead to create the service. There are two ways to add the service to an existing application:
- Bind the existing service to the application. This option helps to share the single service instance across multiple applications and to enable data sharing. This helps in effective service utilization.
- Add a new service and bind it to the application. Use this option if you want the service instance to be exclusive to your application.
For more details on handling services in Bluemix, you can check this link.
When you bind or add a MySQL service instance to an application, the application restarts automatically and it will subsequently begin using this new service. By that time, you will have the sample application created on Bluemix dashboard with the MySQL service instance bound to it. When you click on Show Credentials in the MySQL service instance, you will get the instantiating credentials.
The following example below may explain it better:
"hostname": "188.8.131.52", #<hostname/IP of the server hosting mySQL service>
"host": "184.108.40.206", #
"port": 3307, #
"user": "uM7MDCbhYIKbw", #
"username": "uM7MDCbhYIKbw", #
"password": "pNyQdGc25lkLi", #
From the values, we are interested only in those that are present under the “credentials” section. We will need to add those to our web application. The “name” specifies the name of the database. It should be noted that you can’t connect to the MySQL service instance directly through the MySQL client. The configuration parameters will be active when the application is deployed in Bluemix environment.
Once you bind the service with the WebApp, you can download the starter kit from the Bluemix dashboard. Note that the starter kit download option is only available when you create the WebApp from the Bluemix dashboard, and not when the app is created with the cf client.
Download the starter kit and import it to your Eclipse integrated development environment (IDE) to continue writing the business logic. Verify that the MySQL service is bound with the application by checking the “services” section of manifest.yml file in the starter kit.
Using the MySQL configuration parameters in the web application
WebApps always have the requirement to create and manipulate the tables. In Bluemix, you have to create the database tables with Java code. We have used the web.xml file present in the WebApp as the store for database initialization parameters. These database parameters are stored as context parameters so they can be read from all servlets or JavaServer Pages (JSP) within the application. The initialization can be done in a web.xml element:
Once the parameters are set in web.xml, we can create the database connection in a listener class. You have to declare this listener class in web.xml as below:
In the listener class, we created DBConnection as a context attribute with the help of the following code (the DBConnection is shared across the web application):
ServletContext ctx = servletContextEvent.getServletContext();
//initialize DB Connection
String dbURL = ctx.getInitParameter("dbURL");
String user = ctx.getInitParameter("dbUser");
String pwd = ctx.getInitParameter("dbPassword");
DBConnectionManager connectionManager = new DBConnectionManager(dbURL, user, pwd);
So when you need to create the database connection for building or accessing the tables, use the following code in your servlets or JSPs:
Connection con = (Connection) getServletContext().getAttribute("DBConnection");
You can use the PreparedStatement interface to query the databases. Once the application is ready, you can push it to the Bluemix environment in the form of .war as:
cf push -p .war
You can troubleshoot the application after successful deployment by taking the following steps in on the Bluemix dashboard:
- Click on your application
- Select Files and Logs
- Select the log directory and you will find the stderr.log and stdout.log for getting the error details.
See this link for more assistance in getting started with MySQL service.
Good luck in your ventures with IBM Bluemix and MySQL! Please leave a comment below to let me know if this was helpful and to join in the conversation.