java怎么通过servlet获取from的传值查询mysql数据库对应表的数据并显示到jsp页面中? 10
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServl...
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String name = request.getParameter("name"); String fullName = request.getParameter("fullName"); try { List userList = findLists(name,fullName); request.setAttribute("userList", userList); request.getRequestDispatcher("/client/listUser.jsp").forward(request, response); } catch (SQLException e){ e.printStackTrace(); } }private List findLists(String name, String fullName) throws SQLException { // TODO Auto-generated method stub Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try{ //连接数据库 conn = DbUtil.getConnection(); //创建PrepareStatement对象 String sql = "SELECT * FROM user_table where name like ? or fullname like ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1,"'%" + name +"%'"); pstmt.setString(2,"'%" + fullName +"%'"); //执行查询 rs = pstmt.executeQuery(); //储存查询出来的用户数据 List userList = new ArrayList(); //遍历结果集 while(rs.next()){ int id = rs.getInt("id"); System.out.print(name); String password = rs.getString("password"); String gender = rs.getString("gender"); String phone = rs.getString("phone"); String email = rs.getString("email"); int status = rs.getInt("status"); int roleId = rs.getInt("role_id"); String roleName = rs.getString("roleName"); User user = new User(id, name, password, fullName, gender, phone, email, status); Role role = new Role(roleId, roleName); user.setRole(role); userList.add(user); } return userList; }finally{ DbUtil.close(pstmt, rs, conn); } }表里面有name字段“a”字符数据,但我想找到name里有“a”字符的数据,可是查询不到相应的数据,怎么搞
展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询