问题
i made a program that have a database.. Here is my database looked now:
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!
回答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.
回答2:
You need a where clause to reach out to the logged in member.
来源:https://stackoverflow.com/questions/20044002/get-the-username-and-user-type-in-the-database-to-the-program-java