Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add Multiple rows in Servlet

 
Rashmi Trivedi
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Rohit Dhodapkar
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • 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())
}
 
Bruce Jin
Ranch Hand
Posts: 672
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic