• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Sniffing out apostrophe's in SQL Server

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic