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

How to Insert Data into a table in mysql database

Sultan Altoobi
Ranch Hand

Joined: Dec 11, 2011
Posts: 34
Hello Every Body,,


I trying to insert data into the follwoing table:
login
fields: id --> primary key, int, autoincreament
username -->char
password -->char

here is the code I used:


When I try to run the code it gives me the following error:

SQL Exception: java.sql.SQLException: Column count doesn't match value count at row 1

I don't know what is the problem!

any help please!!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

Your syntax isn't even correct; values need to be separated with commas, not "and". Since you're already using PreparedStatement, let's use it properly to avoid SQL injection:
This will work if and only if your table has exactly two columns. If it has more, like an automatically generated key, you must specify the values you are setting the values for:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sultan Altoobi
Ranch Hand

Joined: Dec 11, 2011
Posts: 34
Rob Spoor wrote:Your syntax isn't even correct; values need to be separated with commas, not "and". Since you're already using PreparedStatement, let's use it properly to avoid SQL injection:
This will work if and only if your table has exactly two columns. If it has more, like an automatically generated key, you must specify the values you are setting the values for:



Thanks alot,,

now I understand.

and great comments from you on how to avoid sql injection!

My Regards.
Sultan Altoobi
Ranch Hand

Joined: Dec 11, 2011
Posts: 34
Hi again,,

I made changes to the code and it works fine! Thank you again Rob Spoor.

Now How I can avoid sql injection in the following syntax(Select statetment):





any help please!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

Replace any value with a single question mark (?); there should be no more quotes in the statement. Next, for each question mark, use one of the PreparedStatement's setXXX methods to set the values, with indexing starting at 1.

In your case there are two values: username1.getText() and password1.getText(). Similar to the insert statement I wrote down, your statement becomes Note the absence of quotes.
Now you can use setString (because both values are Strings) to set these values:
After you've set all values, execute your statement as usual.
Sultan Altoobi
Ranch Hand

Joined: Dec 11, 2011
Posts: 34
Thank you Rob Spoor!

I'm Gratefull to you. Thanks for help.

My Regards,,,
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

You're welcome.
peter m hayward
Ranch Hand

Joined: Sep 27, 2012
Posts: 31
just thought i say thanks as well this solved my issue
pete
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Insert Data into a table in mysql database
 
Similar Threads
Connection.prepareStatement is throwing "General Error Exception"
How to insert password in an encrypted form into a table
Update Statement Problem
Can't insert Data
"Function sequence error!" what is it mean