File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes not too sure about this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "not too sure about this" Watch "not too sure about this" New topic

not too sure about this

joew weakers
Ranch Hand

Joined: Aug 21, 2003
Posts: 48
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");
text1 = new JTextField(25);
text1.setToolTipText("This is where you enter your name");
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( //iterates thru the result set produced
String name = (String) ors.getString(1);
userNames.add(name); //adds usernames to the vector userNames
SystemUserTextHandler handler = new SystemUserTextHandler();
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");
System.out.println("Invalid Username"); //this is returned even if name should match

Joined: Aug 16, 2003
Posts: 13
hey i did find n e thing like test1.getText(); where u take the test from the field, before doing this check that the field should not be empty and then take the data from the field.

no GOD except ALLAH...
martin chile

Joined: Jun 24, 2002
Posts: 21
if ((String)userNames.elementAt(i)).equals(e.getActionCommand()){
maybe it helps!
yours martin
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
why aren't you adding the username to your SQL Query (WHERE condition? It is nonsense to retrieve first all data and then to iterate through
the ResultSet when you can handle this in the DB.
The DB handles such stuff a lot faster than java
joew weakers
Ranch Hand

Joined: Aug 21, 2003
Posts: 48
Cheers Martin, Oliver for your responses. Oliver thanks for pointing out that little matter. That really makes a hell of a lot more sense than trying to achieve my goal using my method. Is this code a little messy as somebody told me not to embed too many if, while, for statements in the one method as it can slow processing down:
if(e.getSource() == text1)
System.out.println("hi " + e.getActionCommand());
currentUser = text1.getText();
Connection cont = new Connection(); //establishes connection to database
Statement stmt = cont.conn.createStatement(); //creates stmt
String query = "Select user_name from map_users where user_name = '"+currentUser+"'";
OracleResultSet ors = (OracleResultSet) stmt.executeQuery(query); //executes the SQL stmt
System.out.println("Welcome to JoeMap");
OracleSpatialLayer osLayer = new OracleSpatialLayer();
else(System.out.println("Sorry but"));

catch(Exception h){}
I am not too sure as I am only starting out programming but is this OK!
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
looks decent enough, though I have no clue as to what those Oracle specific classes are supposed to do.
Do make sure to close your JDBC resources before terminating!
And of course keep in mind that as you are working now you're checking the username in a case-sensitive manner. This is usually not what you want (often usernames are not case sensitive, passwords are).
Best check username.toUpperCase() with the uppercase version of the name as it exists in the database.
Most SQL dialects have a UCASE() or UPPERCASE() function you can use.
Your query would become something like:
"Select user_name from map_users where UCASE(user_name) = '"+currentUser.toUpperCase()+"'";

I agree. Here's the link:
subject: not too sure about this