欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-20 17:24:41
完成一个简单的基于mvc的数据查询模块,要求能够按照name进行模糊查询。 index.jsp: <%@ page import="student.testbea...

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

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