我正在嘗試創建一個Java Derby數據庫,它必須在點擊一個按鈕時顯示下一條記錄。我下面的教程:
homeandlearn.co.uk/java/database_scrolling_buttons.html
在按鈕我有下面的代碼的方法:點擊滾動瀏覽數據庫--java Derby
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);
}
當我運行應用程序,並單擊按鈕一次,它顯示下一個記錄。但是,如果我再次點擊,應用程序崩潰。我想多次單擊按鈕時滾動瀏覽整個數據庫。我是否必須在此方法中再次聲明連接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()指向下一條記錄?
如果應用程序崩潰,你需要幫助,你*必須*顯示完整的錯誤信息,並指出哪些行導致異常發生。否則,你是在要求我們猜測和閱讀頭腦,這是我們不擅長的事情。 – 2013-04-23 18:08:50
當我省略上面的代碼行if(rs.next()){它顯示以下錯誤消息:線程「AWT-EventQueue-0」中的異常java.lang.NullPointerException \t at Employees.Workers.btnNextActionPerformed(Workers .java:255) \t at Employees.Workers.access $ 200(Workers.java:19) \t at Employees.Workers $ 3.actionPerformed(Workers.java:145)第255行指向行if(rs.next() ){因爲rs指向一個空對象。我該如何指向數據庫的以前記錄? – user1189952 2013-04-23 18:23:50
所以你正試圖調用一個null ResultSet變量的方法。解決方案:不要這樣做 - 不要在空變量上調用方法。更大的問題,爲什麼變量爲空,可能不是我們可以幫助的東西,除非你顯示更多的代碼。你需要回溯你的代碼,看看你在哪裏*不*初始化這個變量。 – 2013-04-23 18:37:30