2009-12-17 81 views
-2

我有一個「經理」類,也有一個框架是「報告」。在框架中我有三個單選按鈕,其文本是「1970年以前」,「1970年至2000年」,「2000年以後」以及如果選擇其中一個,在這些單選按鈕下面的文本字段中,我將顯示具有這種情況的行數在我的SQL表中。同樣,在我的SQL表中,我創建了一個具有名稱,系列,父名,motherName,dateOfBirth,placeOfBirth,也是第一列是自動創建的ID。dateOfBirth列中的文本就像「2009年12月5日」,我只需要一年!但是當我運行這段代碼時,這些堆棧跟蹤將顯示在控制檯中,爲什麼?請幫幫我。爲什麼它返回SQL異常?

我Manager類(只是其中的一部分):

public static int getPercent(String i)throws SQLException{ 
    Statement stmt = conn.createStatement(); 
    List<String> list = null; 
    if(i.equals("O")){ 


     ResultSet rst = stmt.executeQuery("SELECT dateOfBirth from birthtable"); 
     while(rst.next()){ 
      String s1 = rst.getString(6) ; 
      if(rst.wasNull()){ 
       s1=null; 
      } 
      String s2 = s1.substring(s1.length()-4); 
      int s3 = Integer.parseInt(s2); 
      if(list ==null&& s3<1970){ 
       list = new ArrayList<String>(); 
      } 
      else{ 
       list = new ArrayList<String>(); 
       s2 = null; 
      } 
      list.add(s2); 
     } 


    } 
    if(i.equals("N")){ 

     ResultSet rst = stmt.executeQuery("SELECT dateOfBirth from birthtable"); 
     while(rst.next()){ 
      String s1 = rst.getString(6) ; 
      if(rst.wasNull()){ 
       s1=null; 
      } 
      String s2 = s1.substring(s1.length()-4); 
      int s3 = Integer.parseInt(s2); 
      if(list ==null&&s3>2000){ 
       list = new ArrayList<String>(); 
      } 
      else{ 
       list = new ArrayList<String>(0); 
       s2 = null; 
      } 
      list.add(s2); 
     } 
    } 
    if(i.equals("H")){ 
     ResultSet rst = stmt.executeQuery("SELECT dateOfBirth from birthtable"); 
     while(rst.next()){ 
      String s1 = rst.getString(6) ; 
      if(rst.wasNull()){ 
       s1=null; 
      } 
      String s2 = s1.substring(s1.length()-4); 
      int s3 = Integer.parseInt(s2); 
      if(list ==null&&s3>1970&& s3<2000){ 
       list = new ArrayList<String>(); 
      } 
      else{ 
       list = new ArrayList<String>(0); 
       s2 = null; 
      } 
      list.add(s2); 
     } 
    } 
    return list.size(); 
} 

我的幀(只是那部分):

