0

参考我之前的一个问题:

使用java表单的数据库滚动按钮-调用下一个方法将光标向上移动结果集对象时出错

关于我必须回答上述问题的答案(请参阅此问题的底部段落)

我想知道 - 当有人说你是:'隐藏你的变量' - 这实际上意味着什么:它是否参考了 netbeans 生成的代码事件存根,它被设置为私有函数?如果是这样,您如何将其更改为公开,因为我已经尝试过,而 Netbeans 不会让我这样做!

这是我的代码:��

public void DoConnect( ) {

        try{
        String host = "jdbc:derby://localhost:1527/Employer";
        String uName = "admins";
        String uPass= "admins";


        Connection con = DriverManager.getConnection( host, uName, uPass );
//        Statement stmt = con.createStatement( );
        stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );

        String SQL = "SELECT * FROM WORKERS";
        ResultSet rs = stmt.executeQuery( SQL );

        rs.next( );
        int id_col = rs.getInt("ID");
        String id=Integer.toString(id_col);
        String first= rs.getString("First_Name");
        String last = rs.getString("Last_Name");
        String job = rs.getString("Job_Title");

        textID.setText(id);
        textFirstName.setText(first);
        textLastName.setText(last);
        textJobTitle.setText(job);

        }

        catch ( SQLException err ){
               System.out.println( err.getMessage( ) );

        }

    }

和 netbeans 生成的事件代码存根。我在其中放置了我的事件代码:

private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        try {





        if ( rs.next( )) {

               int id_col = rs.getInt("ID");
                String id = Integer.toString(id_col);
                String first = rs.getString("First_Name");
                String last = rs.getString("Last_Name");
                String job = rs.getString("Job_Title");

                textID.setText(id);
                textFirstName.setText(first);
                textLastName.setText(last);
               textJobTitle.setText(job);
            }
            else {
                rs.previous( );
                JOptionPane.showMessageDialog(Workers.this, "End of File");
            }
        }
        catch (SQLException err) {
            JOptionPane.showMessageDialog(Workers.this, err.getMessage());
            System.out.println( err.getMessage( ) );
        }
    }                                       


    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Workers().setVisible(true);
            }
        }
    }

@Uwe Plonus 很高兴地给了我更早的答案,他说了以下内容:

“您的问题是您的 ResultSet 为空。

你隐藏了你的变量 rs。

您的代码(基本上)是:

public class Sample {
    ResultSet rs; // is null
    public method() {
        ResultSet rs = stmt.execute(); // here the other rs is hidden
    }
}

如果你想让你的代码工作,不要隐藏变量。也不要隐藏连接。

为了更快地获得答案,请尝试减少您的代码并缩短示例并专注于您的实际问题。”

我说...

“感谢您的回答。您能否具体告诉我��如何取消 rs 对象为空”!

就此而言,取消隐藏我的变量。并将私人事件存根公开??

请帮忙!

4

1 回答 1

4

在以下代码中:

public class Sample {
    ResultSet rs; // is null
    public method() {
        ResultSet rs = stmt.execute(); // here the other rs is hidden
    }
}

你有两个变量:

  • 一个名为 的变量rs,在类中声明
  • rs在方法中声明的一个局部变量也称为

因此,当您rs在方法中分配某些内容时,您不会为该rs字段分配任何内容,因为它们是两个不同的变量。如果您打算初始化该字段,则代码应为

public class Sample {
    ResultSet rs;
    public method() {
        this.rs = stmt.execute(); // there is no declaration here, so rs is the field
                                  // and not a new, local variable
    }
}

或者干脆

public class Sample {
    ResultSet rs;
    public method() {
        rs = stmt.execute(); 
    }
}
于 2012-12-09T17:03:25.927 回答