• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Having problems with a String

 
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I'm trying to make a string that will be used an SQLstatement into a database but I can't seem to figure out why I get an error when declaring the String,

 
Saloon Keeper
Posts: 8562
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to concatenate the strings using the '+' operator.

 
Adam Chalkley
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You need to concatenate the strings using the '+' operator.



Hey Carey that worked thanks

I'm just wondering why do you need to concatenate in this instance?

Thanks
 
Carey Brown
Saloon Keeper
Posts: 8562
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you think about it, what would the compiler do with
a = b c
You have two operands, 'a', and 'b', are they supposed to be added, subtracted, concatenated, etc.? The compiler needs to know what you want it to do with the two operands.
 
Adam Chalkley
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:If you think about it, what would the compiler do with
a = b c
You have two operands, 'a', and 'b', are they supposed to be added, subtracted, concatenated, etc.? The compiler needs to know what you want it to do with the two operands.



Not 100% sure but I'll try wrap my head around it, Thanks for the help =)
 
Carey Brown
Saloon Keeper
Posts: 8562
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a bit rusty with my SQL but it seems that if you're going to insert a numeric value then it shouldn't be enclosed with single quotes ('). Because you are using an int for the ID I'm assuming the your ID column in the database is also an integer.
 
Adam Chalkley
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I'm a bit rusty with my SQL but it seems that if you're going to insert a numeric value then it shouldn't be enclosed with single quotes ('). Because you are using an int for the ID I'm assuming the your ID column in the database is also an integer.



Yes it is indeed ant INT in the database thanks nice spot I have to change that
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Chalkley wrote:I'm just wondering why do you need to concatenate in this instance?


Without meaning to sound patronising its because the JLS says you have to. See string concatenation: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.1
In some languages, for instance php, you can embed variables in a string and they will be expanded to produce the complete string but Java doesn't support this so you have to concatenate the various parts to form the complete string.
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic