給定一個登錄窗口,它需要來自用戶的2個輸入:用戶名&密碼。 成功識別後,用戶將被重定向到主窗口。 在這裏,我想顯示他或她的用戶名,但我收到空。JAVA - 在類之間傳遞值
這是我當前的代碼: 登錄類:
private void LoginUser(String username, String password)
{
user = username;
pass = password;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try
{
conn = MainEngine.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT username FROM users WHERE username ='"+user+"' AND password ='"+getMD5Hex(pass)+"'");
if(rs.next()) // Successful login
{
Window_AfterLogin window_afterlogin = new Window_AfterLogin();
window_afterlogin.setVisible(true);
this.dispose();
}
else
{
label_info.setText("Wrong credentials");
}
}
catch(Exception e)
{
System.err.println(e);
}
}
public String getUserName()
{
return user;
}
注:用戶並傳遞變量是全球性的。
Window_AfterLogin類:
public Window_AfterLogin() {
initComponents();
Window_Login window_login = new Window_Login();
System.out.println(window_login.getUserName());
}
*對不起,我在標題 – Unrealxqt
*您可以編輯您的問題,並更改標題的不必要的「中」字... –
哪裏是主窗口的代碼? – sForSujit