博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ajax 模糊查询的简单实现
阅读量:4309 次
发布时间:2019-06-06

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

类似于百度的搜索引擎模糊查询功能,不过百度的模糊查询功能更强大,这里简单实现下.

要实现模糊查询,首先要做的就是把SQL写好。话不多少,直接贴代码了!

JSP页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'search.jsp' starting page    	

Ajax自动搜索提示

Sevlet类:

package servlet.ajax;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class Search extends HttpServlet {	public void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {          request.setCharacterEncoding("UTF-8");          response.setCharacterEncoding("UTF-8");          String req=request.getParameter("txtSearch");          PrintWriter out = response.getWriter();          SearchDao sd=new SearchDao();          sd.getText(req);          StringBuffer sb=sd.getText(req);          out.print(sb.toString());          out.flush();	}}

 

进行连接数据库,模糊查询的JAVA类:

package servlet.ajax;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;public class SearchDao {		public StringBuffer  getText(String req){		//首先定义下连接数据的URL、用户名、密码		String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";		String user="scott";		String password="yulei123";		String sql="select ename from emp a where a.ename like ?";		if(req.trim().length()==0){	      sql=sql+" and 1<>1";		}		List strList=new ArrayList();		 try {			Class.forName("oracle.jdbc.driver.OracleDriver");			Connection con=DriverManager.getConnection(url,user,password);			PreparedStatement pre=con.prepareStatement(sql);			pre.setString(1,"%"+req.toUpperCase().trim()+"%");			ResultSet rs=pre.executeQuery();			while(rs.next()){				String ename=rs.getString("ename");				strList.add(ename);			}		} catch (ClassNotFoundException e) {			e.printStackTrace();		} catch (SQLException e) {			e.printStackTrace();		}		StringBuffer sb=new StringBuffer();		int size=strList.size();		for(int i=0;i

代码写好后,就可以在文本框输入字母后就可以模糊查询出数据了!

 

转载于:https://www.cnblogs.com/wuyida/p/6300391.html

你可能感兴趣的文章
ios设备唯一标识获取策略
查看>>
获取推送通知的DeviceToken
查看>>
Could not find a storyboard named 'Main' in bundle NSBundle
查看>>
CocoaPods安装和使用教程
查看>>
Beginning Auto Layout Tutorial
查看>>
block使用小结、在arc中使用block、如何防止循环引用
查看>>
iPhone开发学习笔记002——Xib设计UITableViewCell然后动态加载
查看>>
iOS开发中遇到的问题整理 (一)
查看>>
Swift code into Object-C 出现 ***-swift have not found this file 的问题
查看>>
为什么你的App介绍写得像一坨翔?
查看>>
RTImageAssets插件--@3x可自动生成@2x图片
查看>>
iOS开发的一些奇巧淫技
查看>>
常浏览的博客和网站
查看>>
Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.
查看>>
iOS在Xcode6中怎么创建OC category文件
查看>>
5、JavaWeb学习之基础篇—标签(自定义&JSTL)
查看>>
8、JavaWEB学习之基础篇—文件上传&下载
查看>>
reRender属性的使用
查看>>
href="javascript:void(0)"
查看>>
h:panelGrid、h:panelGroup标签学习
查看>>