Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2010
    Posts
    3

    Unanswered: Trouble with DB2 XMLPARSE and JDBC using PreparedStatement

    Hello everyone,

    I want to INSERT XML-data into a DB2 Table. The following exemplary statement works fine:

    INSERT INTO test VALUES ( '1' , 'something', XMLPARSE(DOCUMENT '<test></test>')) ;

    I'd like to use PreparedStatements, for escaping reasons etc., instead but it is giving me some trouble. I've tried the following variants:

    Variant 1:
    Code:
    PreparedStatement ps = 
              connection.prepareStatement(INSERT INTO test VALUES ( '?', '?', XMLPARSE(DOCUMENT'?')) ;
    ps.setString(1,"1");
    ps.setString(2,"something");
    ps.setString(3,"<test></test>");
    -> SQLCODE: -16132, SQLSTATE: 2200M, SQLERRMC: null
    Variant 2:
    Code:
    PreparedStatement ps = 
              connection.prepareStatement(INSERT INTO test VALUES ( '?', '?', '?'));
    ps.setString(1,"1");
    ps.setString(2,"something");
    ps.setString(3,"XMLPARSE(DOCUMENT'<test></test>')");
    -> SQLCODE: -16132, SQLSTATE: 2200M, SQLERRMC: null
    Can anybody give me some pointers as to what I'm doing wrong? I'm somewhat in doubt that it is really a XML-structure problem.

    Greetings,
    arkanoid

    EDIT1: added closing bracket to PreparedStatement 2
    EDIT2: added code tags
    Last edited by arkanoid; 12-09-10 at 11:40.

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Parameter markers should not be quoted.

  3. #3
    Join Date
    Dec 2010
    Posts
    3
    Thanks for your reply.
    Tried that too, like this right?

    Code:
    String param = "XMLPARSE(DOCUMENT '<a></a>')";
    PreparedStatement ps = 
              connection.prepareStatement("INSERT INTO test VALUES ( ?, ?, ?)");
    ps.setString(9,param);
    -> -16132, SQLSTATE: 2200M, SQLERRMC: null
    Code:
    String param = "<test></test>"; || param = "'<test></test>'";
    PreparedStatement ps = 
              connection.prepareStatement("INSERT INTO test VALUES ( ?, ?, XMLPARSE(DOCUMENT ?))");
    ps.setString(9,param);
    -> SQLCODE: -418, SQLSTATE: 42610, SQLERRMC: null
    Edit1 : added code tags
    Last edited by arkanoid; 12-09-10 at 12:08.

  4. #4
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Quote Originally Posted by arkanoid View Post
    Code:
    String param = "<test></test>"; || param = "'<test></test>'";
    PreparedStatement ps = 
              connection.prepareStatement("INSERT INTO test VALUES ( ?, ?, XMLPARSE(DOCUMENT ?))");
    ps.setString(9,param);
    -> SQLCODE: -418, SQLSTATE: 42610, SQLERRMC: null
    Try an explicit cast:

    PreparedStatement ps =
    connection.prepareStatement("INSERT INTO test VALUES ( ?, ?, XMLPARSE(DOCUMENT cast(? as varchar(256))))");

  5. #5
    Join Date
    Dec 2010
    Posts
    3
    Quote Originally Posted by n_i View Post
    Try an explicit cast:

    PreparedStatement ps =
    connection.prepareStatement("INSERT INTO test VALUES ( ?, ?, XMLPARSE(DOCUMENT cast(? as varchar(256))))");
    That did the trick, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •