• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having problems with a String  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • 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,

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to concatenate the strings using the '+' operator.

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • 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: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 518
4
  • Mark post as helpful
  • send pies
  • 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: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 518
4
  • Mark post as helpful
  • send pies
  • 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
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!