Three days ago, my friends at Packt Publishing send me a free ebook called PhoneGap Beginner's Guide. And while I knew about PhoneGap I had never really use it...so of course, my first thought was...How can I make this work with SAP HANA?
Two days ago, I started to read the book and make the PhoneGap installation...which was a total pain and didn't even work...so I simply put it aside for next year...
Yesterday...my developer spirit could more than me...and since 9 am to 11:30 pm I embarked myself on a crusade to have PhoneGap up and running and of course...to make it work with SAP HANA...here's my story...so you don't have to break your head :wink:
With all that...I was almost ready to go...as I was missing the Cordova-2.2.0.jar file...for that...I did the following...
settings.setNavDump(true);
Now...I was really ready to go...so I create a folder called Projects inside Corbova...and again on CMD I did the following...
That created a Blag_Test folder with all the related files from the Cordova project. Then I simply upload it to Eclipse, and made a couple of test to see if everything was working as expected...it did...so the new odyssey for SAP HANA was going to start...
At first...my initial thought was...this should be easy...I have already do it on PowerBuilder, so basically I need to import the ngdbc.jar into my Eclipse Project and that's it...wrong! Didn't work...and after several tries and fails...I finally see the light...I delete the project from Eclipse...copy the ngdbc.jar inside my libs folder of BlagTest...re-imported on Eclipse...and magically...I got a connection to SAP HANA...
Now...make that connection work was another nightmare...for this blog I needed to undust my almost forgotten knowledge of Java and JavaScript...and also...learn new things like PhoneGap and JQueryMobile...
But...I'm going to try to keep the long story short, so you don't get bored...
MyClass.java |
---|
package com.BlagTest;
import java.sql.*; import java.util.ArrayList;
public class MyClass { public ArrayList<String> getData(String p_carrid){ Connection connection = null; ArrayList<String> carrid = new ArrayList<String>(); String data = ""; try{ Class.forName("com.sap.db.jdbc.Driver"); try{ connection = DriverManager.getConnection("jdbc:sap://XX.XX.XXX.XXX:30115","SYSTEM","manager"); } catch (SQLException e){ } } catch (ClassNotFoundException e){ } if(connection != null){ try{ String sqlstring = "select CONNID, FLDATE, PRICE from SFLIGHT.SFLIGHT where carrid = '" + p_carrid + "'"; Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(sqlstring); while(rs.next()){ data = rs.getString("CONNID") + "/" + rs.getString("FLDATE") + "/" + rs.getString("PRICE"); carrid.add(data); } } catch(SQLException e){ } } return carrid; } } |
In this file, what I'm doing is establishing an JDBC connection to my SAP HANA Server hosted on TK ucloud biz. The I'm selecting the CONNID, FLDATE and PRICE from the SFLIGHT table where the CARRID is going to be a parameters send from the application. As I didn't want to pass a multidimensional array, or an array of arrays, or anything like that...I simply concatenate the values using a "/" to split them later.
BlagTest.java |
---|
package com.BlagTest;
import android.app.Activity; import android.os.Bundle; import org.apache.cordova.*;
public class BlagTest extends DroidGap {
private MyClass mc;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.init(); mc = new MyClass(); super.appView.getSettings().setJavaScriptEnabled(true); super.appView.addJavascriptInterface(mc, "MyCls"); super.loadUrl("file:///android_asset/www/index.html"); } } |
Here, basically we saying that we want to be able to send data from Java to JavaScript by using the setJavaScriptEnabled(true) and then adding the addJavaScriptInterface(mc, "MyCls") we're telling how our class is going to be called...when we call them from JavaScript.
index.html |
---|
<html> <head> <title>SAP HANA from PhoneGap</title> <meta name="viewport" content="width=device-width, initialscale=1.0"></meta> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css"/> <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script> <script> function getData(){ var carridField = document.getElementById('carrid'); var getCarrid = carridField.value; var myArrayList = window.MyCls.getData(getCarrid); carridField.value = ""; $("#content").append("<ul id='list' data-role='listview' data-inset='true'</ul>"); $("#content").trigger("create"); for(var i = 0; i < myArrayList.size(); i++){ var array = "" + myArrayList.get(i); array = array.split('/'); var _connid = array[0], _fldate = array[1], _price = array[2]; var list = "<li><p>CONNID: " + _connid + "</p><p>FLDATE: " + _fldate + "</p><p>PRICE: " + _price + "</p></li>"; $("#list").append(list); } $("#list").listview("refresh"); } </script> </head> <body>
<div data-role="page"> <div data-role="content" id="content"> <div align="center"><h1>SAP HANA from PhoneGap</h1></div> Carrid: <input type="text" id="carrid" size="2"/> <button id="submitCarrid" onClick="getData()">Submit</button> </div> </div> </body> </html> |
What I'm doing here...is as following...
After that, we can simply go to Project --> Clean to rebuild our project and then right click on our Project Folder and choose Run As --> Android Application.
It took so long time...but the rewards can't be greater...hope you like this :smile:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
13 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
5 |