Win a copy of Mastering Non-Functional Requirements this week in the Design forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

User information from MySQL Database  RSS feed

 
Dean Hampson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I have an action listener on a Register button. Once the user types in their name, nothing happens. I want the program to detect whether that username already exists in the database, and if it does, show the takenLabel. If the username does not exist, create it. When I try this program, nothing happens when I click the Register button.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36833
481
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dean,
You say "nothing happens". Does the regButtonActionPerformed method even get called? If so, what is the lat line run in it? Does it skip the loop? Youc an use the debugger or add System.out.println() statements to get this information.
 
Dean Hampson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works if I don't add the else block. It will display the red takenLabel in the dialog. But, if I add the else block, none of it works. I think the action does get called but ignores both if and else.
 
Ron McLeod
Saloon Keeper
Posts: 1486
210
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is unlikely that the name will already be in the database, another approach would be to just blindly try to insert the name in the accounts table, and handle cases where it was unsuccessful.  For example using MySQL:

This of course would require that the name field has a primary key or unique constraint, such as:
 
Paul Clapham
Sheriff
Posts: 22215
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dean Hampson wrote:I think the action does get called but ignores both if and else.


Are you running this code in a context where you could see a stack trace if it got printed to System.err? I ask because your code looks like it's part of a GUI application, and they are often run in such a way that the console (and hence any System.out and System.err output) can't be seen. It's possible that an exception is being thrown.

Also I don't think that your logic "Write one record for this user for each record already in the database which is for some other user" is a good idea. And closing the connection after adding the first one is just going to break things the next time around the loop, which would throw an exception. Which maybe you can't see it. Anyway as I was saying, reading the whole table and scanning through it in Java code for one possible record isn't the right way to do things. If you want to look for a particular record, write a query which does that and let the database do the searching. I know you can write such a query because your code exhibits a good knowledge of how to use PreparedStatement (+1 for that).
 
Dave Tolls
Ranch Hand
Posts: 2652
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To echo Ron, this sort of check is usually pointless.  After all, you still need to deal with the situation where the user name is grabbed in between the check and the insert.
It's a lot more reliable to just do the insert.

One point, though, is your query.  When looking for data in a db don't grab all the data from a table and then iterate over the result set to look for the thing you are after.  This method is colloquially known as Slow-By-Slow.  Write a query that returns you the answer you are looking for, in your case "SELECT 1 FROM <table> WHERE uname = ?".  Then just check rs.next().  If true then the name exists, else it doesn't.

ETA:  of course the second paragraph was echoing Paul!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!