0

我正在尝试创建一个 Java Derby 数据库,该数据库必须在单击按钮时显示下一条记录。我正在关注的教程是:
homeandlearn.co.uk/java/database_scrolling_buttons.html
在按钮的方法中,我有以下代码:

    String host = "jdbc:derby://localhost:1527/Employees";
    String uName = "bjorn";
    String uPass= "";
    con = DriverManager.getConnection( host, uName, uPass );
    PreparedStatement pstmt = con.prepareStatement("SELECT * FROM  App.Workers",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

    ResultSet rs = pstmt.executeQuery();
    rs.next();
if(rs.next()){

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

    textID.setText(id);
    textFirstName.setText(first_name);
    textLastName.setText(last_name);
    textJobTitle.setText(job);  

}

当我运行应用程序并单击一次按钮时,它会显示下一条记录。但是,如果我再次单击,应用程序将崩溃。多次单击该按钮时,我想滚动浏览整个数据库。我是否必须在此方法中再次声明 Connectioncon和 ResultSet rs?当我不这样做时,代码变为:

if(rs.next()){

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

    textID.setText(id);
    textFirstName.setText(first_name);
    textLastName.setText(last_name);
    textJobTitle.setText(job);  

}

现在我收到错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Employees.Workers.btnNextActionPerformed(Workers.java:255)
    at Employees.Workers.access$200(Workers.java:19)
    at Employees.Workers$3.actionPerformed(Workers.java:145)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

第 255 行指向if(rs.next()){并显示错误,因为rs指向一个空对象。如何让rs指向数据库中的上一条记录,以便 rs.next() 指向下一条记录?

4

1 回答 1

0

你知道什么时候在这里声明和初始化 rs 变量:

String host = "jdbc:derby://localhost:1527/Employees";
String uName = "bjorn";
String uPass= "";
con = DriverManager.getConnection( host, uName, uPass );
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM  App.Workers",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

ResultSet rs = pstmt.executeQuery();  // ****** here 
rs.next();

您的 rs 变量是一个局部变量,因此您只初始化一个局部变量,而不是一个类字段。这个局部变量很可能会隐藏类字段,这就是您遇到问题的原因。

如果是这样,解决方案不是重新声明 rs 变量,而是初始化 rs 类字段,而不是局部变量:

// ResultSet rs = pstmt.executeQuery();  // ****** here 
rs = pstmt.executeQuery();  // ****** Note the difference
于 2013-04-23T18:38:27.137 回答