Monday 8 September 2014

Bluemix - Liberty for Java with SQLDB

I wanted to work with a database on the Bluemix, preferably a NoSQL.  After getting to know that SQLDB, which is powered by DB2 is available on Bluemix, my attention turned towards it.

The steps used to create an application using Liberty for Java with SQLDB is given below.

1. Login to Bluemix (Register yourself as a new user, registration process is simple)

2. Select Liberty for Java (available in the Catalog Tab) as the Runtime and provide a name and URL for the application.  I have named it SQLJava and will refer hereafter with this name.

3. Select SQL for Database in the Data Management section and create an instance.  Associate this instance with the Liberty for Java application.

4. Goto the Dashboard and expand SQLJava.  In the top left corner, you will find link to View Guide.  Click on this link.

5. Follow the steps given in the Guide.  The Guide asks you to download Cloud Foundry so as to make use of the cf command.

6. Download code that has been generated for this application.  The link to download is available in the guide.  The downloaded code will be in the form of a zip file.  Expand the zip file

7. I am assuming here that Eclipse has been installed (Eclipse Luna version can be downloaded and installed and WebSphere Liberty server is installed on Eclipse).

8. Import the code that has been downloaded and extracted into an Eclipse workspace.

9. Click on the src folder a Create a new Servlet.

10. Use the annotation @WebServlet("/MyFirstServlet") above the servlet name.  This name will be used to connect to the Servlet from an Explorer.

11.  Within the class, declare a DataSource and provide a @Resource annotation to it.  This resource has to point to the SQL database we created.  Ensure that the value of the database name given in lookup property is the same as the SQL database that was created.
    @Resource(lookup = "jdbc/chitradb")
    private DataSource myDataSource;

12.  This Data Source can then be used within the doGet() method to create a Connection object.
Connection connection = myDataSource.getConnection();

13. Complete the code to perform required tasks within the Servlet.

14. For the compilation of the Servlet and the build and the jar javax.servlet_*.jar within the dep-jar folder.  Add this jar to the classpath and also to the build.xml file, under the classpathDir.

15. Delete the war file webStarterApp.war, a new war file with our changes has to be created.

16. Right click on the build,xml file and Run As Ant Build.  Ensure that there are no errors during the build process.

17. Refresh the project to find the new war created.

18.  Now, login to the Bluemix using the cf command
cf login -a <bluemix url>
Provide the username and password.

19.  I went to the directory in which the war file is present and directly gave
cf push
The new application was uploaded.  Ensure that there are no errors during this process.

The application is up and running and can be accessed, using the <Application URL>/<Servlet Name>

Links Referred:

http://ryanjbaxter.com/2014/03/24/pushing-a-java-app-to-bluemix/
https://developer.ibm.com/bluemix/2014/02/07/java-db2-10-minutes/


No comments:

Post a Comment