Issue
I am trying to get the values that inserted into the database dynamically but I just got the last record using servlet and jsp. This my ado implementation
String sql = "SELECT * FROM eductional_details where schoolname=?"; //To change body of generated methods, choose Tools | Templates.
educatiodetails educat = null;
try {
PreparedStatement ps = (PreparedStatement) con
.prepareStatement(sql);
ps.setString(1, schoolname );
ResultSet rs = ps.executeQuery();
while (rs.next()) {
educat = new educatiodetails();
String DateFrom = rs.getString("datForm");
String DateTo = rs.getString("DatTo");
String DiscriptDetails = rs.getString("Descript");
educat.setSchoolname(schoolname);
educat.setDataform(DateFrom);
educat.setDatato(DateTo);
educat.setDiscription(DiscriptDetails);
}
And here my servlet side
String[] schoolname = request.getParameterValues("shoolname");
String[] dataform = request.getParameterValues("Dateform");
String[] datato = request.getParameterValues("Datato");
String[] discription = request.getParameterValues("Dscript");
for ( int i = 0 ; i < schoolname.length;i++ ) {
educatiodetails edu = new educatiodetails();
edu.setSchoolname(schoolname[i]);
edu.setDataform(dataform[i]);
edu.setDatato(datato[i]);
edu.setDiscription(discription[i]);
EducatiIMP education = new EducatiIMP();
education.addEducation(edu);
HttpSession session = request.getSession(true);
addEducation.getList();
EducatiIMP ado = new EducatiIMP();
educatiodetails educat = ado.geteducatiodetails(schoolname[i].toString());
session.setAttribute("educat",educat);
and here my jsp viewer
<c:forEach items="${educat}" var="item" varStatus="state" >
<p><c:out value="${item.schoolname}"/></p>
</c:forEach>
I tried for each but I got only the last recorded form my database, the insert working fine just the select or retrieve the user information not work well.
Solution
Maybe below code might help you :
Servlet:
String[] schoolname = request.getParameterValues("shoolname");
String[] dataform = request.getParameterValues("Dateform");
String[] datato = request.getParameterValues("Datato");
String[] discription = request.getParameterValues("Dscript");
//creating arraylist of type educatiodetails
ArrayList<educatiodetails> ab= new ArrayList();
for ( int i = 0 ; i < schoolname.length;i++ ) {
educatiodetails edu = new educatiodetails();
edu.setSchoolname(schoolname[i]);
edu.setDataform(dataform[i]);
edu.setDatato(datato[i]);
edu.setDiscription(discription[i]);
//storing value in arraylist
ab.add(edu);
//setting attribute in r1
request.setAttribute("r1", ab);
request.getRequestDispatcher("yourjsppage").forward(request, response);
Jsp :
<!--Here we are printing result-->
<c:forEach var="item" items="${r1}">
<!--printing values-->
<p> ${item.schoolname}</p>
</c:forEach>
Answered By - Swati