I am trying to insert records to a database from jsp. The records are multiple line items returned from a Post action in a form. How do I arrange the records so that I can insert them properly? I can get one record to insert, but not more than one. Please show a code sample.

Ryan Breidenbach

Since I don't know what your JSP page look like, I will simulate a similar form:

<FORM ACTION="post" .... >
Line Item: <INPUT TYPE="text" NAME="lineItem"><BR>
Line Item: <INPUT TYPE="text" NAME="lineItem"><BR>
...
</FORM>
This would be the form in the JSP that is submitting multiple line items. Now, you would have a servlet that processes this request, extacts the text from the form and inserts it into a database:
public class LineItemServlet extends HttpServlet
{
  public void doPost(HttpServletRequest req, HttpServletResponse res)
  {
    String lineItems[] = request.getParameterValues("lineItem");
    for (int i = 0; i < lineItems.length; i++)
    {
      insertLineItem(lineItems[i]);
    }
  }

  private void insertLineItem(String lineItem)
  {
    /*
     * Here is where the data access code would be. You could access
     * the database from the servlet or use another class that handle data access.
     */
  }
}
This is a very minimalistic approach to your problem, but is should work. Also, you don't have to name all the parameters the same, I just think it is easier for handling multiple parameters of the same type.
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact