详解基于MVC的数据查询模块进行模糊查询
程序员文章站
2023-01-25 18:15:04
完成一个简单的基于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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
详解基于MVC的数据查询模块进行模糊查询
-
MongoDB模糊查询操作案例详解(类关系型数据库的 like 和 not like)
-
基于Geomesa服务查询轨迹数据无法根据空间和时间范围进行查询的问题解决办法
-
使用MVC模式开发程序,完成数据的模糊查询
-
Python使用sql语句对mysql数据库多条件模糊查询的思路详解
-
基于数据库lucence 3.6.2多字段配合多关键字的模糊查询
-
详解基于MVC的数据查询模块进行模糊查询
-
MongoDB模糊查询操作案例详解(类关系型数据库的 like 和 not like)
-
使用MVC模式开发程序,完成数据的模糊查询
-
Python使用sql语句对mysql数据库多条件模糊查询的思路详解