Don't know where is the problem--please help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Don't know where is the problem--please help

  1. #1
    Join Date
    Apr 2003
    Posts
    7

    Question Don't know where is the problem--please help

    i try to display a retrieved user name on JTextField. the sql query i made can successfully retireve the data on the system.out.println() which means the connection is no problem.
    however, i got some problems in the JtextField and maybe the problem domain class too.
    the following is part of my code:
    Code:
    //Data Access class
    //get user name from DB when user type in corrent id and password
    public static String retrieveUserName(UserInfoPD u) throws SQLException
    {     
            Statement statement = connection.createStatement();
            String query = "SELECT UserName FROM LogIn WHERE UserId = '" + u.getUserID() + "'" +
                            "AND Password = '" + u.getPassword() + "'";
            
            ResultSet r = statement.executeQuery(query);  
            String s1 = null;
            if(r.next())
            {
                s1 = r.getString(1);
            }
            r.close();
            statement.close();
            return s1;       
    }       
    
    //send retrieved data to here and return it
    //Problem Domain class***************
    public String retrieveUserName() throws SQLException 
    {      
           // UserInfoDA.connect();      
           return UserInfoDA.retrieveUserName(this);
    }
    
    //GUI class *****************
    private void startIt()
    {       
            try       
            {           
                  messageField1.requestFocus();
                  UserInfoPD u = new UserInfoPD(); //got problem here          
                  messageField1.setText(u.retrieveUserName()); //got problem here      
            }       
             catch (Exception e) 
             {          
                     (e.toString());          
                     System.exit(1);       
             }       
    }
    Can anyone please tell me what's wrong with it because the result i got is nothing.

    and I have the feeling the problem might be in my PD class. Please help me i am a beginner of java. If need, i will post my PD class later.

    Thanks in advance

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    What actually is the problem? Null pointer error? What line? Please post the error.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Apr 2003
    Posts
    7
    after i run the program, I got no exception.
    Everything seems compile and run fine.
    The problem is, I should get userName on JTextField but it displays nothing on it, just a blank JTextField.

    so I think either my code in GUI class is wrong or i have some logic problem.

    I have tried so many times and ways but still got the same blank result. really have no idea now.

    oh, but in Data Access class, i have used a main method to test whether i can get the data i want, and Yes, i can get the user name i want when i just test in main method in Data access class.

    Thanks for the help

  4. #4
    Join Date
    Mar 2003
    Posts
    86

    Laeds

    UserInfoPD u = new UserInfoPD(); - Whats in an empty UserInfoPD?

    Try printing the select you run, and the reault.
    If that is ok, try to set anything to the JEdit field.

    That should point out where the problem is.

    Sharbov.

  5. #5
    Join Date
    Apr 2003
    Posts
    7
    You are right

    if i have 3 records such as id=00001, 00002, 00003, password = 12345, 54321, 11111, userName = A, B, C, respectively.
    When I type in directly like this
    Code:
    UserInfoPD u = new UserInfoPD("00002","54321");
    I will get the result i want.
    HOwever, waht I really try to get is to have another GUI to have Users input their ID and Password, then pass these 2 info to SELECT query then return the right Name back to JTExtField GUI.

    So basically I know i have to have anything inside UserInfoPD u = new UserInfoPD(HERE);

    BUt I don't know what to pass to (HERE) now.
    Sorry I am still a newbie and thank you for pointing me out this so I know what's the problem now. JUst that I am not good enough to know what. But i will keep trying.

    Thanks. really appreciate

  6. #6
    Join Date
    Apr 2003
    Posts
    7
    ok, here is what i tried to pass user input id and password to UserInfoPD() problem domain class.
    Code:
    LogInFrame logFrame = new LogInFrame();  //this is the ID Password GUI that user inputs info 
    String id = logFrame.messageField1.getText();
    String password = logFrame.messageField2.getText();
              
    UserInfoPD u = new UserInfoPD(id, password);  //i pass id, password to here
    System.out.println(id + " " + password);//in here, it still get me a blank result
    messageField1.requestFocus();                    
    messageField1.setText(u.retrieveUserName());
    I am so confused. When i have System.out.println(id + " " + password); in LogInFrame class, it prints out the id and password that i entered. However, in the class i provided above, it just seems return nothing.

    Can anyone please tell me what to do or where i am doing wrong.

    Thanks

  7. #7
    Join Date
    Mar 2003
    Posts
    86

    What is returned?

    Dont do this:
    messageField1.setText(u.retrieveUserName());

    Do that

    String userName = u.retrieveUserName();

    System.out.println(userName);

    messageField1.setText(userName);

    Just to see what is returned.

    If you have a valid return value, then the problem is pure UI. We'll go on from there.

    Sharbov.

  8. #8
    Join Date
    Apr 2003
    Posts
    7

    return is Null

    Thank you for your help,
    After I use System.out.println(userName);
    I got a Null return.

    Does this mean that my user input ID and Password didn't get through it?

    Thanks again

  9. #9
    Join Date
    Mar 2003
    Posts
    86

    hmmm...

    Well,

    It only means that the return from the data object is null, and that there is no UI problem (so far).
    Now lets see why...
    Try adding more System.out
    Print the select statment you fire, pring the result from the ResultSet.
    That should get us there.

    Sharbov.

  10. #10
    Join Date
    Apr 2003
    Posts
    7

    Ok, here goes...

    ok, after i tried to print out the reuslt of query, resultset, I got the following info.

    SELECT UserName FROM LogIn WHERE UserId = ''AND Password = ''

    sun.jdbc.odbc.JdbcOdbcResultSet@68cd79

    null


    So actually I didn't pass the UserId and Password to the query from what i got here. But how?

    Again, thank you very much for helping me with patience. really appreciate it

  11. #11
    Join Date
    Mar 2003
    Posts
    86

    UserInfoPD

    Show me the UserInfoPD implementation.

  12. #12
    Join Date
    Apr 2003
    Posts
    7
    Code:
    import java.util.*;
    import java.sql.*;
    
    public class UserInfoPD
    {
        private String userId;
        private String password;    
        private String userName;
        
        public UserInfoPD()
        {
            this.userId = userId;
            this.password = password;
            this.userName = userName;
        }
        
        public UserInfoPD(String newUserID)
        {
            setUserID(newUserID);
        }
        //constructor
        public UserInfoPD(String newUserID, String newPassword)
        {
            setUserID(newUserID);
            setPassword(newPassword);
        }
        
        public UserInfoPD(String newUserID, String newPassword, String newUserName)
        {
            setUserID(newUserID);
            setPassword(newPassword);
            setUserName(newUserName);
        }
        
        public String getUserID()
        {
            return userId;
        }
        
        public String getPassword()
        {
            return password;
        }
        
        public String getUserName()
        {
            return userName;
        }
        
        public void setUserID(String newUserID)
        {
            userId = newUserID;
        }
        
        public void setPassword(String newPassword)
        {
            password = newPassword;
        }
        
        public void setUserName(String newUserName)
        {
            userName = newUserName;
        }
    
        public String retrieveUserName() throws ClassNotFoundException, SQLException//method retrieves user name
        {
            UserInfoDA.connect();
            return UserInfoDA.retrieveUserName(this);
        }
    }
    
    class UserInfoSet
    {
         private Vector user;
         private int currentUserIndex;
    
         /**
          Gets the user data and puts it into the user Vector.
          This "simulates" database access.
        */
        public UserInfoSet()
        {
           user = new Vector();        
           currentUserIndex = 0;
        }
        
        /**
          Uses the input Vector to initialize user data.
          @param   user   Vector of initial user data
        */
        public UserInfoSet(Vector user)
        {
           this.user = user;
           currentUserIndex = 0;
        }
    
        /**
          Open DB connection.
          @exception ClassNotFoundException if jdbc driver not found
          @exception SQLException if a database access error occurs
        */
        public void connectDB() throws ClassNotFoundException, SQLException
        {
           UserInfoDA.connect();
        }
    
        /**
           Closes DB connection.
           @exception SQLException if a database access error occurs
        */
        public void disconnectDB() throws SQLException
        {
           UserInfoDA.disconnect();
        }
    
        /**
           Gets the person data from DB and puts it into the person Vector.
           @exception SQLException if a database access error occurs
        */
        public void retrieveDB() throws SQLException
        {
           user = UserInfoDA.retrieve();
           currentUserIndex = 0;
        }
    
        /**
          Gets the person data Vector
          @return  Vector where each element contains data for a person
        */
        public Vector getUser()
        {        
            return user;
        }
    
        /**
           Gets the index of the current person
           @return  Index of the current person
        */
        public int getCurrentUserIndex()
        {
           return currentUserIndex;
        }
    
        /**
           Returns the current person
           @return  current person
        */
        public UserInfoPD currentUser() 
        {
           return (UserInfoPD) user.elementAt(currentUserIndex);
        }
    
        /**
           Returns the first user
           @return  current person
        */
        public UserInfoPD firstUser()
        {
           currentUserIndex = 0;
           return (UserInfoPD) user.elementAt(currentUserIndex);
        }
        
        /**
           Returns the next user
           @return  current person
        */
        public UserInfoPD nextUser()throws ArrayIndexOutOfBoundsException
        {       
           return (UserInfoPD) user.elementAt(++currentUserIndex);
        }    
    }
    the following is part of Login GUI that compares retrieved UserID and Password.
    Code:
    UserInfoPD u = (UserInfoPD) userSet.currentUser();
            
            String id =  messageField1.getText();        
            String password = messageField2.getText(); 
            boolean switchUser = true;        
            try
            {
                while(switchUser)
                {
                    if(id.equals(u.getUserID()) && password.equals(u.getPassword()))
                    {   
                        JOptionPane.showMessageDialog(this, "Welcome to Clock In/Out system");
                        testName tN = new testName();this is the GUI that should display USER NAME but for now returns nothing
                        tN.setVisible(true);
                        this.setVisible(false);
                        switchUser = false;
                    }                
                    else
                    {
                        u = (UserInfoPD)userSet.nextUser();
                    }
                }
            }
            catch(ArrayIndexOutOfBoundsException ae)       
            {
                JOptionPane.showMessageDialog(this, "Invalid ID/Password");
                clearMessage();       
                u = (UserInfoPD)userSet.firstUser();
            }
    thank you for your help.
    Do you think the problem might be becasue I compare my user input ID and Password with data in DB only in the LogIn GUI i post above and not pass the input ID and Password to PD class?

  13. #13
    Join Date
    Mar 2003
    Posts
    86

    Code

    Can you send me the whole code? So I could have a look.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center