So this is my table:
mysql> DESCRIBE app_user;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(40) | NO | PRI | NULL | |
| password | varchar(40) | NO | | NULL | |
| email | varchar(40) | NO | PRI | NULL | |
+----------+-------------+------+-----+---------+----------------+
4 rows in set
mysql>
I expect that I could not add 2 lines with the same username, because the username is the primary key.
And this is my Java code:
@Test
public void shouldNotInsertWithSameUserName() throws IOException, SQLException {
AppUserAccessObject appUserAccessObject = new AppUserAccessObject(new DatabaseConnectionImpl());
Assert.assertFalse(appUserAccessObject.insertUser("koray", "email", "password"));
}
So this test passes, which means that insertUser returns false. However, when I check the database, I see a new line. What am I doing wrong? And the insert:
public boolean insertUser(String username,String email,String password) throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO "+ tableName + "(username,email,password) VALUES(?,?,?)");
preparedStatement.setString(1,username);
preparedStatement.setString(2,email);
preparedStatement.setString(3,password);
boolean execute = preparedStatement.execute();
return execute;
}
source
share