Insert into database
I have a problem, I have a system to add users, I have to make check if this user exist before so that i will not add him again, I retrieved all the name from database into an arraylist.I check first if the array list is empty so that I can add the user else he will check if he exists or not here is the code
if(names.size() == 0){
dbstatement.executeUpdate("
insert into users (user_name,user_password,user_type)
values ('" + username + "','" + userpassword + "','" + type + "')");
JOptionPane.showMessageDialog(rootPane, "user added successfully");
}
else{
for (int i = 0; i < names.size(); i++) {
if (username.equals(names.get(i))) {
JOptionPane.showMessageDialog(rootPane, "Sorry, this name already exist");
break;
}
}
dbstatement.executeUpdate
("insert into users(user_name,user_password,user_type)
values ('" + username + "','" + userpassword + "','" + type + "')");
}
the problem is when the program found a name exist before he told me and add him, i know the cause of this problem all i want to know where to but else of the if inside开发者_StackOverflow中文版 for loop, I want him to tell me the user exist onlyy not to add it again
Just use the SQL WHERE
clause to see if the username exist. There's absolutely no need to copy the entire DB table into Java's memory.
preparedStatement = connection.prepareStatement("SELECT id FROM users WHERE user_name = ?");
preparedStatement.setString(1, username);
resultSet = preparedStatement.executeQuery();
boolean exist = resultSet.next();
Wrap this in a method like boolean exist(String username)
and rearrange your code flow as follows:
if (exist(username)) {
// Show warning message.
} else {
// Insert into DB.
}
Note that PreparedStatement
is been used instead of Statement
. This prevents your code from SQL injection attacks.
Just call the database with each name to add.
Let the sql try to insert the name. It will either insert or throw a key violation (assuming you have a unique constraint on the name).
If it throws a key violation you know the name is already in the database.
If it does not throw an error then the name was inserted.
Read/decide/write style processing is not the way to make this work. It can still have issues when another process inserts a new name in the time between the read and the write. This means that you still have to check for key violations anyway. If you have to check for key violations anyway you might as well do it right the first time and just try inserting all the names.
if(names.size() == 0){
dbstatement.executeUpdate("
insert into users (user_name,user_password,user_type)
values ('" + username + "','" + userpassword + "','" + type + "')");
JOptionPane.showMessageDialog(rootPane, "user added successfully");
}
else{
if ( names.contains(username) ) {
JOptionPane.showMessageDialog(rootPane, "Sorry, this name already exist");
}
else {
dbstatement.executeUpdate
("insert into users(user_name,user_password,user_type)
values ('" + username + "','" + userpassword + "','" + type + "')");
}
}
精彩评论