How can I insert an XML document in PostgreSQL in Java? - 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>'));

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.

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.

Related

Java Oracle Blob to Blob comparisons failling using PreparedStatement statement

I try to compare string inside blob saved as byte array to string in SQL query
from what i understand i need to convert the value to Blob and then compare Blob and Blob
but im getting error
String s = "SELECT * FROM TEST VAL like ?";
Blob blob = conn.createBlob();
blob.setBytes(1, ((String)"yes").getBytes("UTF-8"));
PreparedStatement p = conn.prepareStatement(s);
p.setBlob(1,blob);
p.executeUpdate();
but getting exception error
java.sql.SQLException: ORA-06553: PLS-306: wrong number or types of arguments in call to 'CAST_TO_VARCHAR2'
The first mistake is that the statement is a SELECT statement :
String s = "SELECT * FROM TEST VAL like ?";
but you are trying to call p.executeUpdate();
According to the documentation of: PreparedStatement#ecexuteUpdate()
Executes the SQL statement in this PreparedStatement object, which
must be an SQL Data Manipulation Language (DML) statement, such as
INSERT, UPDATE or DELETE; or an SQL statement that returns nothing,
such as a DDL statement.
that is - this method is not intended for execution of SELECT statement. Only INSERT, DELETE, UPDATE or an SQL that returns nothins - SELECT returs a resultset.
Another mistake is that according to the documentation of LIKE operator their arguments can be only of the following datatypes:
char1 LIKE char2 [ ESCAPE asc_chars ]
All of the character expressions
(char1, char2, and esc_char) can be of any of the data types CHAR,
VARCHAR2, NCHAR, or NVARCHAR2. If they differ, then Oracle converts
all of them to the data type of char1.
As you see, BLOB datatype are not alloved here as parameter. BLOB datatype was used as a parameter of LIKE operator --> p.setBlob(1,blob);, therefore the error was thrown: ORA-06553: PLS-306: wrong number or types of arguments in call to 'CAST_TO_VARCHAR2' because Oracle tried to cast BLOB to VARCHAR2 datatype, but this cast is not allowed.

Java MySQL: Insert special charactar into database

I want to insert a special character like ✪ into a database.
When I do it like this in the Java code:
String message = "✪";
preparedStatement = connection.prepareStatement("INSERT INTO `messages` (`message`) VALUES (?)");
preparedStatement.setString(1, message);
preparedStatement.executeUpdate();
It just inserts a ? instead of ✪. But when I execute the SQL command on phpMyAdmin it works fine and ✪ is inserted.
The column message in the database is of the type varchar(2048) and collation utf8_general_ci.
And the text file encoding of the java project is UTF-8 as well.
I have added the Parameter ?characterEncoding=UTF-8 to the JDBC URL, as #Mathisca pointed out.

JDBC4 insert date "0001-01-01"

I want to insert "0001-01-01" as a value into a date field by using Java PreparedStatement.
But it throws exception when I tried this:
String sql = "insert into mytable values(?)"
ps = conn.prepareStatement(sql);
ps.setDate(1, java.sql.Date.valueOf("0001-01-01"));
ps.executeUpdate(); // throws exceptions here.
The error is :
The supplied value is not a valid instance of data type datetime. Check the source data for invalid values. An example of an invalid value is data of numeric type with scale greater than precision.
If I don't use PreparedStatement, I can insert "0001-01-01". However,
prepare statement seems not allow me to insert this value.
It will work if I inserted "1969-01-01" instead of "0001-01-01".
Any ideas?
Updates:
Here are more info that might be needed.
we use sql server 2012.
we have to use "0001-01-01" because these values were already there. I am changing some very very old codes to use prepare statement. So I have to insert the same values in the same functionality.
Updates 2:
We are using "date" datatype, not "datetime" datatype.
Based on this https://msdn.microsoft.com/en-us/library/bb630352.aspx, "0001-01-01" is not out of range for "date" field.
In addition, I am able to insert "0001-01-01" to the date field without using prepare statement. i.e.
String sql = "insert into mytable values('0001-01-01')"
java.sql.Statement statement = conn.createStatement();
statement.executeUpdate(sql);
So it is not sql server's problem or db field's problem.
Try using the different suitable JDBC driver.

Clob column java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column

insertSQL = "insert into TELBP_INPUT_LOG (SERIAL_NO, INPUT_XML) values (?, ?)";
statement = connection.prepareStatement(insertSQL);
statement.setString(1, serialNo);
statement.setString(2, inXml);
//statement.setString(2, "test");
insertCount = statement.executeUpdate();
when the program run to executeUpdate(), error
java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column
is thrown, but if I copy the value of serialNO and inXml and run in SQL developer, no error prompted, what is the reason?
oracle version:Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
column:
SERIAL_NO VARCHAR2(22)
INPUT_XML CLOB
Websphere:Websphere 5.1
jdbc: both ojdbc14 and ojdbc6 is tried, both has same error
You cannot write a String into a Clob column.
Instead of
statement.setString(2, inXml);
use
statement.setClob(2, xmlClob);
You first need to create xmlClob:
Clob xmlClob = connection.createClob();
Writer clobWriter = myClob.setCharacterStream(1);
clobWriter.write(inXml);
for clob field you can make use of .setClob(..)
setString():Sets the designated parameter to the given Java String value. The driver converts this to an SQL VARCHAR or LONGVARCHAR value (depending on the argument's size relative to the driver's limits on VARCHAR values) when it sends it to the database.
API DOC
CLOB API DOC
Java (the underlying driver) treats CLOB as a character stream. When ever you are setting String, the underlying driver implementation will automatically do the relevant conversion (String to Varchar etc.,). As CLOB is a special type, it is the responsibility of the programmer to do the necessary steps. Follow the link to now how to insert clob using java:
http://docs.oracle.com/javase/tutorial/jdbc/basics/blob.html

Insert data to sql using java issue

I'm facing an issue with insertion to SQL database from java code.
I'm using INSERT sql query using the java code to enter the data from XML file to SQL database.
You may suppose column named "Description".
Imagine there is a record in XML which contains apostrophe ('). The program crashes due to the error caused by the apostrophe which is included in the data.
I know that manually we can add another apostrophe and make it work, but imagine data of 10.000 records, how can we handle this issue?
Don't do this (string concatenation):
String sql = "insert into MyTable (description) values ('" + value + "')";
Statement st = connection.createStatement();
st.executeUpdate(sql);
Do do this (prepared statement):
PreparedStatement ps = connection.prepareStatement(
"insert into MyTable (description) values (?)"
);
ps.setString(1, value);
pt.executeUpdate();
The value will get correctly escaped for you. Not only does this protect against mishaps like the one you mentioned, it also helps defend you from SQL injection attacks.
Humorous illustration:
Source
You have two options, you should use PreparedStatement and bind your parameter(s). Or, if you really, really, want - you could use StringEscapeUtils.escapeSql(str).

Categories

Resources