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在其中,该实例不具有数据库中原始(和显示)实例的值。ProsecutorMainLogInLJFrame

要修复,您必须传入已设置的实例:Username

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

并创建LogInL

new ProsecutorMain(this);

ProsecutorMain JFrame当您从查询中获得结果时,您可以创建一个LawRecord包含必要字段username和字段的自定义,而不是创建一个新的id。然后,您可以将其传递给ProsecutorMain.

JFrames您可以使用模式JDialog来接受搜索条件,而不是在这里使用 2 。

链接:如何制作对话框

于 2013-01-12T21:11:09.737 回答