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>
What I am expecting is that, I should not be able to add 2 rows with same username because username is 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 insertUser returns false.. However when I check the database, I see a new row insterted. What am I doing wrong? And the insertuser:
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;
}