File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Update more than one rows through one Submit/Update Button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Update more than one rows through one Submit/Update Button" Watch "Update more than one rows through one Submit/Update Button" New topic

Update more than one rows through one Submit/Update Button

Mehak Daani

Joined: Oct 08, 2003
Posts: 16
Can any one help me out on this ...
i have 10 different data in 10 different textboxes/combo boxes..... now i want to "update" the database with the data that is there in the fields through ONE update button .....
So its like i have a unique id field ... as IdNumber .... and for these idnumber i'm entering values in the text box as well as combo boxes....
if there would be only one text box called date and one combo box named class then my update statement would be ...
(Update <tablename> set date = '"+date+"',class ='"+class+"' where idnumber = '"+idNumber+"')
but now the problem is that i have more than 1 data for both the date textbox and class combo box ... so how would i go bout the updating of the records ......
IDnumber :123,124,125,126
TextboxDate : 2003-10-15 ,2003-20-10 ,2003-09-03 .
ClassCombobox abc, def,ghi
So in the database it would be updating the coloum date and class for the IDNUMBER 123 where the date feild would be updated with the value 2003-10-15 and the class field will be updated with the value abc ... then for the IDNUMBER 124 the date field will be updated with the value 2003-20-10 and the class field will be updated with the value "def" and so on ...
so that means that for each idnumber i have to update the database date feild and combo box .... how would i do that .....
plz keep in mind that all this has to be done ... on ONE sumbit button/update button which calls another jsp ......
Retreiving the values is not a problem ... updating the values corresponding to the idnumber is a problem ....
Plz if any one else can help .... i would be very thankful
If possible ..... please try and include a sample code

Malhar Barai
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
Can you possible loop the update idea quite off the top of my mind....there could be some other possiblities too...
What I mean to say is..
May be you can store the values in an array, & run the update query in a loop in your bean.

Malhar Barai
SOA & Java Book
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 642
C if following approch hepls u

String sDates[]=request.getParameterValues("date");//date is name of ur text box
String sClass[]=request.getParameterValues("class");
String sIds=request.getParameterValues("id");
for(int i=0;i<=ids.length;++i)
(Update <tablename> set date = '"+sDates[i]+"',class ='"+sClass[i]+"' where idnumber = '"+sIds[i]+"')

All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Mehak Daani

Joined: Oct 08, 2003
Posts: 16
Hi praful....
I tried the code that u had posted.. but now it gives the error :
Incompatible type for =. Can't convert java.lang.String to

Amit KumarS
Ranch Hand

Joined: Oct 10, 2003
Posts: 100
can u post your code that we can understand better???

****************************<br />In 24 hrs Earth rotates once on its Axis.
Mehak Daani

Joined: Oct 08, 2003
Posts: 16
Below is my full code ..... my "prioset.jsp" is calling this file. ....
all the names that u see in the while loop like txtname+i,prio+i,exp+i..are coming from the previous jsp where the name of the textbox and combo box keeps changing ........
so the txtname+i goes into the tg_date ,then the prio+i goes into the priority and the exp+i goes into the expert string ....and in my databse corresponding to each call id i have a tg_date,priority and expert coloum ....
i have removed the String[] and now i'm using vectors to store the values through the add element ..... now if i write
a_call = v_callids.elementAt(cnt).toString();
a_exp= v_experts.elementAt(cnt).toString();
a_pri = v_pri.elementAt(cnt).toString();
a_date = targetdate.elementAt(cnt).toString();

now if i have supposing 4 records .... so for the above code .... the a_call get the 4 id's but the a_exp,a_pri,and a_date get only ONE value ..... that means that the loop iterates 4 times for the a_call but only onece for the other three ....
The reason what came into my mind was that in my for loop the vector whose size i'm taking is v_callid's and the a_call is also storing the value v_callids.elementAt(cnt) ...... so may be that's y it's get all 4 values .... coz i couldn't notice any other reason as to y it should iterate 4 times only for "a_call"....???
If any one can find any other reason do let me know ......
Now how can i iterate through all the vectors one after another .... means ..... that i want to write a loop such that it should iterate and get the first value of the callid,tg_date,pri,expert ...then update ...then iterated through the second value ... and so on ....!!!
I have tried
String date[] = request.getParameter("txtname" + i);
But it gives a conversion error
Incompatible type for =. Can't convert java.lang.String to
Any body has ideas to solve my problem ....!!! :roll:
my code is below .... awaiting for replies .... and yeah how do i convert a string to String[]......??? is there any built in function for it ...???

Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/it_support","root","");
int i = 0;
int n =0;
int ex =0;
int p = 0;
int cnt = 0;
int c =0;

String tgdate = new String();
String tg_date = new String();
String tempdate = new String();
String temp_expert = new String();
String experts = new String();
String tempexp = new String();
String pri = new String();
String temp_pri = new String();
String priority = new String();
String callid = new String();
String temp_callid = new String();
String call_id = new String();
String a_call = new STring();
String a_exp = new STring();
String a_pri = new STring();
String a_date = new STring();

Vector targetdate = new Vector();
Vector v_expert = new Vector();
Vector v_pri = new Vector();
Vector v_callid = new Vector();
Vector v_callids = new Vector();
Statement stmt1 = conn.createStatement();
Statement uprecord = conn.createStatement();
ResultSet gtcount = stmt1.executeQuery("Select * from complaints where status =0");
i = i+1;
tgdate = request.getParameter("txtname" + i) ;
temp_expert = request.getParameter("exp" + i);
pri = request.getParameter("prio" + i);
call_id = gtcount.getString("callid");

tempdate = targetdate.toString();
n = tempdate.length()-1;
tg_date = tempdate.substring(1,n);
tempexp = v_expert.toString();
ex = tempexp.length()-1;
experts = tempexp.substring(1,ex);
temp_pri = v_pri.toString();
p = temp_pri.length()-1;
priority = temp_pri.substring(1,p);
temp_callid = v_callid.toString();
c = temp_callid.length()-1;
callid = temp_callid.substring(1,c);
out.println("The values" + ca);
out.println("The string values" + tg_date);
out.println("Total no: of Records Retreived " + i);
out.println("The string experts" + experts);
out.println("The string priority" + priority);
out.println("The string callid " + callid);

out.println("Entering in loop");
a_call = v_callids.elementAt(cnt).toString();
a_date = targetdate.elementAt(cntd).toString();
a_exp = v_expert.elementAt(cnte).toString();
a_pri = v_pri.elementAt(cntp).toString();
out.println("calls" + a_call);
out.println("a_date" + a_date);
out.println("exp" + a_exp);
out.println("pri" + a_pri);

<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 642
It's not
String date[] = request.getParameter("txtname" + i);
it is
String date[] = request.getParameterValues("txtname" + i);
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63837

Welcome to the Ranch Sweety!
You'll find this forum a great place to seek help on JSP pages, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it.
JSP Forum Bartender

[Asking smart questions] [About Bear] [Books by Bear]
Mehak Daani

Joined: Oct 08, 2003
Posts: 16
thanks to all for helping me out .... now my jsp is working fine....!!!

I agree. Here's the link:
subject: Update more than one rows through one Submit/Update Button
jQuery in Action, 3rd edition