• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems using a query

 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

I am facing a problem wherein I get the result of a query (let say name of a store) and then use that value to make another query. The problem is sometimes the store's name is like ABC's store. Now because of ' the query string termintaes there because of which an exception is raised.

I would like to enter the query as it probably something like an escape sequence. We use MS Access and MS SQL Server.
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yuck!

Ok, the best way to handle that is to not allow it in the first place.

If, however, the application requirements stated the need for that. I would write a class/method that would accept a string as a argument and parse through the string and replace the ' with a ~a and return the modified string back to be processed in your update/insert query.

You'd have to write another method that does the reverse to the string to display it correctly when you pull it out of your database.

You could use the same methods with other illegal characters as well.

Does that make sense?

Steve
[ September 08, 2005: Message edited by: Stephen Boston ]
 
Ranch Hand
Posts: 293
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you make use of a PreparedStatement instead of a Statement? That way you won't have to worry about doing things with ' characters.
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After doing some research on PreparedStatement (I have never used that before, and most of my SQL experience is in ASP) I'd say Dave hit it on the head for you.

A much more simpler solution than what I offered.

And thanks Dave, I'll add that to my Java bag of tricks that I'm creating.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve and Dave

Steve what you suggested was infact quite close to what I was doing but somehow I didn't really liked the idea of involving the extra overhead. I would give Prepared statments a try. I thought that the issuse would pop up again with prepared statments and never tried. Will try now.

Thanks for your help.
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic