开发者

why it returns SQL exception?

开发者 https://www.devze.com 2022-12-14 01:34 出处:网络
I have a \"Manager\" class and also one frame which is\"Report\".In the frame I have three radio buttons which their texts are \"Years before 1970\",\"between 1970 and 2000\",\"years after 2000\" and

I have a "Manager" class and also one frame which is"Report" .In the frame I have three radio buttons which their texts are "Years before 1970","between 1970 and 2000","years after 2000" and if one of them is selected , in the text field below these radio buttons I will show the number of rows that have this situation which are in my SQL table.also in my SQL table I have created a table that has name, family, fatherName,motherName,dateOfBirth,placeOfBirth,Also the first column is ID which is created automatically .the text in dateOfBirth column is like"December 5, 2009" and I need just its year !!! but when i run this code these stack trace will be shown in the console,why?? please help me.

my Manager class(just a part of that):

 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();
}

my frame(just a part of that):

 pr开发者_JAVA技巧ivate 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)


The answer is in the stack trace! Look at line 1137 where you call getString(6). Now look at the error message: java.sql.SQLException: Column Index out of range, 6 > 1. now look at your query: "SELECT dateOfBirth from birthtable".

You are asking (in the query) for 1 value (dateOfBirth) and they you are trying to get it as the 6th value returned... so change the 6 to a.......... 1 and it should work.

You really do need to read the messages - they tell you a lot.

0

精彩评论

暂无评论...
验证码 换一张
取 消