简体   繁体   English

Java:检查自动生成的 ID

[英]Java: Check auto-generated ID

My Java program adds items into database.我的 Java 程序将项目添加到数据库中。 I have a code for generating random string that will be used as an item ID.我有一个用于生成将用作项目 ID 的随机字符串的代码。 I want to make IDchecker(id) that will check whether the ID already exists into database.我想制作 IDchecker(id) 来检查 ID 是否已经存在于数据库中。

If I have codeIDgenerator() and IDchecker(id) methods, how do I make a loop that will generate new code if the ID already exists, or will exit the loop if the ID is unique and doesn't come up in the database?如果我有 codeIDgenerator() 和 IDchecker(id) 方法,我如何创建一个循环,如果 ID 已经存在,将生成新代码,或者如果 ID 是唯一的并且没有出现在数据库中,则将退出循环?

Also I'm having trouble with my IDchecker(id) method, I'm using ResultSet to bring back the data from SQL, but I can't find a way to determine how many rows does ResultSet has (if at all).我的 IDchecker(id) 方法也有问题,我正在使用 ResultSet 从 SQL 带回数据,但我找不到确定 ResultSet 有多少行的方法(如果有的话)。 There is no isEmpty() for resultSet? resultSet 没有 isEmpty() 吗?

Here's the code:这是代码:

public void AddItem() {
    boolean checkCode = false;
    while (checkCode == false) {
        Random r = new Random();
        int numbers = 100000 + (int) (r.nextFloat() * 899900);
        String ID= Integer.toString(numbers);
        try {
            if (DatabaseConnection.checkID(ID) == false) {
                checkCode = true;
                System.out.println("ID is unique");
            } else if (DatabaseConnection.checkID(ID) == true) {
                System.out.println("ID is NOT unique");
            }
        } catch (SQLException ex) {
            Logger.getLogger(ModelTabeleIntervencija.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

And here is the ckeckID(ID) method这是 ckeckID(ID) 方法

public boolean CheckID(String ID) throws SQLException {
    String query = "SELECT itemId FROM items WHERE itemID= '"+ID+"'";
    Statement dbStatement = connection.createStatement();
    ResultSet rsItems= dbStatement .executeQuery(query);
        if (rsItems.isEmpty( )== true){
            return false;
            // ID not found - is unique
        } else{
        return true;
            // ID found - is not unique
        }
}

Thanks谢谢

While generating unique id is best done in your database, I can help you simplify your code.虽然生成唯一 ID 最好在您的数据库中完成,但我可以帮助您简化代码。 You shouldn't need to check your database twice.您不需要检查数据库两次。

private final Random r = new Random();
public String getUniqueId() {
    try {
        while (true) {
            int n = r.nextInt(1000 * 1000) + 1000 * 1000;
            String id = ("" + n).substring(1); // number between 000000 and 999999
            if (DatabaseConnection.checkID(id))
                return id;
        }
    } catch (SQLException ex) {
        throw new IllegalStateException("Cannot access database", ex);
    }
}

However, instead of generating a random id, you could just get the next id.但是,您可以获取下一个 id,而不是生成随机 id。

public String getUniqueId() {
    try {
        String maxId = DatabaseConnection.selectMaxId();
        int n = Integer.parseInt(maxId) + 1;
        return ("" + n).substring(1); // number between 000000 and 999999
    } catch (SQLException ex) {
        throw new IllegalStateException("Cannot access database", ex);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM