为什么它返回SQL异常?

时间:2009-12-17 04:39:00

标签: java sql

我有一个“经理”课程,还有一个框架是“报告”。在框架中我有三个单选按钮,其文本是“1970年之前”,“1970年至2000年之间”,“2000年之后”如果选中其中一个,在这些单选按钮下面的文本字段中,我将显示在我的SQL表中有这种情况的行数。在我的SQL表中,我创建了一个名称,族, fatherName,motherName,dateOfBirth,placeOfBirth,第一列也是自动创建的ID .dateOfBirth列中的文字就像“2009年12月5日”,我只需要它的年份!!!但是当我运行这段代码时,这些堆栈跟踪将显示在控制台中,为什么?请帮帮我。

我的经理类(只是其中的一部分):

 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 个答案:

答案 0 :(得分:3)

答案是在堆栈跟踪中!查看第1137行,您可以在其中调用getString(6)。现在看一下错误信息:java.sql.SQLException:Column Index超出范围,6&gt; 1.现在查看您的查询:“从出生地选择dateOfBirth”。

你问(在查询中)1值(dateOfBirth)并且他们试图将它作为返回的第6个值...所以将6更改为.......... 1它应该工作。

你确实需要阅读这些消息 - 他们会告诉你很多。