ユーザがログインすると、ログイン・フレームが閉じて別のフレームが開き、他のフレームの詳細が表示されます。私の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;
} ...
私はMVC -patternをフォロー
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();
}
}
});
ログインフレームが作成され、他のフレームが表示されますか?あるいは、どちらも他のクラスから作られていますか? –
ユーザがログインすると、ログインフレームが閉じられ、別のフレームが開きます。ここには、ユーザのユーザ名が必要なボタンがあります。 –
ユーザがログインすると、2番目のフレームにユーザ名を属性として渡します。コンストラクタまたはセッタを介して。 –