user991873
user991873

Reputation: 11

Can any one tell me what is wrong with PrepareStatement syntax

public ResultSet readSubSet(int No, String name, String case) throws SQLException {
    preparedStatement = connect.prepareStatement("SELECT *  FROM target WHERE myName=? AND myCase=? LIMIT ?,10");
    preparedStatement.setString(8, name);
    preparedStatement.setString(6, case);
    preparedStatement.setInt(1, No);
    resultSet = preparedStatement.executeQuery();   
    return resultSet;
}

After run I got: java.sql.SQLException: Parameter index out of range (8 > number of parameters, which is 2).

my table define myName and myCase is TEXT, is that ok I use prepareStatement.setString

Upvotes: 0

Views: 58

Answers (2)

Rajeev 4circlesllc
Rajeev 4circlesllc

Reputation: 1

In your SQL you only have 3 variables and you are setting variable 8 as name in preparedStatement.setString(8, name)...

Look @ http://download.oracle.com/javase/tutorial/jdbc/basics/prepared.html#supply_values_ps

Upvotes: 0

nonVirtualThunk
nonVirtualThunk

Reputation: 2852

When you call setString you pass in two arguments, the first is the index of the parameter you are filling in and the second is the value you are filling it with. In your case, you would want prepareStatement.setString(1,name); to fill in the 1st parameter, prepareStatement.setString(2,case); to fill in the 2nd parameter, and so on.

Upvotes: 1

Related Questions