| | |
Problem with update data to database
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 15
Reputation:
Solved Threads: 0
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 + "%'");
}
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 + "%'");
}
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
----------------------------------------------
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
•
•
Join Date: Oct 2006
Posts: 15
Reputation:
Solved Threads: 0
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 + "%'");
}
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 + "%'");
}
•
•
Join Date: Oct 2006
Posts: 15
Reputation:
Solved Threads: 0
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 + "%'");
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 + "%'");
Because you are defining the variable inside the loop. Change the following:
to
JSP Syntax (Toggle Plain Text)
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); }
JSP Syntax (Toggle Plain Text)
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
----------------------------------------------
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
•
•
Join Date: Oct 2006
Posts: 15
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- insert and update data in two different database (JSP)
- How to insert data into database? (JSP)
- How to adding new data to database (Visual Basic 4 / 5 / 6)
- Populate data from database into JList (Java)
- Updating database with related tables and queries?? (VB.NET)
- Login and retrieve user data from database (ASP.NET)
Other Threads in the JSP Forum
- Previous Thread: Unsure how to save to database dynamic Textfield values
- Next Thread: insert values to mysql database
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






