Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems using a query

 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • 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.
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • 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 ]
 
Dave Salter
Ranch Hand
Posts: 293
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic