This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Hi Ranchers, I came up with the following code that sniffs out any apostrophe's occuring in data that is to be inserted/updated to an SQL Server table:
The problem is that I have some misgivings with the above code about being slow or not being fully optimized. Any ideas? I'm still using JDK 1.3.1 - would regular expressions in JDK 1.4 help a lot? Thanks in advance for your opinions. Ex Animo Java! -- Val [ May 09, 2002: Message edited by: Val Pecaoco ]
"Knowledge is power, but enthusiasm is the key." -- Lavern Barn
mustang india
Ranch Hand
Joined: Feb 05, 2002
Posts: 60
posted
0
Why cant we use just this String s = "This is ''' my string"; System.out.println(s); s = s.replace('\'', '\"'); System.out.println(s);1 This prints out This is ''' my string and This is """ my string
I've been put on record before, and I still say (even though it isn't the strict purpose) to use PreparedStatements. Instead of trying to figure out what needs escaping and how to escape it, make it the responsibility of the Driver. Dave
Val Pecaoco
Ranch Hand
Joined: Dec 05, 2001
Posts: 156
posted
0
Hi mustang,
Originally posted by mustang india: Why cant we use just this String s = "This is ''' my string"; System.out.println(s); s = s.replace('\'', '\"'); System.out.println(s);1 This prints out This is ''' my string and This is """ my string
But the string that will be stored in the database will be "This is """ my string", and not the original "This is ''' my string". What my code did was to replace a single apostrophe with two, which, to a certain effect, the first apostrophe is a sort of escape character that tells SQL Server to accept the second apostrophe as data. I used the replace() method in StringBuffer because the replace() method in String can only replace a character with another single character, and '' obviously counts as two (which, formally, makes '' a String). [ May 09, 2002: Message edited by: Val Pecaoco ]
mustang india
Ranch Hand
Joined: Feb 05, 2002
Posts: 60
posted
0
What I had given is just an eample, u can replace any number of single quotes with double quotes when u say s = s.replace('\'', '\"'); This will replace all occurences of ' with ". Yeah u can use this with StringBuffer as well. Regards, Mustang.
Val, you create a lot of temporary strings in your code. Each substring method returns a new temporary string. A better way may be to convert the string to a char array like this:
That should cut down on the number temporary String objects created. Ex Animo Java! Jamie [ May 13, 2002: Message edited by: Jamie Robertson ]