• 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

Issue with a String Array.

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

I have a routine that needs to create a string by concatenating a hard coded string with one element of a passed-in string array. Basically, I have the following code snippet:



This new_string value never gets created correctly - comes up as null. However, if I take out the two single quotes from the hard coded strings, then it gets created correctly. can someone point me to a resource that explains this? I'm not sure what I need to do. I need those single quotes to properly format a MySQL query.

Thanks in advance.
Scott
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There isn't any resource that explains why that happens, mainly because it doesn't. I would ask you to post more code, but I'm going to focus on your comment about MySQL. Don't build queries for JDBC by concatenating a bunch of strings like that. For one thing it's hard to get all the quotes just right (as you already saw), but for another thing if your criteria[0] string contained a quote then your query would not be well-formed. And for another thing you open yourself to SQL injection attacks when you do that. You are far better off to use a PreparedStatement, then you would only need a single predefined query string in most cases.
 
Scott Updike
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. This was mainly used for testing query strings and thought preparedstatements might be too involved (they are used everywhere else in the base app). Ok, I'll continue using PreparedStatements...

Scott
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic