This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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: 27289
87
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic