• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Add Multiple rows in Servlet

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Guys
I am in the process or creating a servlet which allows me to insert multiple rows in Database.

I am able to do with single line input however i am stuck when i get multiple rows.

Does anybody have an example of this so that i can get some ieda.

The process would be

A webpage will list multiple rows and user will update some rows with value.

A servlet should handle the multiple update..

thank you very much and thanks in advance..

regards,
Rashmi Trivedi.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am able to do with single line input however i am stuck when i get multiple rows.


If you can do it with a single line, then you can do it with several lines It's only a matter of loop, isn't it ? You get all the lines posted from JSP, you loop and insert.
 
Rashmi Trivedi
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. You are probably right.

Adding loop can be solution however i am strugggling to get the loop working.

How do tell servlet that there are multiple lines. I cannot get my head aroud this.

Can you please help??

Thanks in advance..

regards,
Rashmi Trivedi
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could you please be more clear with your question, so that i could help you out.

have a nice day.

krishna prasad
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds to me like your problem lies in the area of coding the fields in the HTML form so that your servlet can tell which field belongs to which row in the database. How are you coding the fields in the HTML form right now?

Bill
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thing you need to do is to get all data (rows) which you want to insert into your servlet.
For example on JSP each row has two text box which can be updated.

1. First Name
2. Surname

Now in the JSP make sure that the name of name and the surname text box is same for each row .Doing so you would be left with two arrays of text box. One for name and one for surname.

On the Servlet
use

String[] name = request.getParameterValues("name");
String[] surname = request.getParameterValues("surname")
Emolyee emp = null; // Mabe a bean which have getter and setter for name and surname

ArralList list = new ArrayList();
for(int i = 0 ;i<name.lenght;i++) {
emp = new Employee();
emp.setName(name[i]);
emp.setSurname(surname[i]);
list.add(emp);
}

Now pass this list to you database class.
There loop through the list and make dynamic insert statements

like

for(int k = 0;k<list.size();k++) {
insert into emp values(list.get(k).getName(),list.get(k).getName())
}
 
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSF will make this task easier. It can map the form object to a bean. There is little parsing of form data.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adopting a frameword like JSF before understanding how to manage simple web parameters from a for loop doen't sound like a good idea to me.

Learn the basics first, then see if a framework can speed things up for you.
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic