插入数据库

发布于 2024-11-07 02:37:57 字数 972 浏览 0 评论 0原文

我有一个问题,我有一个添加用户的系统,我必须检查该用户之前是否存在,这样我就不会再次添加他,我从数据库中检索所有名称到数组列表中。我首先检查数组列表是否存在为空,以便我可以添加用户,否则他将检查他是否存在 这是代码,

    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 + "')");
    }

问题是当程序在他告诉我并添加他之前发现一个名称存在时,我知道这个问题的原因,我只想知道在哪里,但除了 for 循环内的 if 之外,我想让他告诉我的用户存在只是为了不再次添加它

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 for loop, I want him to tell me the user exist onlyy not to add it again

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原来分手还会想你 2024-11-14 02:37:57

只需使用 SQL WHERE 子句即可查看用户名是否存在。完全没有必要将整个 DB 表复制到 Java 内存中。

preparedStatement = connection.prepareStatement("SELECT id FROM users WHERE user_name = ?");
preparedStatement.setString(1, username);
resultSet = preparedStatement.executeQuery();
boolean exist = resultSet.next();

将其包装在像 boolean exit(String username) 这样的方法中,并按如下方式重新排列代码流:

if (exist(username)) {
    // Show warning message.
} else {
    // Insert into DB.
}

请注意 PreparedStatement 被用来代替 Statement。这可以防止您的代码遭受SQL 注入攻击

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.

活雷疯 2024-11-14 02:37:57

只需使用要添加的每个名称调用数据库即可。

让 sql 尝试插入名称。它将插入或抛出键冲突(假设您对名称有唯一约束)。

如果它引发密钥违规,您就知道该名称已在数据库中。

如果没有抛出错误,则名称已被插入。

读/决定/写风格处理并不是完成这项工作的方法。当另一个进程在读取和写入之间插入新名称时,它仍然可能会出现问题。这意味着您仍然必须检查关键违规行为。如果您无论如何都必须检查关键违规行为,那么您最好第一次就做对,然后尝试插入所有名称。

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.

香橙ぽ 2024-11-14 02:37:57
 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 + "')");
    }
}
 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 + "')");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文