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.

:sad::sad::sad:
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

Edited 3 Years Ago by pyTony: fixed formatting

String ca1 = request.getParameter("ca1_<%=i%>");

should this not be

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.

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.

If what you mean, is that you want to concatenate all values into one string and then insert that then change

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 {
    // whatever it is you want here
  }
}

to

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

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.

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.

This question has already been answered. Start a new discussion instead.