大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

详解基于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 list = (List)request.getAttribute("list");
 if(list == null){
  list = new ArrayList();
 }

%>



 
 
 
 Document


NAME: <% for(int i = 0 ; i < list.size() ; i++){ TestBean record = list.get(i); %> <% } %>
ID Name
<%=record.getId()%> <%=record.getName()%>

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 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 findByName(String Name) throws ClassNotFoundException,SQLException{
  List list = new ArrayList();
  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;
 }
}

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


文章名称:详解基于MVC的数据查询模块进行模糊查询
文章路径:http://dzwzjz.com/article/iedjop.html
在线咨询
服务热线
服务热线:028-86922220
TOP