指数3超出范围

时间:2022-04-19 16:40:57

I'm new to this forum and also new to SQL.

我是这个论坛的新手,也是SQL新手。

I've a problem with the below code of mine.

我的下面的代码有问题。

Its throwing an error of SQLException which is the index 3 is out of range

它抛出SQLException的错误,即索引3超出范围

I don't know what's the problem.

我不知道是什么问题。

public static ArrayList<LotInfoVO> getLotInfoRecords3(String studName,String DoubleCheck, String lngstudid) throws ClassNotFoundException {
        {
            ArrayList <LotInfoVO> lotRecords4 = new ArrayList<LotInfoVO>();
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                PreparedStatement stmt = getCon().prepareStatement("SELECT * FROM MesLib.tblSchoolRecordsr where strstudName = ? and strDoubleCheckCode = ? and (lngstudID NOT LIKE '-[^0-9]') ");
                stmt.setString(1, studName);
                stmt.setString(2, DoubleCheck);
                stmt.setString(3, lngstudID);

                ResultSet rs = stmt.executeQuery();
                while(rs.next()) {
                    LotInfoVO voLotInfo3 = new LotInfoVO();
                    voLotInfo3.setStstudName(rs.getString("strstudName"));
                    voLotInfo3.setLngStudID(rs.getString("lngStudID"));
                    voLotInfo3.setLngStudSubID(rs.getString("lngStudSubID"));
                    voLotInfo3.setStrDoubleCheckCode(rs.getString("strDoubleCheckCode"));
                    voLotInfo3.setStrPackage(rs.getString("strCourse"));
                    voLotInfo3.setStrDimension(rs.getString("strSchool"));
                    voLotInfo3.setStrLead(rs.getString("strSubjects"));
                    //get values for other instance variables
                    lotRecords4.add(voLotInfo3);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }  finally {
                closeConnection();
            }
            return lotRecords4;
        }
    }

Sorry for my coding I'm just a new programmer in town..

对不起我的编码我只是一个新的程序员在城里..

So please be good to me thanks!

所以请好好谢谢!

2 个解决方案

#1


0  

            PreparedStatement stmt = getCon().prepareStatement("SELECT * FROM MesLib.tblSchoolRecordsr where strstudName = ? and strDoubleCheckCode = ? and (lngstudID NOT LIKE '-[^0-9]') ");
            stmt.setString(1, studName);
            stmt.setString(2, DoubleCheck);
            stmt.setString(3, lngstudID);

Here two question marks (?) in your sql. So this query wants only two parameter. But you are setting three parameters. Thus index of 3 out of range exception occurs.

你的SQL中有两个问号(?)。所以这个查询只需要两个参数。但是你要设置三个参数。因此发生3个超出范围异常的索引。

To avoid this exception remove this line. Cause according to your sql this line (below) is unnecessary

要避免此异常,请删除此行。根据你的sql原因这行(下面)是不必要的

            stmt.setString(3, lngstudID); 

#2


1  

Just comment this line.

只需评论这一行。

stmt.setString(3, lngstudID);

The number of Question marks (?) in the query and supplying parameters should be same.

查询和提供参数中的问号(?)的数量应该相同。

As you are supplying lngstudId as extra you are getting exception.

当你提供额外的lngstudId时,你会遇到异常。

#1


0  

            PreparedStatement stmt = getCon().prepareStatement("SELECT * FROM MesLib.tblSchoolRecordsr where strstudName = ? and strDoubleCheckCode = ? and (lngstudID NOT LIKE '-[^0-9]') ");
            stmt.setString(1, studName);
            stmt.setString(2, DoubleCheck);
            stmt.setString(3, lngstudID);

Here two question marks (?) in your sql. So this query wants only two parameter. But you are setting three parameters. Thus index of 3 out of range exception occurs.

你的SQL中有两个问号(?)。所以这个查询只需要两个参数。但是你要设置三个参数。因此发生3个超出范围异常的索引。

To avoid this exception remove this line. Cause according to your sql this line (below) is unnecessary

要避免此异常,请删除此行。根据你的sql原因这行(下面)是不必要的

            stmt.setString(3, lngstudID); 

#2


1  

Just comment this line.

只需评论这一行。

stmt.setString(3, lngstudID);

The number of Question marks (?) in the query and supplying parameters should be same.

查询和提供参数中的问号(?)的数量应该相同。

As you are supplying lngstudId as extra you are getting exception.

当你提供额外的lngstudId时,你会遇到异常。