jsp中利用sql的模糊查找

  1. % 这个符号有三种用法
    将它放在前面“%x” 这是查询数据库中你以 x 结尾的数据
    将它放在后面“x%” 这是查询数据库中你以 x 开头的数据
    将它放在两侧“%x%” 这是查询数据库中你的数据中有 x 的数据

  2. “_” 这个符号的用法跟“%”的用法是一样的
    但是它是一个占位符,只能占一个字符的位置而“%”可以帮多个字符占位置
    比如“_x” ax能被查找到 但是aax是不能的(使用的时候你要知道你要查找的数据有多少个字符)

  3. “[ ]”这个符号的用法是,将【】里面的字符匹配一个就能被查找到
    比如 “[123456]x” 1x、2x、3x…能被找到 但是7x、8x是不能找的

  4. “[^ ]”跟上面的“[ ]”作用是相反的,上面能被找到的,用了这个就不会被找到

  5. 特殊的字符是不能用上述的方法去模糊查找的 “%” 、“_”、“[”
    如果利用jsp连接数据库去查找的话在前面加一个“\”就能被找到

下面是我自己建立的数据库和自己用的查找的代码 我用的是%去查找的

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<input type="text" id="a">
<input type="button" onclick="jiancha()"value="检查">
<table width="100" height="167" border="1" align="center">
		<caption>查询表单</caption>
		<%
			String a = (String) session.getAttribute("xxx");
		
			if (a == null || a.equals(""))
				a = "";
			else
				a = " where " + a;
			String b = "select * from b " + a;
			try {
				Class.forName("com.mysql.cj.jdbc.Driver");
			} catch (Exception e) {
				System.out.println("加载数据驱动时抛出异常,内容如下:");
				e.printStackTrace();
			}
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/a?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8",
					"root", "123456");
			Statement pStmt = conn.createStatement();
			ResultSet rs = pStmt.executeQuery(b);
			int i = 0;
			while (rs.next()) {
		%>
		<tr>
			<td align="center" valign="middle"><%=rs.getString(1)%></td>
		</tr>
		<%
			}
			rs.close();
			pStmt.close();
			conn.close();
		%>
	</table>
<script>
function jiancha()
{
	var a = encodeURI(document.getElementById("a").value); 
	createRequest("test1.jsp?a=" + a);
	}

	function createRequest(url) {
		http_request = new XMLHttpRequest();
		http_request.onreadystatechange = function() {
			if (http_request.readyState == 4) {
				if (http_request.status == 200) {
					alert(http_request.responseText);
					window.location.reload();
				} else {
					alert("您所请求的地址有错误");
				}
			}
		}
		http_request.open("POST", url, true);
		http_request.send(null);

	}
		</script>
</body>
</html>

这是test1.jsp 我是利用ajax技术去将文本框中的值转化放在session中,在原来的代码中去显示数据库

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String a = new String(request.getParameter("a").getBytes("UTF-8"), "UTF-8");

String b = "";

if(a.equals("")) b = a;		
else b = "name like '%" + a + "%'";

session.setAttribute("xxx", b);

%>

sql模糊查找(利用jsp)我就使用查找特殊符号的,其余的你们复制代码去测试就行了

sql模糊查找(利用jsp)
sql模糊查找(利用jsp)
最后我在b站看到一个做弹幕的代码 挺有意思的 发个截图
sql模糊查找(利用jsp)

相关文章:

  • 2021-10-31
猜你喜欢
  • 2022-01-02
  • 2022-01-13
  • 2022-12-23
  • 2021-10-22
  • 2022-12-23
  • 2021-08-11
  • 2021-08-01
相关资源
相似解决方案