Hey I am using IBM Worklight V6.2.I want to insert values into database
My Html Code is
<h1>Please Enter The Car Details</h1>
<form >
Car No:<input type="number" id="carnum" placeholder="Please enter your no" ><br><br>
Details:<input type="text" id= "details" placeholder="Please enter car details" > <br><br>
<input type="submit" value="Register" onclick="loadFeeds1()">
</form>
My procedure is:
var users = WL.Server.createSQLStatement("insert into car(carno,details) values (?,?)");
function getusers(carno,details) {
return WL.Server.invokeSQLStatement({
preparedStatement : users,
parameters : [carno,details]
});
}
My js file is this
function loadFeeds1(){
var invocationData = {
adapter:"car2",
procedure:"getuser",
parameters:["carno","details"]
};
WL.Server.invokeProcedure(invocationData,{
onSuccess :loadFeedsSuccess1,
onFailure :loadFeedsFailure1,
});
}
function loadFeedsSuccess1() {
WL.Logger.debug("inserted");
}
function loadFeedsFailure1() {
WL.Logger.debug("failed");
}
I am able to invoke procedure from adapter..but not able to see when i insert values in browser.not it is showing anything in console..Kindly suggest...
You can't simply place the ID
s of your input
s as the WL.client.invokeProcedure
's parameters... You need to pass their value.
For example:
function loadFeeds1(){
var invocationData = {
adapter:"car2",
procedure:"getuser",
parameters:[$('#carnum').val(),$('#details').val()]
};
WL.Server.invokeProcedure(invocationData,{
onSuccess :loadFeedsSuccess1,
onFailure :loadFeedsFailure1,
});
}
This is an end-to-end scenario, where I take 2 values from the HTML and insert them into the database. To re-create, you can you use the WorklightTraining.sql scheme provided in the Adapters sample project. You can see it works because after the 'success', if you will refresh the database - you'll see the new record.
HTML:
<h1>Test Insert Into Database</h1>
<input type="text" id="value1" placeholder="value1"/><br/>
<input type="text" id="value2" placeholder="value2"/><br/>
<input type="button" value="Insert values to database" onclick="insertValuesToDB();"/>
main.js:
function insertValuesToDB() {
var invocationData = {
adapter: 'insertValuesAdapter',
procedure: 'insertValuesProcedure',
parameters: [$('#value1').val(), $('#value2').val()]
};
WL.Client.invokeProcedure(invocationData, {onSuccess: insertSuccess, onFailure: insertFailure});
}
function insertSuccess() {
alert("success");
}
function insertFailure() {
alert("failure");
}
Adapter XML:
...
...
<connectivity>
<connectionPolicy xsi:type="sql:SQLConnectionPolicy">
<dataSourceDefinition>
<driverClass>com.mysql.jdbc.Driver</driverClass>
<url>jdbc:mysql://localhost:3306/worklight_training</url>
<user>Worklight</user>
<password>Worklight</password>
</dataSourceDefinition>
</connectionPolicy>
<loadConstraints maxConcurrentConnectionsPerNode="5" />
</connectivity>
<procedure name="insertValuesProcedure"/>
...
...
Adapter implementation:
var insertValuesProcedureStatement = WL.Server.createSQLStatement("INSERT INTO users(userId, firstName, lastName, password) VALUES (?,?, 'someLastName', 'somePassword')");
function insertValuesProcedure(value1,value2) {
return WL.Server.invokeSQLStatement({
preparedStatement : insertValuesProcedureStatement,
parameters : [value1,value2]
});
}