Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding to a database with variables

 
Ramzi Hajaj
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys

Am new to JDBC and am having some problems inserting into a table using variables. I am inserting into a table Players(PlayerID, firstname, surname, attempts). Player ID is autoincrement.

Here is the code:



Can anyone see where i am going wrong? Apparently i have an error in my sql syntax.

Thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. You are missing a space.

Your Java String evaluates to:


Since playervalues isn't the name of a table, it blows up. Add a space between the table name and values keyword and you should be good. This is actually a very common "beginner to JDBC" mistake so don't worry about it.
 
Ramzi Hajaj
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks thats good to know. Nnow its saying

Caught SQL Exception - Column count doesn't match value count at row 1

One of the values is an auto increment value but i have left that out as thought that was correct. Is this what is causing this exception?

Thanks for your help
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh right. You need to name the columns in your sql statement so the database knows which one to skip. It's a good practice anyway in case things change later.
 
Ramzi Hajaj
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. I added column names and it worked fine.

I'm loving this JDBC business.

R
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you concatenating the string literals in the first place? Why not just:
 
Ramzi Hajaj
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear

You make a very grizzly point. I will alter as you have suggested.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"grizzly point"... I like that!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic