简体   繁体   English

如何使用Spring JDBC检索记录

[英]How to retrieve a record using Spring JDBC

I am trying to search for a record and display it in a JSP using Spring JDBC. 我正在尝试搜索记录,并使用Spring JDBC在JSP中显示它。 I am facing the below error now on the browser. 我现在在浏览器上遇到以下错误。

Exception Message: page 异常消息: 页面

My JSPs: Search.jsp: 我的JSP:Search.jsp:

 <div class="container"> <div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3"> <div class="row"> <div class="col-xs-12 col-sm-6 col-md-6"> <div class="form-group"> <form action="/SpringMail/searchresults" method="get"> <input type="text" name="searchRecord" id="search_record" class="form-control input-lg" placeholder="Enter email to search" tabindex="1" required="required"> <div class="col-xs-12 col-md-6"><input type="submit" value="Search" class="btn btn-primary btn-block btn-lg search" tabindex="4"></div> </form> </div> </div> </div> </div> </div> 

Searchresult.jsp: It has a table. Searchresult.jsp:它有一个表。 So I am just including the code of that. 因此,我只包含其中的代码。

 <table class="table"> <thead> <tr> <th>#</th> <th>Firstname</th> <th>Lastname</th> <th>DisplayName</th> <th>DateOfBirth</th> <th>Email</th> <th>Password</th> <th>Contact</th> <th>Skills</th> </tr> </thead> <tbody> <% List students = (List)request.getAttribute("searchresult"); Iterator stdIter = students.iterator(); while(stdIter.hasNext()) { Student s = (Student) stdIter.next(); %> <tr> <td><% s.getFirstName(); %></td> <td><% s.getLastName(); %></td> <td><% s.getDisplayName(); %></td> <td><% s.getDateOfBirth(); %></td> <td><% s.getEmail(); %></td> <td><% s.getPassword(); %></td> <td><% s.getContact(); %></td> <td><% s.getStudentSkills(); %></td> </tr> <% } %> </tbody> </table> 

My Controller: RegistrationController: 我的控制器:RegistrationController:

    `   // For Search
@RequestMapping(value="/search", method=RequestMethod.GET)
public ModelAndView getSearchForm() {
    ModelAndView searchmv = new ModelAndView("search");
    return searchmv;
}

@RequestMapping(value="/searchresults/{email}", method=RequestMethod.GET)
public ModelAndView searchResults(@PathVariable("email") String email,HttpServletRequest request) {
    System.out.println("Inside Search method");
    ModelAndView searchResult = new ModelAndView("searchresult");
    request.setAttribute("searchresult", st.getStudent(email));
    return searchResult;
}
// For Search`

getStudent():


    public List<Student> getStudent(String email) {
    System.out.println("Inside getStudent() method");
    System.out.println("Entered email: " + email);
    List studentList = new ArrayList();
    String sql = "select * from student where email = " + email;
    studentList = jt.query(sql, new StudentRowMapper());
    return studentList;
}

I am doing this hands on for the first time. 我是第一次这样做。 Kindly guide me to the correct way of doing it. 请指导我正确的方法。

I got the answer. 我得到了答案。 The method: searchResults in RegistrationController is in the wrong format. 方法:RegistrationController中的searchResults格式错误。 I fixed it like this. 我这样修复。

    @RequestMapping(value="/searchresults", method=RequestMethod.GET)
public ModelAndView searchResults(@RequestParam("searchRecord") String email, Map model) {
    ModelAndView searchResult = new ModelAndView("searchresult");
    model.put("searchresult", st.getStudent(email));
    return searchResult;
}

Thanks for the inputs 感谢您的投入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM