詳解基于MVC的數(shù)據(jù)查詢模塊進(jìn)行模糊查詢
更新時(shí)間:2020年01月15日 10:26:06 作者:小任性嘛
這篇文章主要介紹了Java基于MVC的數(shù)據(jù)查詢模塊進(jìn)行模糊查詢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
完成一個簡單的基于MVC的數(shù)據(jù)查詢模塊,要求能夠按照name進(jìn)行模糊查詢。
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(); //執(zhí)行查詢
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;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java整合mybatis實(shí)現(xiàn)過濾數(shù)據(jù)
這篇文章主要介紹了Java整合mybatis實(shí)現(xiàn)過濾數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
SpringBoot自定義注解之實(shí)現(xiàn)AOP切面日志詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot自定義注解之實(shí)現(xiàn)AOP切面統(tǒng)一打印出入?yún)⑷罩?,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
maven項(xiàng)目中<scope>provided</scope>的作用及說明
這篇文章主要介紹了maven項(xiàng)目中<scope>provided</scope>的作用及說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Java正則表達(dá)式之split()方法實(shí)例詳解
這篇文章主要介紹了Java正則表達(dá)式之split()方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了split方法的功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-03-03

