主要完成的功能:
一、完成标题模糊查询
一般情况下我们可能需要查看某一类新闻,或者网上购物时在搜索框中输入自己所需的物品的关键字来查找物品,这个功能就是我们程序员所说的模糊查询功能!!下面我们来看一下该功能实现的代码吧。
根据输入的标题关键字进行模糊查询(例如:输入“疫情”则出现如下新闻)
模糊查询是在首页进行操作 代码在首页代码里面:
<%@page import="java.nio.charset.StandardCharsets"%>
<%@page import="sun.nio.cs.UTF_8"%>
<%@page import="sun.nio.cs.UTF_8"%>
<%@page import="sun.nio.cs.UTF_8"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>bootstrap</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
<link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
outline: none !important;
}
body,
html {
background: #7f8d90;
}
nav,
.breadcrumb {
border-radius: 0px !important;
margin-bottom: 0px !important;
}
.breadcrumb {
margin-bottom: 20px !important;
background: #36485c;
color: white;
}
li h4 {
width: 300px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.breadcrumb .active {
color: yellow;
}
</style>
</head>
<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="index.jsp" style="font-size: 25px;">⭐</a>
</div>
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown"> 新闻管理
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="/web04/news/add.jsp">新闻发布</a></li>
<li class="divider"></li>
<li><a href="/web04/news/show.jsp">类别管理</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a><%=request.getParameter("yh") %></a></li>
<li><a href="${pageContext.request.contextPath}/login.jsp">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>
</div>
</nav>
<ol class="breadcrumb">
<li>您当前的位置是</li>
<li>新闻发布系统</li>
<li class="active">首页</li>
</ol>
<form action="${pageContext.request.contextPath}/news/index.jsp" class="form-inline" style="margin: 0px auto 20px;">
<div class="form-group" style="display: block;text-align: center;">
<div class="input-group">
<div class="input-group-addon">新闻标题</div>
<input name="newName" class="form-control" placeholder="请在此输入搜索的关键字" type="text">
<span class="input-group-btn">
<button class="btn btn-primary" type="submit">搜索🔍</button>
</span>
</div>
</div>
</form>
<div class="container">
<ul class="list-group">
<%
//点击了表单之后 跳转的是当前这个页面 同时携带一个newName(查询的关键字)过来
String newName=request.getParameter("newName");
if(newName==null){
newName="";//查询所有
}
//破碎重组
//newName=new String(newName.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//定义连接字符串
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
Connection con=DriverManager.getConnection(url,"scott","123123");
//获得执行对象(查询所有的新闻数据)
PreparedStatement ps=con.prepareStatement("select * from t_news where news_title like ?");
ps.setString(1, "%"+newName+"%");
//得到结果集
ResultSet rs=ps.executeQuery();
//结果集中有很多数据
while(rs.next()){
%>
<li class="list-group-item">