jowdislove
jowdislove

Reputation: 27

Index of 3 is out of range

I'm new to this forum and also new to 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

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!

Upvotes: 0

Views: 5190

Answers (2)

Md. Nasir Uddin Bhuiyan
Md. Nasir Uddin Bhuiyan

Reputation: 1596

            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.

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

            stmt.setString(3, lngstudID); 

Upvotes: 1

Ravi MCA
Ravi MCA

Reputation: 2621

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.

Upvotes: 1

Related Questions