SQL错误:列索引超出范围,2> 1

时间:2016-08-07 20:02:50

标签: java mysql sql jdbc

我想在法律案件监控系统中发出任何即将发生的案件的通知。这是我的代码 -

try{
        Class.forName("com.mysql.jdbc.Driver");
        cn22= DriverManager.getConnection("jdbc:mysql://localhost:3306/mmtc?zeroDateTimeBehavior=convertToNull", "root", "");
        st22=cn22.createStatement();
        st23=cn22.createStatement();
        st24=cn22.createStatement();
        st25=cn22.createStatement();


    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, "Not Connected.");

    }
    DateFormat df=new SimpleDateFormat("M/d/yy");
    Date dateobj=new Date();
    String datetoday=df.format(dateobj);

    Date todaydate=datechanger(datetoday);
    DateTime dt1=new DateTime(todaydate);
    String fetchdate="Select nextdate from newentry where location='"+Login.locationofcurrentuser+"'";
    String fetchdatecount="select count(*) as total from newentry where location='"+Login.locationofcurrentuser+"'";
    ResultSet rsdash=st23.executeQuery(fetchdate);
    ResultSet rsdashcount=st24.executeQuery(fetchdatecount);
    String dategetter="Select caseno from newentry where location='"+Login.locationofcurrentuser+"'";
             ResultSet rss786=st25.executeQuery(dategetter);

    if(rsdashcount.next()){

      int size=rsdashcount.getInt("total");


    ArrayList<String> a=new ArrayList<String>();
     ArrayList<String> caseno=new ArrayList<String>();
     while(rss786.next()){
         int y=1;
         while(y<=size){
             caseno.add(rss786.getString(y++));

         }
     }


      ArrayList<Date> dates=new ArrayList<Date>();
      while(rsdash.next()){
          int z=1;
          while(z<=size){
              a.add(rsdash.getString(z++));
          }
      }


     for(int i=0;i<size;i++){
         dates.add(datechanger(a.get(i)));



         DateTime dt2=new DateTime(dates.get(i));
         int gap=Days.daysBetween(dt1,dt2).getDays();
         ArrayList<String> b=new ArrayList<String>();

         if (gap<4){



               b.add("Upcoming case-"+caseno.get(i)+" due on-"+a.get(i)+""); 
               System.out.println(b);








         }
         notification.setText(b.toString());


     }


    }else
    {
        notification.setText("No Upcoming Cases.");
    }

如果我在newentry表中有一个条目,它工作正常,并在我的仪表板中抛出通知。 一旦我在表中输入第二个条目就会抛出一个sql错误 -

java.sql.SQLException: Column Index out of range, 2 > 1. 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.ResultSetImpl.checkColumnBounds(ResultSetImpl.java:831)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5774)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5693)
at mmtc.Dashboard.<init>(Dashboard.java:79)

我的代码的第79行是 -

 caseno.add(rss786.getString(y++));

这是我的第一个项目,我正在学习。在此先感谢。

1 个答案:

答案 0 :(得分:0)

您不能拥有rss786.getString(y++)rsdash.getString(z++),而是可以拥有rss786.getString(1)rsdash.getString(1),因为您的相应查询只有一列适用于您的结果集。

当您在表格中输入第二行时,第79行之前的while循环迭代两次并尝试获取rss786.getString(2) - 这意味着从不存在的查询中获取第2列,因此&#39; Column Index out of范围,2> 1&#39;

希望您现在明确使用它!