aspose file tools*
The moose likes JDBC and the fly likes Adding to a database with variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Adding to a database with variables" Watch "Adding to a database with variables" New topic
Author

Adding to a database with variables

Ramzi Hajaj
Greenhorn

Joined: Aug 19, 2012
Posts: 13
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

Joined: May 26, 2003
Posts: 31062
    
232

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ramzi Hajaj
Greenhorn

Joined: Aug 19, 2012
Posts: 13
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

Joined: May 26, 2003
Posts: 31062
    
232

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

Joined: Aug 19, 2012
Posts: 13
Thanks a lot. I added column names and it worked fine.

I'm loving this JDBC business.

R
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Why are you concatenating the string literals in the first place? Why not just:


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ramzi Hajaj
Greenhorn

Joined: Aug 19, 2012
Posts: 13
Bear

You make a very grizzly point. I will alter as you have suggested.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

"grizzly point"... I like that!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding to a database with variables