将数据库中的用户名和用户类型获取到程序Java [英] Get the username and user type in the database to the program Java
问题描述
我制作了一个具有数据库的程序.这是我现在的数据库:
i made a program that have a database.. Here is my database looked now:
使用用户名登录时,我已经可以使用用户类型检索用户名.但是我现在有一个问题,程序可以正常运行,唯一的办法是即使我使用用户名:Fuhans登录,该程序也可以显示所有用户名和用户类型.
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.
我想要的是该程序仅显示已登录的用户名"(例如:我以用户名"登录:Fuhans,该程序显示消息框,其中仅显示用户名"(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.
我该如何解决?
这是我的代码:(使用Java编写)
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);
}
}
非常感谢!
推荐答案
更改此查询:
select Username, UserType from Member
收件人
select Username, UserType from Member where Username=? and UserType=?
然后将相应的参数传递给?
.基本上,您获取的所有记录都没有过滤.
And and pass corresponding parameter to ?
. Basically you are getting all records with no filtering.
这篇关于将数据库中的用户名和用户类型获取到程序Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!