Java:检查自动生成的 ID

发布于 2024-11-09 18:53:36 字数 1539 浏览 0 评论 0原文

我的 Java 程序将项目添加到数据库中。我有一个用于生成随机字符串的代码,该字符串将用作项目 ID。我想制作 IDchecker(id) 来检查 ID 是否已存在于数据库中。

如果我有 codeIDgenerator() 和 IDchecker(id) 方法,如果 ID 已存在,如何创建一个循环生成新代码,或者如果 ID 是唯一的并且不在数据库中出现,则如何退出循环?

另外,我的 IDchecker(id) 方法遇到了问题,我正在使用 ResultSet 从 SQL 中带回数据,但我找不到方法来确定 ResultSet 有多少行(如果有的话)。 resultSet 没有 isEmpty() 吗?

这是代码:

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);
        }
    }

这是 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
        }
}

谢谢

My Java program adds items into database. I have a code for generating random string that will be used as an item ID. I want to make IDchecker(id) that will check whether the ID already exists into database.

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?

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). There is no isEmpty() for resultSet?

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

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

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

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

发布评论

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

评论(1

你好,陌生人 2024-11-16 18:53:36

虽然生成唯一 ID 最好在数据库中完成,但我可以帮助您简化代码。您不需要检查数据库两次。

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);
    }
}

但是,您可以获取下一个 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);
    }
}

While generating unique id is best done in your database, I can help you simplify your code. 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.

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