0
votes

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

1
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 verficationdhineshsundar
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 <url>jdbc:mysql://localhost:3306/mydatabase</url>Anson Tan
What problem are you facing? Are you getting an error message?Pekka
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
Okay, I will do it now. 5 minsAnson Tan

1 Answers

1
votes

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