7

I'm trying to update values using JDBC and I continue to get the same error for different tables and with different schemas.

Let's say that I have a table like this

+----------------+-------------+------+-----+---------+-------+
| Field          | Type        | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| field1         | varchar(50) | YES  |     | NULL    |       |
| field2         | varchar(50) | YES  |     | NULL    |       |
+----------------+-------------+------+-----+---------+-------+

then, I try to add a row:

String Text1 = text1;
String Text2 = text2;
String Query_String = "INSERT INTO tablename(field1,field2) VALUES ('"+Text1+","+Text2+"')";
Query_Statement.executeUpdate(Query_String);

the number of columns is the same, and also in the text there are not other commas, but I continue to get the error "java.sql.SQLException: Column count doesn't match value count at row 1"

I'm sure it's something simple, probably on the syntax since I managed to make it to work with a single column...

Thanks in advance

Andrew Strathclyde
  • 327
  • 4
  • 10
  • 23

3 Answers3

9

There is something wrong with:

String Query_String = "INSERT INTO tablename(field1,field2) VALUES ('"+Text1+","+Text2+"')";

You've missed some quotes between Text1 and Text2:

String Query_String = "INSERT INTO tablename(field1,field2) VALUES ('"+Text1+"','"+Text2+"')";
Giann
  • 3,142
  • 3
  • 23
  • 33
4
String Query_String = "INSERT INTO tablename(field1,field2) VALUES ("'"+Text1+"' , '"+Text2+"');";  

It should like this note ' this

PreparedStatement would be better choice.

Angel Angel
  • 19,670
  • 29
  • 79
  • 105
jmj
  • 237,923
  • 42
  • 401
  • 438
1

you have got a mistake with your quotes...

the following will be executed:

INSERT INTO tablename(field1,field2) VALUES ('Text1,Text2'); 

you have to write:

String Query_String = "INSERT INTO tablename(field1,field2) 
VALUES ('"+Text1+"','"+Text2+"')"

Tobias Bambullis
  • 736
  • 5
  • 17
  • 45