Re: Mismatch in Statement and PreparedStatement execution in Oracle
DB.
Alex Kizub wrote:
PerparedStatement ps=con.prepareStatement("select text from table1
You should copy and paste your code into the post.
where text='A'"); // A without space
brings the same 'A ' // A+space
ps=con.prepareStatement("select text from table1 where text=?"); //
parameter
ps.setString(1,"A "); // A+space
brings, of course, the same 'A ' // A+space
ps.setString(1,"A"); // A without space
brings nothing
Wrong 'setX()' method. According to the docs at
<http://java.sun.com/javase/6/docs/api/java/sql/PreparedStatement.html#setString(int,%20java.lang.String)>
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.
So it's not a bug in 'setString()' ut your attempt to use it contrary to the
documented purpose.
I don't see a method specific to SQL CHAR types.
This tells me that providing a value to 'setString()' of the correct length
for the CHAR column is the only portable approach.
Any suggestions except make text match length of the database field
which makes application schema dependent?
There is no such animal, other than non-portable approaches.
I reached this conclusion by a combination of reading the Javadocs and
googling around. I presume you tried both of those things, too. I may well
have missed something obvious there; it's happened to me before.
You can, however, use
<http://java.sun.com/javase/6/docs/api/java/sql/PreparedStatement.html#getMetaData()>
to dynamically retrieve the length for padding purposes.
--
Lew