File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes dealing with ' -- an apostrophe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "dealing with Watch "dealing with New topic

dealing with ' -- an apostrophe

Sam Kebab
Ranch Hand

Joined: Jan 23, 2002
Posts: 104
I am having problems inserting some rows that include ' -- i.e. an apostrophe.
For instance M'PLX-W.
My program inserts around 5 to 10 thousand rows a night. I can't manually put an escape character. Would anyone know how i can handle this. For this program i will be using MS SQL database.
ps. I tried using prepared Statements -- and they seem to work fine; but much to my dismay prepared statements are (literally) running 20 times slower than the regular create statement.
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 567
I don't know why your prepared statements are running slower, someone else will have to answer that.
But you didn't say what the problem with the apostrophes actually is. Presumably they're causing your SQL statements to break? Or is it your HTML/Javascript that they're breaking? You have to escape them or substitute them.

I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
Richard Whitely

Joined: Feb 08, 2002
Posts: 2
It sounds like u are preparing the statment every time u use it, prepare it once then use it many times to see the performance improvment over a statment.
Dharmesh Chheda
Ranch Hand

Joined: May 08, 2001
Posts: 204
if its a form then when you get the data in the next page .. call a bean which modified the string by adding an escape character before the '
call the bean , set the string , call the modify function and then get the string by the getter method.
use any string manipulation method like StringBuffer etc...
i did the same .. then pass the modified string for insertion in the database...
let me know if it works for you

regards,<br />Dharmesh Chheda
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

sounds like you can do a few things to improve performance:
1. declare the PreparedStatement outside the loop to insert 10000 rows
2. Batch the preparedstatements

batching your statements will give you a huge performance gain. PreparedStatements when utilized properly will give you performance gains on repeated execution of the same statements.
Give it a try and see what happens, or maybe post some relevant code (if there is a lot, then skeleton it for us!)
Sam Kebab
Ranch Hand

Joined: Jan 23, 2002
Posts: 104
Thank you for the help -- everyone.
I can't wait to try out your advice -- on Monday. I have a strong feeling it will work.
Sam Kebab
Ranch Hand

Joined: Jan 23, 2002
Posts: 104
Hello Jamie.
Wow!!! The batch update has made the program loop "faster than a speeding bullet". (although i used the regular create statement).
I have one small problem though when using the batch update with a prepared statement -- the error reads:
invalid transaction state
-- i think the line: int [] inserts = ps.execut..
reads - (negative).
I agree. Here's the link:
subject: dealing with ' -- an apostrophe
It's not a secret anymore!