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

java sql query

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In jdbc code if I need to do some query like

select address from station where name like '%La'Plata%';

If does not work. I want to query rows whose name include La'Plata. But I guess since it includes a special character ('), it doesn't work. How should this be fixed ?
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The quick and dirty answer is to escape the single quote with a second single quote:


However, it's far better to use parameterized queries if you're going to be accepting input from users to avoid SQL Injection attacks (and just good practice in any case):



This allows you to avoid worrying about escaping special characters while minimizing security risks like this one
 
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
yep, old 'Bobby Tables'

Nice post, Stevi
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stevi Deter:
The quick and dirty answer is to escape the single quote with a second single quote:


However, it's far better to use parameterized queries if you're going to be accepting input from users to avoid SQL Injection attacks (and just good practice in any case):



This allows you to avoid worrying about escaping special characters while minimizing security risks like this one



Thanks Stevi. I like the 2nd approach. but for the 1st approach, what if my search term includes a special char like %, should I still use ' as the escape ?
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Raj,

For the wildcard characters '_' and '%' in a Like statement, I think you need to use the escape syntax, which lets you define the escape character sequence:



This example should match any string that has a literal % character.
 
Fire me boy! Cool, soothing, shameless self promotion:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic