28.01.2013 Views

SAP HANA Developer Guide - Get a Free Blog

SAP HANA Developer Guide - Get a Free Blog

SAP HANA Developer Guide - Get a Free Blog

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

After logging in with your <strong>SAP</strong> <strong>HANA</strong> user name and password, you should get the following:<br />

4.5.7 Retrieve Data<br />

To make things more interesting, let's make a call to the database via the JavaScript and then display the results.<br />

You will be using the SQL statement:<br />

select * from DUMMY<br />

This is test SQL to check connectivity, and returns one row with one field called DUMMY, whose value is X.<br />

1. In MyFirstSourceFile.xsjs, delete or comment out all your existing code.<br />

2. Add the following code:<br />

$.response.contentType = "text/html";<br />

var output = "Hello, World !";<br />

var conn = $.db.getConnection();<br />

var pstmt = conn.prepareStatement( "select * from DUMMY" );<br />

var rs = pstmt.executeQuery();<br />

if (!rs.next()) {<br />

$.response.setBody( "Failed to retrieve data" );<br />

$.response.status = $.net.http.INTERNAL_SERVER_ERROR;<br />

} else {<br />

output = output + "This is the response from my SQL: " +<br />

rs.getString(1);<br />

}<br />

rs.close();<br />

pstmt.close();<br />

conn.close();<br />

$.response.setBody(output);<br />

3. Save the file.<br />

4. Commit the file by right-clicking the file and selecting Team Commit .<br />

5. Activate the file by right-clicking the file and selecting Team Activate .<br />

In your browser, refresh the page. You should get the following:<br />

72<br />

P U B L I C<br />

© 2012 <strong>SAP</strong> AG. All rights reserved.<br />

<strong>SAP</strong> <strong>HANA</strong> <strong>Developer</strong> <strong>Guide</strong><br />

Setting Up Your Application

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!