Java 实际上,如何使resultset不等于NULL?

Java 实际上,如何使resultset不等于NULL?,java,database,forms,swing,events,Java,Database,Forms,Swing,Events,参考我之前的一个问题: 关于我对上述问题的回答(见本问题下段) 我想知道——当有人说您是‘隐藏变量’时——这在实践中究竟意味着什么:它是指设置为私有函数的netbeans生成的代码事件存根吗?如果是这样的话,你怎么把它改成public呢?因为我已经尝试过了,而Netbeans不让我这么做 这是我的代码:�� public void DoConnect( ) { try{ String host = "jdbc:derby://localhost:1527/Em

参考我之前的一个问题:

关于我对上述问题的回答(见本问题下段)

我想知道——当有人说您是‘隐藏变量’时——这在实践中究竟意味着什么:它是指设置为私有函数的netbeans生成的代码事件存根吗?如果是这样的话,你怎么把它改成public呢?因为我已经尝试过了,而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先生在早些时候善意地回答了我的问题,他说:

“您的问题是您的结果集为空

您正在隐藏变量rs

您的代码(基本上)是:

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

为了更快地得到答案,请尽量减少代码,缩短示例,集中精力解决实际问题。”

我说

“谢谢你的回答。你能具体地告诉我吗��如何取消rs对象的空值,请“

对于这件事,取消隐藏我的变量。同时也将私人活动存根公开


请帮忙

在以下代码中:

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

在以下代码中:

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

实际上,我从未见过空的结果集,除非我不是从Statement.executeQuery()获得它。对我来说,这听起来像是一个小的编程错误。实际上,我从未见过空的结果集,除非我不是从Statement.executeQuery()获得它。听起来像是一个小的编程错误。谢谢@JB。又好又清楚。我明白了,现在我的程序运行了。应该更好地了解基本程序错误。哇,我知道你的声望很高。谢谢@JB。又好又清楚。我明白了,现在我的程序运行了。应该更好地了解基本程序错误。哇,我知道你有很高的声誉。