private void range1ActionPerformed(java.awt.event.ActionEvent evt) { 
    int i = 0; 
    try { 
     i = Manager.getPercent("O"); 
    } catch (SQLException ex) { 
     Logger.getLogger(BirthReport.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    resulText.setText(i+""); 
} 

stacktarce:

 Dec 17, 2009 7:57:48 AM AdminGUI.BirthReport range1ActionPerformed 
    SEVERE: null 

java.sql.SQLException: Column Index out of range, 6 > 1. 
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) 
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) 
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926) 
     at com.mysql.jdbc.ResultSetImpl.checkColumnBounds(ResultSetImpl.java:792) 
     at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5529) 
     at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5448) 
     at database.Manager.getPercent(Manager.java:1137) 
     at AdminGUI.BirthReport.range1ActionPerformed(BirthReport.java:152) 
     at AdminGUI.BirthReport.access$100(BirthReport.java:23) 
     at AdminGUI.BirthReport$2.actionPerformed(BirthReport.java:84) 
     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) 
     at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) 
     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) 
     at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:291) 
     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) 
     at java.awt.Component.processMouseEvent(Component.java:6038) 
     at javax.swing.JComponent.processMouseEvent(JComponent.java:3265) 
     at java.awt.Component.processEvent(Component.java:5803) 
     at java.awt.Container.processEvent(Container.java:2058) 
     at java.awt.Component.dispatchEventImpl(Component.java:4410) 
     at java.awt.Container.dispatchEventImpl(Container.java:2116) 
     at java.awt.Component.dispatchEvent(Component.java:4240) 
     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322) 
     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986) 
     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916) 
     at java.awt.Container.dispatchEventImpl(Container.java:2102) 
     at java.awt.Window.dispatchEventImpl(Window.java:2429) 
     at java.awt.Component.dispatchEvent(Component.java:4240) 
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) 
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) 
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) 
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:177) 
     at java.awt.Dialog$1.run(Dialog.java:1039) 
     at java.awt.Dialog$3.run(Dialog.java:1091) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.awt.Dialog.show(Dialog.java:1089) 
     at java.awt.Component.show(Component.java:1419) 
     at java.awt.Component.setVisible(Component.java:1372) 
     at java.awt.Window.setVisible(Window.java:801) 
     at java.awt.Dialog.setVisible(Dialog.java:979) 
     at AdminGUI.BirthFrame.jButton1ActionPerformed(BirthFrame.java:190) 
     at AdminGUI.BirthFrame.access$300(BirthFrame.java:21) 
     at AdminGUI.BirthFrame$4.actionPerformed(BirthFrame.java:94) 
     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) 
     at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) 
     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) 
     at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) 
     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) 
     at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272) 
     at java.awt.Component.processMouseEvent(Component.java:6038) 
     at javax.swing.JComponent.processMouseEvent(JComponent.java:3265) 
     at java.awt.Component.processEvent(Component.java:5803) 
     at java.awt.Container.processEvent(Container.java:2058) 
     at java.awt.Component.dispatchEventImpl(Component.java:4410) 
     at java.awt.Container.dispatchEventImpl(Container.java:2116) 
     at java.awt.Component.dispatchEvent(Component.java:4240) 
     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322) 
     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986) 
     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916) 
     at java.awt.Container.dispatchEventImpl(Container.java:2102) 
     at java.awt.Window.dispatchEventImpl(Window.java:2429) 
     at java.awt.Component.dispatchEvent(Component.java:4240) 
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) 
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) 
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) 
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) 
     at java.awt.EventDispatchThread.run(EventDispatchThread.java:121) 
+1

管理員類1137行發生了什麼? – 2009-12-17 04:43:33

+0

我明白了,[:-)]但是當選擇「1970年前的年份」時,它將返回「1」(我只有一行),但是這一年是2006年! – Johanna 2009-12-17 04:57:03

+0

哦,我得到我的錯誤,我設置s2 = null; – Johanna 2009-12-17 05:03:01

回答

3

答案在堆棧跟蹤中!看看你調用getString(6)的第1137行。現在看看錯誤消息:java.sql.SQLException:列索引超出範圍,6> 1.現在看看你的查詢:「SELECT dateOfBirth from birthtable」。

你問(在查詢中)1值(dateOfBirth),他們試圖把它作爲第6個值返回......所以將6改爲.......... 1,它應該工作。

你真的需要閱讀信息 - 他們告訴你很多。

+0

謝謝,[:「>]。我修正了它,但是當選擇」1970年前的年份「時,它會返回」1「(我只有一行),但是這一年是2006 !! – Johanna 2009-12-17 04:55:43

+1

那麼你的代碼很糟糕說實話...我會告訴你如何讓它變得更好,但是由於錯誤而放棄了,一旦你得到一個工作版本在這裏發佈修改後的代碼(只需編輯你的答案並添加工作代碼)我會做的是使用調試器或添加一些System.out.println()語句,以便觀察代碼中發生了什麼,並以這種方式調試它。 – TofuBeer 2009-12-17 05:01:32

相關問題