Skip to Content
avatar image
Former Member

Insert values from array - XSJS

for(var j=0; j<output2.length; j++)

{

var pstmt4 = conn.prepareStatement("INSERT INTO STUDENT01.HISTORY VALUES(?,?,?,?)");

pstmt4.setString(1,date);

pstmt4.setString(2,time);

pstmt4.setString(3,output2[j]); // at this line

pstmt4.setString(4,output);

pstmt4.executeQuery();

}

The above code shows an error while inserting. but works without the loop. Am i missing something?

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

3 Answers

  • Apr 28, 2014 at 12:32 PM

    Might I also suggest that you use a batch operation. This will send all the inserts into the database in one bundle (use executeBatch instead). Here is a similar example that uses the Batch concept:

    var st = conn.prepareStatement('INSERT INTO #local_roles_table_1 VALUES(?)');

    st.setBatchSize(userJson.roles.length);

    for ( var x = 0; x < userJson.roles.length; x++) {

    st.setString(1, userJson.roles[x].role_name);

    st.addBatch();

    }

    st.executeBatch();

    st.close();

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Thomas,

      Is the batch mechanism also supported with the new XSJS DB interface - SPS9? Or do we have to revert to the older prepare statement.

      Nehal

      Former Member

  • Apr 27, 2014 at 07:11 PM

    Hi Rajkumar,

    What is the error message?

    Give a try moving the prepareStatement outside the for, after it's already prepared I belive it will work fine just binding and executing.

    Regards, Fernando Da Rós

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 28, 2015 at 08:01 AM

    For some reason, it worked with a while loop.

    Add comment
    10|10000 characters needed characters exceeded