views:

1254

answers:

4

In the following SQL query using the PreparedStatement class:

String query_descrip = "insert into timitemdescription (itemkey, languageid, longdesc, shortdesc) values (?, 1033, ?,?)";
PreparedStatement pstmt2 = con.prepareStatement(query_descrip); 
pstmt2.setInt(1, rs4);
pstmt2.setString(2, itemdescription);
pstmt2.setString(3, itemdescription.substring(0,39));
pstmt2.executeUpdate();

I sometimes get apostrophes and single and double quotes in my item descriptions. for example, my late issue with one item is a "Planar 22" monitor". Of course, the string was misinterpreted and thought the description value was just "Planar 22". What is the best way to handle special characters in a string?

I've read that some people are using regex, however these seemed to be specific to a case by case basis. Another way I'm working on is reading the string array character by character. I was hoping there was a more efficient and less resource-intensive way of doing this.

UPDDATE AFter some more extensive testing, it turns out there were more problems occuring in my code. it was also a URL Encoding problem. When the html form was being populated by the jsp code,it would try to move the description field to an online form, it truncates it there on the form rather than on the query. jTDS also corrected the problem receiving the special characters. Because jTDS is a jar, it also helped avoid rebooting the machine. I will award the jTDS thread the bounty since that was what I partially used.

thanks in advance

+15  A: 

Since you are using PreparedStatement, you don't have to do anything at all, it will be handled for you by the JDBC driver. The only thing you have to look out for is non-ASCII characters, specifically you have to make sure the DB tables use an encoding for textual columns that can handle all characters you're going to use. But that's an SQL issue, not a Java issue.

Michael Borgwardt
when I pass the itemdescription into the prepared statement, strings with double quotes are not being escaped and interpreted as the value terminator. Do you know any workarounds for this?
phill
What SQL server and what driver are you using? If it's really not properly escaping strings, it is seriously flawed (possibly in other ways too) and I would consider moving to a different one.
Michael Borgwardt
i'm using MS SQL Server 2000 Driver for JDBC and SQL Server 2005
phill
Is there a more recent version of the JDBC driver? Not escaping quotes would be an absolute show-stopper bug making the driver completely worthless. I can hardly believe Microsoft would release such an obviously broken version. Or maybe it's a problem with your code, though the example above looks OK and I don't know of a way to misuse PreparedStatements that would cause such a problem. What makes me a bit suspicious is that a mid-string quite being interpreted as value terminator should cause an SQLException when the rest of the value can't be parsed. Does that happen?
Michael Borgwardt
If there is, I won't be able to reload an updated version of the driver because other programs are using it. It is also running on a server which cannot be rebooted.
phill
If thats really out of your control, then it looks like manual escaping of strings is the only thing you can do, although this is very difficult to get right.
Michael Borgwardt
i'm struggling with it already. I wish java had an easier way of handling strings.
phill
Which version of Java are you using? There are many string manipulation methods already associated to the String class and many more available via projects such as Apache Commonshttp://commons.apache.org/such as StringUtilshttp://commons.apache.org/lang/api-release/org/apache/commons/lang/StringUtils.html
martinatime
i'm using Java 6
phill
There is a more recent jdbc driver version for SS 2005 however I couldn't run the risk of rebooting the server following the install.
phill
+3  A: 

You don't need to handle those characters specially if you're binding them as parameters to a PreparedStatement, as you are doing. That's one of the main benefits of the prepared-statement approach.

araqnid
+3  A: 

Like the others have said, you do not have to do anything to handle the special characters. You need to try a different JDBC driver.

Try using the jTDS driver and see if it helps you with your PreparedStatement. It is an open source database driver for SQL Server. I use it at work now and it works like a champ and actually conforms to the JDBC specification unlike the MS driver.

Mr. Will
could you suggest any example code or links which implements the jar file for a prepared statement?
phill
The code example you have now would work. The only thing that would be different would be the driver url you use to create the connection.
Mr. Will
when i try to run Class.forName("net.sourceforge.jtds.jdbc.Driver"); in a try..catch clause, it keeps telling me "must be caught or thrown" any ideas?
phill
nevermind, when I just change the exception from sqlexception, it appears to be working
phill
+1 for jTDS. Can't recommend it enough.
banjollity
A: 

I'm fairly certain the problem isn't with the code you posted. To help troubleshoot:

  1. Have you tried running the code snippet above in a debugger? What's the value of "itemdescription" before you pass it to the database call?
  2. How are you actually verifying the value in the database? Is this more Java code? Or are you looking at it with SQLCMD or something like that?
Jack Leow