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");
        }       
    }

質問の変数はユーザー名です

4

1 に答える 1

0

変数UserNamenull、その元の(および表示された)インスタンスのデータベースからの値を持たないProsecutorMain新しいインスタンスを作成しているときに含まれます。LogInLJFrame

修正するには、次のように設定されているインスタンスを渡す必要があります。Username

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

で作成するにはLogInL

new ProsecutorMain(this);

ProsecutorMain JFrameクエリの結果を取得するときに新しいものを作成するのではなくLawRecord、必要なフィールドusernameidフィールドを使用してカスタムを作成できます。次に、これをに渡すことができProsecutorMainます。

ここで2を使用する代わりに、検索条件を受け入れるためJFramesにモーダルを使用できます。JDialog

リンク:ダイアログの作り方

于 2013-01-12T21:11:09.737 に答える