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


本文向大家介绍详解基于MVC的数据查询模块进行模糊查询,包括了详解基于MVC的数据查询模块进行模糊查询的使用技巧和注意事项,需要的朋友参考一下

完成一个简单的基于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;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#yiidian.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。