If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Trouble with DB2 XMLPARSE and JDBC using PreparedStatement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-09-10, 08:56
arkanoid arkanoid is offline
Registered User
 
Join Date: Dec 2010
Posts: 3
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 10:40.
Reply With Quote
  #2 (permalink)  
Old 12-09-10, 10:13
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Parameter markers should not be quoted.
Reply With Quote
  #3 (permalink)  
Old 12-09-10, 10:34
arkanoid arkanoid is offline
Registered User
 
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 11:08.
Reply With Quote
  #4 (permalink)  
Old 12-09-10, 11:16
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
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))))");
Reply With Quote
  #5 (permalink)  
Old 12-09-10, 11:26
arkanoid arkanoid is offline
Registered User
 
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!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On