2013-01-12 209 views
0

我已經拋棄了舊的問題,因爲它並不清楚,並且出現了一堆錯誤,我發現它更容易「我認爲」解決方案,儘管沒有完全正常工作。 修訂:::::::: LognL類變量不在類之間傳遞

public final class LogInL extends JFrame { 
Connection conn = null; 
ResultSet rs = null; 
PreparedStatement pst = null; 
String Username; 
String ID; 
public LogInL() { 
    initComponents(); 
    ButtonGroup();   
} 

private void backBActionPerformed(ActionEvent e) { 
    LoginMain login = new LoginMain(); 
    login.setVisible(true); 
    this.dispose(); 
} 

private void LoginBActionPerformed(ActionEvent e) { 

    if(prosecutorCB.isSelected()) 
    { 
     try { 
      conn = SQLConnect.ConnectDb(); 
      String sql = "SELECT prosecutors.username, criminalrecords.ID FROM prosecutors, criminalrecords " 
         + "WHERE username = ? and ID = ?"; 
      pst = conn.prepareStatement(sql); 
      pst.setString(1, usernameF.getText()); 
      pst.setString(2, criminalIdF.getText()); 

      Username = usernameF.getText(); 
      System.out.println("Username is " + Username); 

      ID = criminalIdF.getText(); 
      System.out.println("Criminal ID is " + ID); 
      rs = pst.executeQuery(); 
      if(rs.next()) 
      { 
       ProsecutorMain Frame = new ProsecutorMain(); // call Policemain class //display it 
       Frame.pack(); 
       Frame.setVisible(true); // make it visible 
       System.out.println("Welcome to prosecutors"); 
       this.dispose(); 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null,"<html>wrong username or criminal ID<br>" 
       + "Criminal may not longer be in database"); 
      } 
     } 
     catch (SQLException ex) 
     { 
      Logger.getLogger(LogInL.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    }//end if proseutor 


}//end logination 

private void ButtonGroup() 
{ 
    ButtonGroup bg = new ButtonGroup(); 
    bg.add(prosecutorCB); 
    bg.add(criminaldefenceCB); 
} 

修訂:::::::: ProsecutorMain類

public class ProsecutorMain extends JFrame { 
Connection conn = null; 
ResultSet rs = null; 
PreparedStatement pst = null; 
LogInL id = new LogInL(); 
String UserName; 
public ProsecutorMain() throws SQLException 
{ 
    initComponents(); 
    UserName = id.Username; 
    username.setText(UserName); 

    firstname.setText("blablabla"); 
    incidentlocation.setText("kupa"); 
    System.out.println(UserName); 


} 
private void logOutActionPerformed(ActionEvent e) { 

     int response = JOptionPane.showConfirmDialog(null, "<html> Are you sure you want to log out?", 
       "Confirm",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 
     if (response == JOptionPane.NO_OPTION) 
     { 

     } 
     if (response == JOptionPane.YES_OPTION) 
     { 
      this.dispose(); 
      LogInL login = new LogInL(); 
      login.setVisible(true); 
      JOptionPane.showMessageDialog(null,"You have been sucessfully logged out"); 
     }  
    } 

變量的問題是用戶名

+0

什麼問題? –

+0

我想幫助我重新組織代碼,我必須履行我的要求 –

回答

0

可變UserNamenullProsecutorMain,因爲您正在創建一個LogInL的新實例,該實例沒有原始數據庫中的值(並顯示yed)那個JFrame的實例。

要解決,你就必須在做實例傳遞Username集:

public ProsecutorMain(LogInL id) { 
    this.id = id; 
    ... 
} 

,並在LogInL創建:

new ProsecutorMain(this); 

而不是創建一個新的ProsecutorMainJFrame當您從查詢中得到結果時,您可以創建一個自定義LawRecord與必要的usernameid字段。你可以通過這個ProsecutorMain

而不是在這裏使用2 JFrames,您可以使用模式JDialog接受搜索條件。

鏈接:How to Make Dialogs

+0

你好Reimeus你可以看看現在修訂的問題,謝謝。 –

+0

無法確定可能設置或可能未設置的內容。如果您的代碼不多,請發帖,否則請考慮發佈[SSCCE](http://sscce.org/) – Reimeus

+0

問題已經修改,並附有相關代碼 –