将表单值发布到SQL适配器Worklight V6

时间:2015-01-15 15:17:10

标签: javascript sql ibm-mobilefirst worklight-adapters

如何将表单中的所有值发布到将表单值插入数据库的sql适配器?

  1. 我有一个带有onclick事件的表单

  2. 我有一个适配器设置,其中包含addUserInfo的过程:

    function addUserInfo(firstname, lastname, email, state, province, zippostal, phonenumber, streetnamenumber, city) {
        return WL.Server.invokeSQLStatement({
            preparedStatement : addStatement,
            parameters : [firstname, lastname, email, state, province, zippostal, phonenumber, streetnamenumber, city]
        });
    }
    
  3. 我可以获得一些建议/如何连接两者的例子吗?

1 个答案:

答案 0 :(得分:0)

以下答案提供了一个端到端的方案,用于将从HTML中获取的值插入到数据库中:https://stackoverflow.com/a/25164028/1530814


您需要从HTML获取输入并将其用作WL.client.invokeProcedure的参数:

function loadFeeds1(){
    var invocationData = {
        adapter:"car2",
        procedure:"getuser",
        parameters:[$('#carnum').val(),$('#details').val()]
    };

    WL.Server.invokeProcedure(invocationData,{
        onSuccess :loadFeedsSuccess1,
        onFailure :loadFeedsFailure1,
    });
}

<强> 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");
}

适配器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"/>
...
...

适配器实施:

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]
    });
}