简体   繁体   中英

How can I insert an XML document in PostgreSQL in Java?

I have a table in Postgresql

DROP TABLE xml_docs;
CREATE TABLE xml_docs(
id serial PRIMARY KEY,
cad_number character(50),
gkuzu_name character(50),
gkuzu xml,
rreq_name character(50),
rreq xml
)

I use JDBC for data base connection. And i want to insert whole xml document in table.
How can i make this?

UPDATE

Okey. i try

  String sql = "INSERT INTO xml_docs(cad_number,gkuzu_name,gkuzu,rreq_name,rreq) VALUES(?,?,?,?,?)";
  PreparedStatement stmt = ce.prepareStatement(sql);
  stmt.setString(1, "11:33:5464563");
  stmt.setString(2, xml_gkuzu.getName());
  stmt.setString(3, xml_gkuzu.toString());
  stmt.setString(4, xml_rreq.getName());
  stmt.setString(5, xml_rreq.toString());
  stmt.executeQuery();
ce.close();
  se.close();

and get exeption

Exception in thread "main" org.postgresql.util.PSQLException: ERROR: column "gkuzu" is of type xml but expression is of type character varying
Подсказка: You will need to rewrite or cast the expression.

Whats wrong?

UPDATE 2

When i try do this

 String sql1 = "INSERT INTO xml_docs(cad_number,gkuzu_name,gkuzu,rreq_name,rreq) VALUES(11335464563,"+xml_gkuzu.getName()+",XMLPARSE("+xml_gkuzu.toString()+"),"+xml_rreq.getName()+",XMLPARSE("+xml_rreq.toString()+"))";

i get exeption

Exception in thread "main" org.postgresql.util.PSQLException: ERROR: syntax error at or near "bf48e000b0"

I'm not sure, but try this:

First convert your XML to a Java String. Then create an insert statement und use the XMLPARSE method of PostgreSQL to convert your value to the xml type of PostgreSQL:

INSERT INTO xml_docs(id, gkuzu) VALUES (1, XMLPARSE('<foo><bar>Hello</bar></foo>'));

See: http://wiki.postgresql.org/wiki/XML_Support

UPDATE:

Java code example:

String sql = "INSERT INTO xml_docs(id, gkuzu) VALUES (?, XMLPARSE(?))";
[...]
stmt.setString(2, "<foo>Hello World!</foo>");

This should create this statement:

INSERT INTO xml_docs(id, gkuzu) VALUES (1, XMLPARSE('<foo>Hello World!</foo>'));

An update to the accepted answer if you do not have Postgres built with libxml support:

Java code example:

String sql = "INSERT INTO xml_docs(id, gkuzu) VALUES (?, XML(?))";
[...]
stmt.setString(2, "<foo>Hello World!</foo>");

This should create this statement:

INSERT INTO xml_docs(id, gkuzu) VALUES (1, XML('<foo>Hello World!</foo>'));

Thus for version 9.0 and greater you may want to switch XMLPARSE ==> XML . Otherwise you will need special support for XMLPARSE

From Postgres Documentation :

The function-like expressions xmlparse and xmlserialize for converting to and from type xml are not repeated here. Use of most of these functions requires the installation to have been built with configure --with-libxml.

Instead of rewriting the insert statement using PostgreSQL-proprietary syntax, you could use JDBC 4 SQLXML :

String xml = xml_gkuzu.toString();

SQLXML sqlxml = connection.createSQLXML();
sqlxml.setString(xml);
stmt.setSQLXML(3, sqlxml);

Though postgres has native XML Data type , from java end, You can handle with Plain strings.
You can convert your xml document to String and insert, It should work.

UPDATE:

After looking at your error, You need to pass an additional variable to the server through driver URL.

jdbc:postgresql://localhost/test?stringtype=unspecified
or
jdbc:postgresql://localhost/test?user=user&password=pass&stringtype=unspecified

The extra param stringtype=unspecified will remove the type check for the input strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM