0

i made a program that have a database.. Here is my database looked now:

enter image description here

I already could retrieve the Username with the User Type when user login with their Username. But i have a problem now, the program run properly, the only thing is the program show all of the Username and User Type even though i login with Username: Fuhans.

What i want is the program only show the Logged In Username (e.g: i login with Username: Fuhans and the program show the message box where it display Username (Fuhans) and the User Type of that Username only (Administrator), not all of the Username at the database.

How do i solve this?

Here is my code: (Written using Java)

private void GetUsername(Connection conn, Statement state, ResultSet result)
    {
        try
        {
            String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
            Class.forName(driver);

            String url = "jdbc:odbc:Database";

            conn = DriverManager.getConnection(url);

            state = conn.createStatement();

            String query = "select Username, UserType from Member";

            result = state.executeQuery(query);

            while(result.next())
            {
                user = result.getString("Username");
                userType = result.getString("UserType");

                _userInformation.setUser(user);
                _userInformation.setUserType(userType);

                _sound.PlaySound(1);
                _infoBox.ShowMessageBox("Welcome: " + _userInformation.getUser() + " - " + _userInformation.getUserType() + " ! ", "Welcome" , 1);
            }
        } 

        catch (Exception e) 
        {
            System.err.println(e.getMessage());

            _sound.PlaySound(2);

            _infoBox.ShowMessageBox(e.getMessage(), "Error", 2);

            _reminder = new Reminder(1);

            JOptionPane.showOptionDialog(null, "Program will be closed due to error!", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);

            System.exit(0);
        }
    }

Very big thanks!

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Kaoru
  • 2,853
  • 14
  • 34
  • 68

2 Answers2

1

Change this query:

select Username, UserType from Member

To

select Username, UserType from Member where Username=? and UserType=?

And and pass corresponding parameter to ?. Basically you are getting all records with no filtering.

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
  • and how do i do that sir? I am new at this matter.. I already changed to the way you suggest, but it gave me the error COUNT field incorrect – Kaoru Nov 18 '13 at 09:24
  • You need to use PreparedStatement to do this, please refer in Google you will get good examples, I have left this as an exercise for you. :) – Pradeep Simha Nov 18 '13 at 09:26
0

You need a where clause to reach out to the logged in member.

Pierre Arlaud
  • 4,040
  • 3
  • 28
  • 42