當用戶登錄時,登錄框關閉並打開另一個框架,我需要用戶登錄詳細信息以獲取其他詳細信息。我的prepareStatement & resultSet已經設置在頂部。在Java中仍然是一個新手如何檢索用戶名值從登錄幀到另一個框架與Java?
登錄的動作監聽
btnLogin.addActionListener((ActionEvent ae) -> {
if (ae.getSource().equals(btnLogin)) {
DB_connection db_connection = new DB_connection();
String username = username_login_txtf.getText();
String password = password_login_txtf.getText();
try {
String sql = "SELECT * FROM user_profile as up WHERE up.username=? AND up.user_password=?";
preStatement = db_connection.connect().prepareStatement(sql);
preStatement.setString(1, username);
preStatement.setString(2, password);
res = preStatement.executeQuery();
boolean login = false;
while(res.next()) {
if(res.getString("username").equalsIgnoreCase(username) && res.getString("user_password").equalsIgnoreCase(password)) {
JOptionPane.showMessageDialog(loginPanel, "You have Logged in!");
login = true;
fr.dispose();
Main_menu.main(null);
break;
} ...
我的另一個框架,我需要檢索特定用戶登錄數據
btnSearch.addActionListener((ActionEvent ae) -> {
if (ae.getSource().equals(btnSearch)) {
// Right now i am getting the username value from another text field that I have set in this form but I want to retrieve that value directly form the login frame
String get_username = username_profile_txtf.getText();
DB_connection db_connection = new DB_connection();
try{
preStatement = db_connection.connect().prepareStatement("Select * from user_profile where username = ?");
// here I need the user login username from the login form
preStatement.setString(1, get_username);
res = preStatement.executeQuery();
if (res.next()){
String username = res.getString("username");
username_profile_txtf.setText(username);
...
}
preStatement.close();
res.close();
} catch(SQLException ex){
System.out.println(ex.getMessage());
} finally {
db_connection.disconnect();
}
}
});
您的登錄框架是否創建並顯示其他框架?或者他們都是從其他班級創建的? –
當用戶登錄時,登錄框關閉並打開另一個框架,其中有一個按鈕,我需要用戶的用戶名 –
當用戶登錄時,將用戶名作爲屬性傳遞給第二個框架。無論是通過構造函數還是通過setter。 –