Skip to content Skip to sidebar Skip to footer

Java.sql.sqlexception: Parameter Index Out Of Range (1 > Number Of Parameters, Which Is 0). While Using Preparedstatement

Using preparedStatement in Java/MariaDb as in the following function public ArrayList findPath(String roomName) throws SQLException, IOException, InstantiationExcepti

Solution 1:

As was mentioned in the comment, you should remove the quotes from the ?.

In addition, in rs.getString(i), i should be positive. Start the count of the loop from 1.

To summarize:

public ArrayList<String> findPath(String roomName)throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
    ArrayList<String> path = newArrayList<String>(); 
    connection = getConnection();
    StringqueryPattern="SELECT Livello_1, Livello_2, Livello_3, Livello_4 FROM Camera WHERE Camera.Nome = ?";
    PreparedStatementqueryStatement= connection.prepareStatement(queryPattern);
    queryStatement.setString(1, roomName);
    ResultSetrs= queryStatement.executeQuery();
    if(rs.next())
    {
        for(inti=1; i < 4; i++)
        {
            path.add(rs.getString(i));
        }
    }
    return path;
}

Post a Comment for "Java.sql.sqlexception: Parameter Index Out Of Range (1 > Number Of Parameters, Which Is 0). While Using Preparedstatement"