2017-08-13 74 views
0

當用戶登錄時,登錄框關閉並打開另一個框架,我需要用戶登錄詳細信息以獲取其他詳細信息。我的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(); 
       } 
      } 
     }); 
+0

您的登錄框架是否創建並顯示其他框架?或者他們都是從其他班級創建的? –

+0

當用戶登錄時,登錄框關閉並打開另一個框架,其中有一個按鈕,我需要用戶的用戶名 –

+0

當用戶登錄時,將用戶名作爲屬性傳遞給第二個框架。無論是通過構造函數還是通過setter。 –

回答

0

按照MVC -pattern。

創建一個模型層,這是一些愚蠢的DTO類,增強了一些基礎架構,以便在模型更改時註冊Listener。 在那裏創建一個用戶名/密碼的商店。

將模型作爲依賴項傳遞給任何GUI類,以便所有工作都針對同一個實例。 (請勿使用 Singelton模式 訪問模型。)

+0

是什麼?我還是一個初學者,你可以解釋更多細節 –

+0

我認爲維基百科是一個更長的解釋更好的資源:https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller –

相關問題