Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5961547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:58:48+00:00 2026-05-22T18:58:48+00:00

My Java program adds items into database. I have a code for generating random

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:58:49+00:00Added an answer on May 22, 2026 at 6:58 pm

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java program that loads thirdparty class files (classes I did not
We have a java program that requires a large amount of heap space -
We have a Java program run as root on Unix, that therefore can read
I am running a java program that sets up a database connection to an
I have a program that reads and draws an SVG file in a java
I am writing a java program that needs a file open dialog. The file
hi i have a little program here that gets a directory from the command
From within my Java program I want to determine which .NET Framework is installed
I wrote a Java program to add and retrieve data from an MS Access.
You launch a java program from a console (maybe using a .bat script). I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.