User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 402,375 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,073 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 3451 | Replies: 7 | Solved
Reply
Join Date: Oct 2006
Posts: 15
Reputation: shaun09 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
shaun09 shaun09 is offline Offline
Newbie Poster

Help Problem with update data to database

  #1  
Oct 18th, 2006
I'm trying to cycle through a series of dynamic fields and update the value to the database. But the value of the last field is the one retrived and updated to all fields in the loop. How do i solve this problem. Thank you so so much for your help.



rst2 = stm1.executeQuery("select * from students");
String subject = request.getParameter("subject");

for(int i = 0; rst2.next(); i++){
String ca1 = request.getParameter("ca1_"+i);
String sa1 = request.getParameter("sa1_"+i);
String ca2 = request.getParameter("ca2_"+i);
String sa2 = request.getParameter("sa2_"+i);
//String query2 = "INSERT into results(ca1,sa1,ca2,sa2) values ('"+ca1+"','"+sa1+"','"+ca2+"','"+sa2+"')";
stm2.executeUpdate("UPDATE results SET ca1 ='"+ca1+"', sa1 ='"+sa1+"', ca2 ='"+ca2+"', sa2 ='"+sa2+"' where subject like '" + subject + "%'");
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,377
Reputation: masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough 
Rep Power: 8
Solved Threads: 120
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: Problem with update data to database

  #2  
Oct 18th, 2006
I think you can extropolate my answer from your other thread to fit this one. It seems to be the same thing.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Oct 2006
Posts: 15
Reputation: shaun09 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
shaun09 shaun09 is offline Offline
Newbie Poster

Re: Problem with update data to database

  #3  
Oct 18th, 2006
I tried this its also not working. What i plan to acheive is that all fields that match the value of subject must be updated. The value of subject is taken from a drop down box. This portion works. The problem is that it updates all fields with the same value. I think its because the update is in a loop? I'm not sure how to rectify my coding. Please help. Thank you for your kind assistance.


String ca1date = request.getParameter("dc1");
String sa1date = request.getParameter("dc2");
String ca2date = request.getParameter("dc11");
String sa2date = request.getParameter("dc22");
String subject = request.getParameter("subject");

rst2 = stm1.executeQuery("select * from results");

for(int i = 0; rst2.next(); i++){

String ca1 = request.getParameter("ca1_"+i);
String sa1 = request.getParameter("sa1_"+i);
String ca2 = request.getParameter("ca2_"+i);
String sa2 = request.getParameter("sa2_"+i);


stm2.executeUpdate("UPDATE results SET ca1 ='"+ca1+"', sa1 ='"+sa1+"', ca2 ='"+ca2+"', sa2 ='"+sa2+"', ca1date ='"+ca1date+"', sa1date ='"+sa1date+"', ca2date ='"+ca2date+"', sa2date ='"+sa2date+"' where subject like '" + subject + "%'");
}
Reply With Quote  
Join Date: Oct 2006
Posts: 15
Reputation: shaun09 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
shaun09 shaun09 is offline Offline
Newbie Poster

Re: Problem with update data to database

  #4  
Oct 19th, 2006
Anyone able to help....

Greatly appreciated.
Reply With Quote  
Join Date: Oct 2006
Posts: 15
Reputation: shaun09 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
shaun09 shaun09 is offline Offline
Newbie Poster

Re: Problem with update data to database

  #5  
Oct 19th, 2006
When i move the update out of the loop i get the following errors.

Undefined variable: ca1
Undefined variable: sa1
Undefined variable: ca2
Undefined variable: sa2


String ca1date = request.getParameter("dc1");
String sa1date = request.getParameter("dc2");
String ca2date = request.getParameter("dc11");
String sa2date = request.getParameter("dc22");
String subject = request.getParameter("subject");

rst2 = stm1.executeQuery("select * from results");

for(int i = 0; rst2.next(); i++){

String ca1 = request.getParameter("ca1_"+i);
String sa1 = request.getParameter("sa1_"+i);
String ca2 = request.getParameter("ca2_"+i);
String sa2 = request.getParameter("sa2_"+i);
}


stm2.executeUpdate("UPDATE results SET ca1 ='"+ca1+"', sa1 ='"+sa1+"', ca2 ='"+ca2+"', sa2 ='"+sa2+"', ca1date ='"+ca1date+"', sa1date ='"+sa1date+"', ca2date ='"+ca2date+"', sa2date ='"+sa2date+"' where subject like '" + subject + "%'");
Reply With Quote  
Join Date: Feb 2006
Posts: 1,377
Reputation: masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough 
Rep Power: 8
Solved Threads: 120
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: Problem with update data to database

  #6  
Oct 19th, 2006
Because you are defining the variable inside the loop. Change the following:
for(int i = 0; rst2.next(); i++){ 
  String ca1 = request.getParameter("ca1_"+i);
  String sa1 = request.getParameter("sa1_"+i);
  String ca2 = request.getParameter("ca2_"+i);
  String sa2 = request.getParameter("sa2_"+i);
}
to
String ca1 = "";
String sa1 = "";
String ca2 = "";
String sa2 = "";

for(int i = 0; rst2.next(); i++){ 
  ca1 += request.getParameter("ca1_"+i) + " ";
  sa1 += request.getParameter("sa1_"+i) + " ";
  ca2 += request.getParameter("ca2_"+i) + " ";
  sa2 += request.getParameter("sa2_"+i) + " ";
}

ca1 = ca1.trim();
sa1 = sa1.trim();
ca2 = ca2.trim();
sa2 = sa2.trim();
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Oct 2006
Posts: 15
Reputation: shaun09 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
shaun09 shaun09 is offline Offline
Newbie Poster

Re: Problem with update data to database

  #7  
Oct 19th, 2006
I'm sorry but it does not work. The value is still taken as the last value and updated to all fields. Also now the strings are joined together...
Reply With Quote  
Join Date: Oct 2006
Posts: 15
Reputation: shaun09 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
shaun09 shaun09 is offline Offline
Newbie Poster

Re: Problem with update data to database

  #8  
Oct 19th, 2006
I think my logic of the problem is wrong. I'm still learning and need all the help i can. This what I’m trying to achieve. I have 6 levels. Primary1-Primary6. Each level takes 4 different subjects. Each subject has four exams in a year. Ca1,sa1,ca2,sa2.

I'm creating a form where teachers can record the grades of the exam into the system so they can retrieve and modify the results anytime.

The problems I’m facing are

1) Database design
I have Childic,Name,Subject,Level,ca1,sa1,ca2,sa2 fields in a table called results. I understand that this is the wrong way to design a table as primary key is to be Childic. But I’m having the four subject assigned to one Childic. So now the database is like

