Unsure how to save to database dynamic Textfield values

Thread Solved

Join Date: Oct 2006
Posts: 15
Reputation: shaun09 is an unknown quantity at this point 
Solved Threads: 0
shaun09 shaun09 is offline Offline
Newbie Poster

Unsure how to save to database dynamic Textfield values

 
0
  #1
Oct 17th, 2006
I'm trying to create a list to record student grades into the database. I'm able to pull out the names from the database and create a table with textfields in it to input the data into.

This is the code i'm using

for(int i = 0; rst2.next(); i++){
.......
<th scope="col"><input name="ca1_<%=i%>" type="text" id="exam1" size="7" /></th>
...

I'm using jsp and mysql

My database fields are exam1, exam2, exam3, exam4

Thank you so much for your help.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 15
Reputation: shaun09 is an unknown quantity at this point 
Solved Threads: 0
shaun09 shaun09 is offline Offline
Newbie Poster

Re: Unsure how to save to database dynamic Textfield values

 
0
  #2
Oct 18th, 2006

This is what i've tried.

for(int i = 0; rst2.next(); i++){
String ca1 = request.getParameter("ca1_<%=i%>");
String query2 = "INSERT into results(ca1) values ('"+ca1+"')";
stm2.executeUpdate(query2);
}


The error as follows..

String not terminated at end of line.
String ca1 = request.getParameter("ca1_<%=i
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Unsure how to save to database dynamic Textfield values

 
0
  #3
Oct 18th, 2006
  1. String ca1 = request.getParameter("ca1_<%=i%>");
should this not be
  1. String ca1 = request.getParameter("ca1_" + i);
Assuming the parameter names are ca1_0, ca1_1, ca1_2, etc.

At this point in the code you are already inside a scriptlet tag (at least I assume you are), so additional scriptlet tags are nothing but syntax errors.
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 Quick reply to this message  
Join Date: Oct 2006
Posts: 15
Reputation: shaun09 is an unknown quantity at this point 
Solved Threads: 0
shaun09 shaun09 is offline Offline
Newbie Poster

Re: Unsure how to save to database dynamic Textfield values

 
0
  #4
Oct 18th, 2006
Thank you it worked. Now i have another problem. Lets say i want to save all the information as a single string in the database instead of it going into another row. Here is my code. I'm using checkbox now.


for(int i = 0; rst2.next(); i++){
if(request.getParameter("ca1_"+i) !=null){
Childic = rst2.getString("Childic") ;
String query2 = "INSERT into results(ca1) values ('"+Childic+"')";
stm2.executeUpdate(query2);
}
else{

}
}

Thank you so very much for your help.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Unsure how to save to database dynamic Textfield values

 
0
  #5
Oct 18th, 2006
If what you mean, is that you want to concatenate all values into one string and then insert that then change
  1. for(int i = 0; rst2.next(); i++){
  2. if(request.getParameter("ca1_"+i) !=null){
  3. Childic = rst2.getString("Childic") ;
  4. String query2 = "INSERT into results(ca1) values ('"+Childic+"')";
  5. stm2.executeUpdate(query2);
  6. } else {
  7. // whatever it is you want here
  8. }
  9. }
to
  1. for(int i = 0; rst2.next(); i++){
  2. if(request.getParameter("ca1_"+i) !=null){
  3. Childic = Childic + rst2.getString("Childic") ;
  4. } else {
  5. // whatever it is you want here
  6. }
  7. }
  8. String query2 = "INSERT into results(ca1) values ('"+Childic+"')";
  9. stm2.executeUpdate(query2);
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 Quick reply to this message  
Join Date: Oct 2006
Posts: 15
Reputation: shaun09 is an unknown quantity at this point 
Solved Threads: 0
shaun09 shaun09 is offline Offline
Newbie Poster

Re: Unsure how to save to database dynamic Textfield values

 
0
  #6
Oct 18th, 2006
Thank you for your help. But there seems to be a problem. It saves to database as

Right now it saves as
A - 1st record
AB - 2nd record
ABC - 3rd record

I want it to be

A B C - 1st record

Thank you for your help.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 15
Reputation: shaun09 is an unknown quantity at this point 
Solved Threads: 0
shaun09 shaun09 is offline Offline
Newbie Poster

Re: Unsure how to save to database dynamic Textfield values

 
0
  #7
Oct 18th, 2006
Got it working. This the code.


rst2 = stm1.executeQuery("select * from students");
String Childic = "";

for(int i = 0; rst2.next(); i++){
if(request.getParameter("ca1_"+i) !=null){
Childic += " " + rst2.getString("Childic") ;
}
String query2 = "INSERT into results(ca1) values ('"+Childic+"')";
stm2.executeUpdate(query2);


Thank you to all who viewed and helped.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 4418 | Replies: 6
Thread Tools Search this Thread



Tag cloud for JSP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC