java - SQL 错误 :Column Index Out of Range, 2>1

标签 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.");
    }

如果我的新条目表中有一个条目,它可以正常工作,并且也会在我的仪表板中发出通知。 一旦我在表中输入第二个条目,它就会抛出一个 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++));

这是我的第一个项目,我正在学习。提前致谢。

最佳答案

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

当您在表中输入第二行时,第 79 行之前的 while 循环会迭代两次并尝试获取 rss786.getString(2) - 这意味着从不存在的查询中获取列 2,因此“列索引超出范围,2 > 1”。

希望您现在已经清楚用法了!

关于java - SQL 错误 :Column Index Out of Range, 2>1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38818035/

相关文章:

mysql - 来自 MySQL 的不一致查询结果

Java:在静态上下文中调用 repaint() (或如何避免它)

MySql - 根据偏好左连接但没有重复项

php - MySQL 基础问题 : how to get rows from one table that are not in another table?

mysql - 尝试使用外键创建表时出错

sql - Informix 到 PostgreSQL - 事务函数

java - 安全登录,不允许用户使用 URL 进行导航

javax.xml.bind.JAXBException : Class *** nor any of its super class is known to this context

java - 单点登录 Gmail

sql - UNIX_TIMESTAMP() 的 ANSI 标准