实际上,如何使结果集不等于NULL?

时间:2012-12-09 16:50:27

标签: java database forms swing events

参考我之前的一个问题:

Database scrolling buttons using java forms - Error when calling next method to move cursor up Resultset object

在参考答案时,我对上述问题有所了解(见本问题的最后部分)

我想知道 - 当它被称为'隐藏你的变量'时 - 这在实践中实际意味着什么:它是否参考了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对象的请求”!

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

请帮助!

1 个答案:

答案 0 :(得分: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(); 
    }
}