当用户登录时,登录框关闭并打开另一个框架,我需要用户登录详细信息以获取其他详细信息。我的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。 –