91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

詳解基于MVC的數據查詢模塊進行模糊查詢

發布時間:2020-09-18 05:15:54 來源:腳本之家 閱讀:173 作者:小任性嘛 欄目:編程語言

完成一個簡單的基于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:h3:D:/temp/h3/mydb";
  Class.forName("org.h3.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;
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

扶余县| 宁河县| 陆川县| 康保县| 平乡县| 尼玛县| 项城市| 乌拉特中旗| 南阳市| 顺义区| 新化县| 探索| 武汉市| 沙坪坝区| 新田县| 来安县| 鄂州市| 天台县| 珠海市| 恩施市| 仙游县| 通州市| 右玉县| 安泽县| 岑巩县| 青岛市| 秭归县| 仪征市| 泽库县| 五家渠市| 巨鹿县| 温州市| 建始县| 华宁县| 阿瓦提县| 丹棱县| 赤峰市| 德钦县| 达拉特旗| 中西区| 西乌|