Childic name, Subject, Level, ca, sa1, ca2, sa2
12345 xyz English Primary1
12345 xyz Science Primary1
12345 xyz Maths Primary1
12345 xyz History Primary1

2)The form to input the data. The teacher selects which subject to update the results. I then create dynamic tables to store the data.

String subject = request.getParameter("subject");
rst2 = stm1.executeQuery("select * from results where subject like '" + subject + "%'");
String Childic = "";
String Childname = "";

for(int i = 0; rst2.next(); i++){
Childic = rst2.getString("Childic") ;
Childname = rst2.getString("Name") ;
%>
<tr>
<th scope="col"> </th>
<th scope="col"><%=Childic%></th>
<th scope="col"><%=Childname%></th>
<th scope="col"><input name="ca1_<%=i%>" type="text" id="ca1" size="7" /></th>
<th scope="col"><input name="sa1_<%=i%>" type="text" id="ca2" size="7" /></th>
<th scope="col"><input name="ca2_<%=i%>" type="text" id="ca3" size="7" /></th>
<th scope="col"><input name="sa2_<%=i%>" type="text" id="ca4" size="7" /></th>
<th scope="col"> </th>
</tr>
<%

}
%>

3)I'm trying to update the record into the database. See previous post for coding.

I'm sorry if the logic of the design and programming is wrong. If someone is willing to guide me, I’m willing to learn and try. All your help is greatly appreciated.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JSP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JSP Forum

All times are GMT -4. The time now is 12:42 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC