将字符串的值保存到另一个类并检索它

时间:2014-09-22 22:44:51

标签: java string

我目前正在尝试将字符串的值保存到JAVA中另一个名为Memory.JAVA的类文件中。我这样做的原因是因为变量不会保存在Try-Catch Blocks之外。因此,我使用以下代码在try catch块中启动类:

Memory mem = new Memory();

然后当我想保存字符串时,我使用以下内容:

 mem.brother1ID = "Whatever";

我没有将它创建为新String的原因是因为在Memory类中,我已经启动了这个字符串。为了测试这已被保存,我使用System.out.println打印出结果,在这种情况下是“Whatever”但是当我尝试在同一个类中打印出相同的结果时,我得到了结果“空值”。有没有人对我的问题有任何建议?请随时在下面发表评论。谢谢!

更新:

下面发布了一些代码:

private void searchFieldKeyReleased(java.awt.event.KeyEvent evt) {                                        
    try {

        Memory mem = new Memory();

        String sql = "select * from userInfo where firstName= ? OR lastname = ?";
        pst=conn.prepareStatement(sql);
        pst.setString(1, searchField.getText());
        pst.setString(2, searchField.getText());

        rs=pst.executeQuery();

        if(rs.next()) {
            String firstName = rs.getString("firstName");
            String lastName = rs.getString("lastName");
            String placeOfResidence = rs.getString("placeOfResidence");
            String employmentStatus = rs.getString("employmentStatus");
            String currentEmployer = rs.getString("currentEmployer");
            String taxStatus = rs.getString("taxStatus");
            String dateOfBirth = rs.getString("dateOfBirth");
            String mother = rs.getString("mother");
            String father = rs.getString("father");
            String brother1 = rs.getString("brother1");
            String brother2 = rs.getString("brother2");
            String brother3 = rs.getString("brother3");
            String brother4 = rs.getString("brother4");
            String brother5 = rs.getString("brother5");
            String sister1 = rs.getString("sister1");
            String sister2 = rs.getString("sister2");
            String sister3 = rs.getString("sister3");
            String sister4 = rs.getString("sister4");
            String sister5 = rs.getString("sister5");
            mem.brother1ID = rs.getString("brother1ID");
            mem.brother2ID = rs.getString("brother2ID");
            mem.brother3ID = rs.getString("brother3ID");
            mem.brother4ID = rs.getString("brother4ID");
            mem.brother5ID = rs.getString("brother5ID");
            mem.sister1ID = rs.getString("sister1ID");
            mem.sister2ID = rs.getString("sister2ID");
            mem.sister3ID = rs.getString("sister3ID");
            mem.sister4ID = rs.getString("sister4ID");
            mem.sister5ID = rs.getString("sister5ID");
            mem.fatherID = rs.getString("fatherID");
            mem.motherID = rs.getString("motherID");
            System.out.println(mem.brother1ID);
            System.out.println(firstName + " " + lastName);
            firstNameField.setText(firstName);
            lastNameField.setText(lastName);
            placeOfResidenceField.setText(placeOfResidence);
            employmentStatusField.setText(employmentStatus);
            currentEmployerField.setText(currentEmployer);
            taxStatusField.setText(taxStatus);
            dateOfBirthField.setText(dateOfBirth);
            motherField.setText(mother);
            fatherField.setText(father);
            brothersField1.setText(brother1);
            brothersField2.setText(brother2);
            brothersField3.setText(brother3);
            brothersField4.setText(brother4);
            brothersField5.setText(brother5);
            sisterField1.setText(sister1);
            sisterField2.setText(sister2);
            sisterField3.setText(sister3);
            sisterField4.setText(sister4);
            sisterField5.setText(sister5);



        }

    } catch(Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}    

这是当我尝试获得与以前System.out.println相同的结果时:

private void brotherViewButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                                   
    try {

        Memory mem = new Memory();

        String sql = "select * from userInfo where id=?";
        pst=conn.prepareStatement(sql);
        String IDNO = mem.brother1ID;
        System.out.println(IDNO);
        pst.setString(1, IDNO);

        rs=pst.executeQuery();

        if(rs.next()) {
            String firstName = rs.getString("firstName");
            String lastName = rs.getString("lastName");
            String placeOfResidence = rs.getString("placeOfResidence");
            String employmentStatus = rs.getString("employmentStatus");
            String currentEmployer = rs.getString("currentEmployer");
            String taxStatus = rs.getString("taxStatus");
            String dateOfBirth = rs.getString("dateOfBirth");
            String mother = rs.getString("mother");
            String father = rs.getString("father");
            String brother1 = rs.getString("brother1");
            String brother2 = rs.getString("brother2");
            String brother3 = rs.getString("brother3");
            String brother4 = rs.getString("brother4");
            String brother5 = rs.getString("brother5");
            String sister1 = rs.getString("sister1");
            String sister2 = rs.getString("sister2");
            String sister3 = rs.getString("sister3");
            String sister4 = rs.getString("sister4");
            String sister5 = rs.getString("sister5");
            System.out.println(firstName + " " + lastName);
            firstNameField.setText(firstName);
            lastNameField.setText(lastName);
            placeOfResidenceField.setText(placeOfResidence);
            employmentStatusField.setText(employmentStatus);
            currentEmployerField.setText(currentEmployer);
            taxStatusField.setText(taxStatus);
            dateOfBirthField.setText(dateOfBirth);
            motherField.setText(mother);
            fatherField.setText(father);
            brothersField1.setText(brother1);
            brothersField2.setText(brother2);
            brothersField3.setText(brother3);
            brothersField4.setText(brother4);
            brothersField5.setText(brother5);
            sisterField1.setText(sister1);
            sisterField2.setText(sister2);
            sisterField3.setText(sister3);
            sisterField4.setText(sister4);
            sisterField5.setText(sister5);



        }

    } catch(Exception e) {
        JOptionPane.showMessageDialog(null, e);
    } 
}     

2 个答案:

答案 0 :(得分:1)

brother1ID对象中Memory的值为null的原因是因为您正在使用该对象的新实例

我强烈建议您阅读有关如何在Java中确定对象实例的范围,以及创建 new 实例的意义,而不是使用现有实例。

例如,执行此操作时:

Memory mem = new Memory();
mem.brother1ID = "1234";

mem = new Memory();
System.out.println(mem.brother1ID);

打印的值为null。这是因为您正在使用该类的新实例。如果您想在多个方法调用中保持值,最好的办法是将Memory对象保存为包含您已显示的方法的任何类的实例变量。即:

private Memory memory = new Memory();

...

private void searchFieldKeyReleased(java.awt.event.KeyEvent evt) {
    // Use 'this.memory'
    this.memory.brother1ID = "1234";
    //(or)
    System.out.println(this.memory.brother1ID);
}

private void brotherViewButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // Use 'this.memory'
}

答案 1 :(得分:0)

请看这里

try {

    Memory mem = new Memory();

    String sql = "select * from userInfo where id=?";
    pst=conn.prepareStatement(sql);
    String IDNO = mem.brother1ID;
    System.out.println(IDNO);

您正在创建内存对象而不设置任何值。 然后你试图打印将为null的brotherId,所有的String变量都将被初始化为null,这就是为什么你得到' null'。你可以做的是将打印语句放在底部的try catch块之外,如果你的数据库中有任何记录,它将打印出最后的记录brotherId。