如何修复列索引超出范围SQLException

时间:2021-05-07 16:40:57

When i'm passing a query i'm getting below error.

当我通过查询时,我遇到了错误。

 Errorno is Nil
 Error String Is Query Problem.....
 java.sql.SQLException: Column Index out of range, 2 > 1.

This is the code in my java method.

这是我的java方法中的代码。

PreparedStatement pstm=con.prepareStatement("select period from stu_attendancemaster where classid=? and absentdt>=? and absentdt<=?");
pstm.setInt(1,classid);
pstm.setDate(2,fromdt);
pstm.setDate(3,todt);
System.out.println("qry for prd "+pstm.toString());
rs=pstm.executeQuery();
System.out.println("after qry for prd "+pstm.toString());

if(rs.next())   {
    stame = new Stu_AttendanceMasterEntity(rs.getInt(1), rs.getDate(2), rs.getInt(3), rs.getString(4), rs.getInt(5), rs.getString(6), rs.getTimestamp(7), rs.getString(8), rs.getTimestamp(9),rs.getString(10),rs.getInt(11),rs.getString(12));
}   else    {
    flag=false;
    errorstring=FN + P1 +" Class Name: " + Dep_ClassMasterDB.getClassname(classid) +" From Date: " +DateUtility.displayDate(fromdt,0) +" To Date: " +DateUtility.displayDate(todt,0) +N + V +DNE;
}
}   catch(Exception e)  {
    flag=false;
    errorstring="Query Problem..... "+e;

3 个解决方案

#1


10  

Error is in this statement:

错误在此声明中:

PreparedStatement pstm=con.prepareStatement("select period from stu_attendancemaster where classid=? and absentdt>=? and absentdt<=?");

you have to select all the 12 fields in your select query.

您必须在选择查询中选择所有12个字段。

Ex: (I am assuming that you have 12 fields in your table stu_attendancemaster) Do this:

例如:(我假设你的桌子中有12个字段stu_attendancemaster)这样做:

    PreparedStatement pstm=con.prepareStatement("select * from stu_attendancemaster where classid=? and absentdt>=? and absentdt<=?");

if not you can modify your query statement like this

如果没有,你可以像这样修改你的查询语句

select `colName1`, `colName2`, `colName3`, `colName4`, `colName5`, `colName6`, `colName7`, `colName8`, `colName9`, `colName10`, `colName11`, `colName12`,  from stu_attendancemaster where classid=? and absentdt>=? and absentdt<=?

Note : colName* should be your actual column name in the table.

注意:colName *应该是表中的实际列名。

EDIT : In case if you need only period from the query: just have rs.getInt(1) and remove rs.getInt(2) to rs.getInt(12)

编辑:如果您只需要查询句点:只需要rs.getInt(1)并删除rs.getInt(2)到rs.getInt(12)

Rule of thumb is : the number of column in the select clause and ResultSet.getXXX() should be same.

经验法则是:select子句中的列数和ResultSet.getXXX()应该相同。

#2


6  

You select one column in your statement and then access more than one column in your ResultSet. To fix your problem, you have to select from your database what you later want to read from the ResultSet.

在语句中选择一列,然后在ResultSet中访问多个列。要解决您的问题,您必须从数据库中选择以后要从ResultSet中读取的内容。

#3


1  

select statement is:

select语句是:

("select period from stu_attendancemaster where classid=? and absentdt>=? and absentdt<=?");

however you are getting more fields than period in your resultSet

但是你在resultSet中获得的字段多于句点

rs.getInt(1), rs.getDate(2), rs.getInt(3), rs.getString(4), rs.getInt(5), rs.getString(6), rs.getTimestamp(7), rs.getString(8), rs.getTimestamp(9),rs.getString(10),rs.getInt(11),rs.getString(12));

you cannot get these data from the resultset when you are just selecting period.

当您只选择句点时,无法从结果集中获取这些数据。

#1


10  

Error is in this statement:

错误在此声明中:

PreparedStatement pstm=con.prepareStatement("select period from stu_attendancemaster where classid=? and absentdt>=? and absentdt<=?");

you have to select all the 12 fields in your select query.

您必须在选择查询中选择所有12个字段。

Ex: (I am assuming that you have 12 fields in your table stu_attendancemaster) Do this:

例如:(我假设你的桌子中有12个字段stu_attendancemaster)这样做:

    PreparedStatement pstm=con.prepareStatement("select * from stu_attendancemaster where classid=? and absentdt>=? and absentdt<=?");

if not you can modify your query statement like this

如果没有,你可以像这样修改你的查询语句

select `colName1`, `colName2`, `colName3`, `colName4`, `colName5`, `colName6`, `colName7`, `colName8`, `colName9`, `colName10`, `colName11`, `colName12`,  from stu_attendancemaster where classid=? and absentdt>=? and absentdt<=?

Note : colName* should be your actual column name in the table.

注意:colName *应该是表中的实际列名。

EDIT : In case if you need only period from the query: just have rs.getInt(1) and remove rs.getInt(2) to rs.getInt(12)

编辑:如果您只需要查询句点:只需要rs.getInt(1)并删除rs.getInt(2)到rs.getInt(12)

Rule of thumb is : the number of column in the select clause and ResultSet.getXXX() should be same.

经验法则是:select子句中的列数和ResultSet.getXXX()应该相同。

#2


6  

You select one column in your statement and then access more than one column in your ResultSet. To fix your problem, you have to select from your database what you later want to read from the ResultSet.

在语句中选择一列,然后在ResultSet中访问多个列。要解决您的问题,您必须从数据库中选择以后要从ResultSet中读取的内容。

#3


1  

select statement is:

select语句是:

("select period from stu_attendancemaster where classid=? and absentdt>=? and absentdt<=?");

however you are getting more fields than period in your resultSet

但是你在resultSet中获得的字段多于句点

rs.getInt(1), rs.getDate(2), rs.getInt(3), rs.getString(4), rs.getInt(5), rs.getString(6), rs.getTimestamp(7), rs.getString(8), rs.getTimestamp(9),rs.getString(10),rs.getInt(11),rs.getString(12));

you cannot get these data from the resultset when you are just selecting period.

当您只选择句点时,无法从结果集中获取这些数据。