博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
站内搜索 简单粗暴放代码
阅读量:7070 次
发布时间:2019-06-28

本文共 2000 字,大约阅读时间需要 6 分钟。

第一步:

  创建表单,放搜索框

输入用户名:

第二步:

  写JS

第三步:

  在Web层创建Servlet,使用的gson转换的json格式,需要导包

public class SearchWordServlet extends HttpServlet {    protected void doGet(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {        //获得关键字        String word = request.getParameter("word");        //查询改关键字的所有商品        UserService service = new UserService();        List userList = null;        try {            userList = service.findUserByWord(word);        } catch (SQLException e) {            e.printStackTrace();        }        //[{"username":"admin","phone":"17805054371","role":"超级管理员"...},{},{}...]        //使用json的转换工具        /*1、JSonlib        JSONArray fromObject = JSONArray.fromObject(userList);        String string = fromObject.toString();        System.out.println(string);    */            Gson gson = new Gson();        String json = gson.toJson(userList);        response.setContentType("texy/html;charset=UTF-8");        response.getWriter().write(json);        }    protected void doPost(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {        doGet(request,response);    }}

第四步:

  创建Service

public class UserService {    public List findUserByWord(String word) throws SQLException {        UserDao dao = new UserDao();        return  dao.findUserByWord(word);            }}

第五步:

  创建Dao,使用的C3P0和DBUtils

public class UserDao {    public List findUserByWord(String word) throws SQLException {        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());        String sql = "select * from user where username like ? limit 0,8";        List query = runner.query(sql,new ColumnListHandler("username"),"%"+word+"%");        return query;    }}

第六步:

  完成并测试

  

转载于:https://www.cnblogs.com/yg1024/p/8371373.html

你可能感兴趣的文章
云计算大数据(Hadoop)开发工程师项目实战视频教程(九部分)
查看>>
Verizon:2012年数据破坏调查报告
查看>>
《iPhone与iPad开发实战—iOS经典应用剖析》连载四
查看>>
手动完成输入校验
查看>>
直击Dell World 2014: 戴尔现在挺好的
查看>>
⑨③-不能浪费拥有的年轻资本
查看>>
自定义编译安装python简单笔记。
查看>>
书摘—极致产品
查看>>
5.10-17项目经理考试圆梦提分现场面授行动
查看>>
IT项目中存储设备的选型
查看>>
esxi报错There is no more space for virtual disk--逻辑卷缩减!
查看>>
NFS4中小企业存储实战
查看>>
Windows Server 2012 R2工作文件夹⑨:自动发现设置
查看>>
ActiveReports 6.0 - 高效开发UI
查看>>
VMM2012应用指南之4-向VMM中添加Hyper-V主机与应用服务器
查看>>
最终用户计算一周观察 002
查看>>
Android应用程序进程启动过程的源代码分析(1)
查看>>
km itm监控启停命令记录
查看>>
NIST:制定Cybersecurity的框架进行时
查看>>
无缝升级Windows8.1普通版至专业版
查看>>