在click上滚动数据库 - java Derby

时间:2013-04-23 18:07:45

标签: java database derby

我正在尝试创建一个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);  

}

当我运行应用程序并单击按钮一次时,它会显示下一条记录。但是,如果我再次单击,应用程序崩溃。我想多次单击按钮时滚动整个数据库。我是否必须在此方法中再次声明Connection con和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()指向下一条记录?

1 个答案:

答案 0 :(得分: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变量是 local 变量,因此您只初始化局部变量,而不是类字段。这个局部变量很可能会影响一个类字段,这就是你遇到问题的原因。

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

// ResultSet rs = pstmt.executeQuery();  // ****** here 
rs = pstmt.executeQuery();  // ****** Note the difference