详解基于MVC的数据查询模块进行模糊查询

作者:小任性嘛 时间:2022-02-13 19:26:49 

完成一个简单的基于MVC的数据查询模块,要求能够按照name进行模糊查询。

Index.jsp:


<%@ page import="student.TestBean" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%
List<TestBean> list = (List<TestBean>)request.getAttribute("list");
if(list == null){
 list = new ArrayList<TestBean>();
}

%>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
  content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="ScoreServlet">
NAME:<input type="text" name="Name">
<input type="submit" method="post">

<table border="1px solid black">
 <tr>
  <th>ID</th>
  <th>Name</th>
 </tr>
<%
for(int i = 0 ; i < list.size() ; i++){
 TestBean record = list.get(i);
%>
 <tr>
  <td><%=record.getId()%></td>
  <td><%=record.getName()%></td>
 </tr>
<%
}
%>
</table>
</form>
</body>
</html>

ScoreServlet.java:


import student.TestBean;
import student.TestDb;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "/ScoreServlet")
public class ScoreServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String strName = request.getParameter("Name");
if(strName == null)
 strName = "";

TestDb testDb = new TestDb();
 try {
  List<TestBean> list = testDb.findByName(strName);
  request.setAttribute("list",list);
  request.getRequestDispatcher("index.jsp").forward(request,response);
 } catch (ClassNotFoundException e) {
  e.printStackTrace();
 } catch (SQLException e) {
  e.printStackTrace();
 }
}
}

TestBean.java:


package student;
public class TestBean {
private int id;
private String name;

public int getId() {
 return id;
}

public void setId(int id) {
 this.id = id;
}

public String getName() {
 return name;
}

public void setName(String name) {
 this.name = name;
}
}

TestDb.java:


package student;
import student.TestBean;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class TestDb {
public List<TestBean> findByName(String Name) throws ClassNotFoundException,SQLException{
 List<TestBean> list = new ArrayList<TestBean>();
 String url="jdbc:h2:D:/temp/h2/mydb";
 Class.forName("org.h2.Driver");
 Connection conn = DriverManager.getConnection(url,"sa","");
 PreparedStatement pstmt = conn.prepareStatement("select ID,NAME from TEST where name like ?");
 pstmt.setString(1,"%"+Name+"%");
 ResultSet rs = pstmt.executeQuery(); //执行查询
 while(rs.next()){
  TestBean record = new TestBean();
  record.setId(rs.getInt(1));
  record.setName(rs.getString(2));
  list.add(record);
 }
 rs.close();
 pstmt.close();
 conn.close();
 return list;
}
}

来源:https://blog.csdn.net/weixin_43728903/article/details/103970650

标签:MVC,模糊查询
0
投稿

猜你喜欢

  • Java中final变量使用总结

    2022-09-29 08:32:00
  • JAVA.io读写文件方式汇总

    2022-10-14 06:32:21
  • 详解Java的回调机制

    2023-07-27 07:17:43
  • Android控件之SlidingDrawer(滑动式抽屉)详解与实例分享

    2023-07-27 09:26:18
  • Java Servlet3.0异步处理问题

    2023-08-12 00:52:05
  • Java几种常用的断言风格你怎么选

    2021-10-30 23:30:32
  • Java实现树形结构的示例代码

    2023-07-30 01:05:19
  • flutter中的资源和图片加载示例详解

    2023-08-24 13:19:39
  • Eclipse的Debug调试技巧大全(总结)

    2023-11-25 06:14:06
  • 关于@RequestBody和@RequestParam注解的使用详解

    2023-01-20 09:08:20
  • springboot集成RestTemplate及常见的用法说明

    2023-02-17 20:02:27
  • Springboot整合mqtt服务的示例代码

    2022-07-20 02:58:01
  • 使用JavaWeb webSocket实现简易的点对点聊天功能实例代码

    2023-10-29 00:14:17
  • 浅谈Android开发中项目的文件结构及规范化部署建议

    2022-05-13 12:47:37
  • Java面试之动态规划与组合数

    2023-11-24 21:20:52
  • @Configuration与@Component作为配置类的区别详解

    2023-03-09 19:50:15
  • 小程序与后端Java接 口交互实现HelloWorld入门

    2023-11-04 18:55:31
  • Spring之IOC详解

    2022-10-13 19:10:24
  • Flutter使用sqflite处理数据表变更的方法详解

    2023-10-21 11:05:49
  • Java删除二叉搜索树最大元素和最小元素的方法详解

    2023-09-30 07:27:09
  • asp之家 软件编程 m.aspxhome.com