• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

checking a mysql table if my generated number is unique

 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have this code that will check if my generated number is unique, I am using an infinite loop that will break if there is no duplicate found, I am not quit confident in my algorithm and I am hoping that if you could suggest better ways of doing this.

 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, just to make sure I understand, you are generating a number and want to ensure it is unique (after encryption)?
To do this you want to query the DB column to ensure there isn't an encrypted version already in the table.

Assuming this is what you need (and I can't say either way) I would just do the query and check resultSet.next(). That returns true if there is a row, so you then want to continue.
I would also not use an int, but a boolean for x and rename it to duplicateFound (or something like that) and have it as true, that way you can assign resultSet.next() to it directly and skip most of those lines at the end.



I would also remove the left over code, such as the statement on line 8, and the concatenation of the query.
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:So, just to make sure I understand, you are generating a number and want to ensure it is unique (after encryption)?
To do this you want to query the DB column to ensure there isn't an encrypted version already in the table.

Assuming this is what you need (and I can't say either way) I would just do the query and check resultSet.next(). That returns true if there is a row, so you then want to continue.
I would also not use an int, but a boolean for x and rename it to duplicateFound (or something like that) and have it as true, that way you can assign resultSet.next() to it directly and skip most of those lines at the end.



I would also remove the left over code, such as the statement on line 8, and the concatenation of the query.


What you assumed is correct I am generating a unique set of number and encrypting them, and afterwards I will check the database for any duplicates if there are then I will simply generate a new number until no duplicates is found.

I see thank you for your input, so my logic in the conditional part is efficient as is?

I will do as you said and proceed to edit my code right away. thank you once again
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you replace lines 15 through 23 in the code you posted above with the line in my post, and obviously change x to the boolean, it will be easier to read.

Is this a single threaded app?
That is, is this bit of code only accessed by a single thread at a time, and is not going to be in, say, a web app?
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes, and don't forget to close your resources (statements and result sets and, if need be, the connection).
As that code stands you will be leaking resources.
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:If you replace lines 15 through 23 in the code you posted above with the line in my post, and obviously change x to the boolean, it will be easier to read.

Is this a single threaded app?
That is, is this bit of code only accessed by a single thread at a time, and is not going to be in, say, a web app?


Thanks! and yes this is a single thread app, it is inside a function and will only be called when needed, also I closed the resources. I just didn't post the whole function just this part where I needed some advice.
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way Rodolfo, you may want to take a look at your other topic: http://www.coderanch.com/t/658103/java/java/AES-secure-key-creation
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic