0

I want to connect my IBM MobileFirst apps to my database,

I use wampserver (localhost), username = "root", password ="...", database name = "mydatabase".

In my MobileFirst project, I created a SQL adapter "myAdapter".
Inside the myAdapter.xml, this is the code:

<connectivity>
    <connectionPolicy xsi:type="sql:SQLConnectionPolicy">
        <!-- Example for using a JNDI data source, replace with actual data source name -->
        <!-- <dataSourceJNDIName>java:/data-source-jndi-name</dataSourceJNDIName> -->

        <!-- Example for using MySQL connector, do not forget to put the MySQL connector library in the project's lib folder -->
        <dataSourceDefinition>
            <driverClass>com.mysql.jdbc.Driver</driverClass>
            <url>jdbc:mysql://localhost:3306/mydatabase</url>
            <user>root</user>
            <password></password> 
        </dataSourceDefinition>
    </connectionPolicy>
</connectivity>
<!-- Replace this with appropriate procedures -->
<procedure name="insertMyTable1"/>

Below is myAdapter-impl.js file

        var insertMyTable = WL.Server.createSQLStatement( 
"IESERT INTO mytable" +
"VALUES (? , ? , ?);");

function insertMyTable1(id, name, age){
return WL.Server.invokeSQLStatement({
    preparedStatement : insertMyTable,
    parameters : [id, name, age]
});

}

//--------------------------------------

in one of my pages, I have a addData.html file, below is the code:

<html>
<script>
function insertData(){
    var id = document.getElementById("id").value;
    var name = document.getElementById("name").value;
    var age = parseInt(document.getElementById("age").value);
    WL.Client.invokeProcedure({
        adapter : "myAdapter",
        procedure : "insertMyTable1",
        parameters : [ id, name, age ]
    });
}
</script>
<body>
<form action="javascript:insertData();">
<table align="center">
<tr>
<td>Id : </td>
<td><input type="text" id="id"></td>
</tr>
<tr>
<td>Name : </td>
<td><input type="text" id="name"></td>
</tr>
<tr>
<td>Age : </td>
<td><input type="text" id="age"></td>
</tr>
<tr>
<td colspan="2" align="center">
    <button type="submit" style="width:100px;">Add</button>
</td>
</tr>
</table>
</form>
</body>
</html>

But I fail to insert the data into mydatabase->mytable, anyone know why ??

error log (in my addData.html) Uncaught ReferenceError: WL is not defined

Anson Tan
  • 1,256
  • 2
  • 13
  • 34
  • I believe you need to provide password. Are you facing any issues with this configuration? If its so, please provide necessary details like error/code, etc. However, this forum is not used for general discussion and verfication – dhineshsundar May 07 '15 at 07:06
  • My password is empty, sorry, due to there is not much tutorial or example of IBM MobileFirst, I really hope someone can guide me whether my syntax is correct, especially the jdbc:mysql://localhost:3306/mydatabase – Anson Tan May 07 '15 at 07:09
  • What problem are you facing? Are you getting an error message? – Pekka May 07 '15 at 07:10
  • Add the actual code you've implemented to call the adapter in your main.js. Add your adapter implementation in your adapter-impl.js file. Add the actual error you're getting when trying to execute your code. – Idan Adar May 07 '15 at 07:11
  • Okay, I will do it now. 5 mins – Anson Tan May 07 '15 at 07:12
  • [Getting Started](https://developer.ibm.com/mobilefirstplatform/documentation/getting-started/) and SQL Adapter [guide](https://developer.ibm.com/mobilefirstplatform/documentation/getting-started-7-0/server-side-development/js-sql-adapter/) follow this. If you are facing any error please provide, because your question is not clear enough. – dhineshsundar May 07 '15 at 07:13
  • @AnsonTan, please provide the error log that you are getting on server side. – dhineshsundar May 07 '15 at 07:36
  • added, but the image is not clear, download the image and view is better. – Anson Tan May 07 '15 at 07:37
  • @AnsonTan given error is from client side which is caused by an emulator read [this](http://stackoverflow.com/questions/22348801/phonegap-eclipse-issue-eglcodeccommon-glutilsparamsize-unknow-param-errors) but i was asking about server side error or exceptions. – dhineshsundar May 07 '15 at 07:38
  • Sorry, I am really new to IBM MobileFirst, where to get the error log in server side ?? – Anson Tan May 07 '15 at 07:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77161/discussion-between-dhineshsundar-and-anson-tan). – dhineshsundar May 07 '15 at 07:43
  • Open the mfp console in Chrome, preview your app, open the chrome devtools, refresh the page. Paste the error you'll see there. The errors you've pasted in the question got nothing to do with your actual error... – Idan Adar May 07 '15 at 07:48
  • thanks @IdanAdar, I get the error message. – Anson Tan May 07 '15 at 08:02
  • What is this html file? Is this a multi-page application? – Idan Adar May 07 '15 at 08:22
  • yes, ahref from my index.html. – Anson Tan May 07 '15 at 08:27

1 Answers1

1

This question is completely unrelated to your SQL adapter.

The problem here is that you have used a href to navigate to another HTML file. By doing so you have exited the scope, or context, of the MFP framework, which is why you are unable to use MFP API methods such as WL.Client.invokeProcedure.

A MFP Hybrid application is a Single Page Application. In the app's index.html there are references to the MFP JavaScript framework in order to load it... Without these, things will break.

In order to use multiple "pages" in your application, see the following tutorial:

You must never actually navigate away from the context of the framework, so operations such as a href are not allowed.

If you want to separate your "pages" to separate HTML files, you can see this example project using jQuery Mobile. Other UI frameworks such as Dojo also provide their own implementation for multi-page support which you could use in your MFP application.

Related questions: https://stackoverflow.com/search?q=%5Bworklight%5D+multipage+is%3Aquestion

Community
  • 1
  • 1
Idan Adar
  • 44,156
  • 13
  • 50
  • 89