title: SQL CallablStatement - The index is out of range
body: <p>I'm using java servlets with database and I always get error with "The index 2 is out of range." I just want to send a parameters name and message from a form and through procedure save into database. Why I get this error message?</p>
  
  <p>Add.jsp</p>
  
  <pre><code>&lt;p&gt;${result}&lt;/p&gt;
      &lt;form action="Servlet" method="post"&gt;
          &lt;div class="comment-form"&gt;
              &lt;input type="hidden" name="action" value="4"&gt;
              &lt;p class="name"&gt;
                  &lt;label&gt;Name:&lt;/label&gt;
                  &lt;input type="text" name="name" value="${param.name}"&gt;
              &lt;/p&gt;
              &lt;p class="text"&gt;
                  &lt;label&gt;Message:&lt;/label&gt;
                  &lt;textarea name="message"&gt;&lt;/textarea&gt;
              &lt;/p&gt;
              &lt;p class="submit"&gt;
                  &lt;input type="submit" name="btnComment" value="Add"&gt;
              &lt;/p&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>Servlet.java</p>
  
  <pre><code>    String name = request.getParameter("name");
      String message = request.getParameter("message");
      if (name == null || name.trim().equals("")) {
          request.setAttribute("result", "Name missing!");
          return "Add.jsp";
      } else {
          try {
              con = comments.getConnection();
              synchronized (con) {
                  stat = con.prepareCall("{CALL procAdd(?)}");
                  stat.setString(1, name);
                  stat.setString(2, message);
                  stat.executeUpdate();
              }
              request.setAttribute("result", "Comment added.");
              return "Comments.jsp";
          } catch (Exception e) {
              request.setAttribute("result", "Error: " + e.getMessage());
              return "Add.jsp";
          }
  </code></pre>
  
  <p>Procedure procAdd</p>
  
  <pre><code>CREATE PROCEDURE procAdd (
  @Name VARCHAR (50),
  @Message VARCHAR(50))
  AS
  BEGIN
  INSERT INTO Comments(Name, Date, Message) VALUES (@Name, GETDATE(), @Message)
  END
  </code></pre>