• Post Reply Bookmark Topic Watch Topic
  • New Topic

a little confused!  RSS feed

 
joew weakers
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there. Just a quickie. I am trying to match a username entered into a JTextField against a couple of usernames held in my database. Firstly I send an SQL select statement via JDBC to my database to return all valid usernames and then put all the results of the query into a Vector called userNames. This works fine. If I iterate through the vector and print the names to the console it works correctly and returns all the valid usernames. However if I then enter a username into the JTextField and hit return it does not return a match against elements of my vector even if the name entered is a valid one. Below is the relevant code.
{
Container c = getContentPane();
c.setLayout(new FlowLayout());
label1 = new JLabel("Please enter your username");
c.add(label1);
text1 = new JTextField(25);
text1.setToolTipText("This is where you enter your name");
c.add(text1);
Connection cont = new Connection(); //establishes the connection correctly
Statement stmt = cont.conn.createStatement();
String query = "Select user_name from map_users"; //returns all valid users
OracleResultSet ors = (OracleResultSet) stmt.executeQuery(query);
while(ors.next()) //iterates thru the result set produced
{
String name = (String) ors.getString(1);
userNames.add(name); //adds usernames to the vector userNames
System.out.println(name);
}
ors.close();
SystemUserTextHandler handler = new SystemUserTextHandler();
text1.addActionListener(handler);
}
private class SystemUserTextHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == text1)
{
System.out.println("hi " + e.getActionCommand()); //prints name to console
int i = 0;
while(i < userNames.size())
{
if(userNames.elementAt(i) == e.getActionCommand()) //maybe is wrong
{
System.out.println("Valid Username");
break;
}
else
{
System.out.println("Invalid Username"); //this is returned even if name should match
}
i++;
}
}
 
Tim Troy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When doing String comparisons, use the equals method. With Strings, the == will tell you if the objects are the same instance (for the most part).
if(userNames.elementAt(i).equals(e.getActionCommand()))
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also might want to try using the contains method in Vector. This would eliminate the loop.
Is this firing every time you enter a single character in your textfield?
 
joew weakers
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers Tim, Paul. I feel like an idiot now. I was staring at that all morning and could not see it. I have just realised something that somebody pointed out to me. When the user enters their name, instead of comparing that name to all names in the database, why not add it on to the select statement as a where clause. This I reckon will make more sense than going through the whole formality of comparing the name to each item in my Vector.
I presume that this would be quicker than cycling through the vector. Thanks again...Joe
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!