Your primary key is based on 3 fields:
- id (which has auto increment value)
- username
So, the primary key restricts a new row with these 3 fields having the same value. But since id
is marked on auto increment and you're always sending null
to it, it will generate a new value, thus never having these three fields with the same value.
A better design for your table would be having id
as primary key only, and an Unique Key for your username
and email
fields. In fact, it would be even better having two unique keys: one for username
and another for email
.
This will do it:
CREATE TABLE app_user (
id INT PRIMARY KEY AUTO_INCREMENT,
username varchar(40),
password varchar(40),
email varchar(40),
UNIQUE KEY (username),
UNIQUE KEY (email)
);
Apart from the design problem in your database:
- Use
PreparedStatement#executeUpdate
to execute DML statemens likeINSERT
,UPDATE
andDELETE
. - Use
PreparedStatement#executeQuery
when retrieving data from database through aSELECT
. - Use
PreparedStatement#execute
when performing any kind of SQL statement likeCREATE TABLE
orALTER TABLE
.
Prepared statement returns false but row is inserted?
From PreparedStatement#execute
javadoc:
Returns:
true
if the first result is aResultSet
object;false
if the first result is an update count or there is no result.
In this case, you got a false
result since you executed an update operation (INSERT
is understood as an update operation).