我刚得到最后一张表的记录?

时间:2019-05-09 22:55:04

标签: servlets

我试图动态获取插入到数据库中的值,但我只是使用servlet和jsp获得了最后一条记录。这是我的道理

#if($ctx.args.approved) 
 #set($approved = $util.dynamodb.toBinaryJson("true"))
#else
 #set($approved = $util.dynamodb.toBinaryJson("false"))
#end

{
  "version": "2017-02-28",
  "operation": "Query",
  "query": {
    "expression": "#approved = :approved",
    "expressionNames": {
      "#approved": "approved",
    },
    "expressionValues": {
      ":approved":  $approved
    },
  },
  "index": "approved-index",
  "limit": $util.defaultIfNull($ctx.args.first, 20),
  "nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.after, null)),
  "scanIndexForward": true,
  "select": "ALL_ATTRIBUTES",
}

这是我的servlet端

    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);

                    }

这里是我的jsp查看器

  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);

我尝试了每种方法,但是我只得到了数据库的最后记录,插入操作仅在选择或检索用户信息时效果很好。

1 个答案:

答案 0 :(得分:1)

也许下面的代码可能会对您有所帮助:

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>