aspose file tools*
The moose likes Servlets and the fly likes Add Multiple rows in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Add Multiple rows in Servlet" Watch "Add Multiple rows in Servlet" New topic
Author

Add Multiple rows in Servlet

Rashmi Trivedi
Ranch Hand

Joined: Aug 13, 2001
Posts: 60
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Rashmi Trivedi
Ranch Hand

Joined: Aug 13, 2001
Posts: 60
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

Joined: Jul 25, 2006
Posts: 158
could you please be more clear with your question, so that i could help you out.

have a nice day.

krishna prasad


have a great day,
krishna prasad
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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

Joined: Apr 27, 2006
Posts: 38
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

Joined: Sep 20, 2001
Posts: 671
JSF will make this task easier. It can map the form object to a bean. There is little parsing of form data.


BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Add Multiple rows in Servlet