N
N
Neonoviiwolf2016-08-15 17:53:28
MySQL
Neonoviiwolf, 2016-08-15 17:53:28

Why don't options set in MySQL always work in JAVA?

For
a long time I tried to solve a simple problem that was thrown by SQLException: Column count doesn't match value count at row 1
Well, I decided for a long time, because I still managed to write a block for working with the database and thought that there was a problem there, but still.
1) I write in Java, I created a small auxiliary program in which you fill in the data to be entered into the database, I decided to add a couple more columns and set MySQL Workbench 6.3 CE to default to null (I planned to fill them in later), i.e. to. despite the fact that at least the NOT NULL checkbox is not checked, you still need to write something there.
2) Still with id, there are primary key and not null, but the id itself is not filled in (maybe I'm not writing the request correctly)

INSERT = "INSERT INTO users VALUES (?, ?, ?, ?, ?, ?, ?)";

under the first "?" id goes, this option requires me to fill in preparedStatement.setInt(1, id)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vamp, 2016-08-15
@Neonoviiwolf

The problem is not in java, but in the request itself. If you don't list the columns you want to set data for, then mysql assumes that you are going to set data for all existing columns in the table. Regardless of whether there is a default value there or not.
To solve your problem, you need to explicitly enumerate the columns:
All columns not explicitly listed in the query will receive a default value if one was defined when the table was created for them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question