使用Servlet完成条件查询
本文讲解使用Servlet如何实现条件查询(支持模糊搜索)。使用的数据库是MySQL5.7版本。
1 创建表和导入数据
在MySQL的test数据库中执行以下SQL来建立表和导入测试数据:
CREATE TABLE `customer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`gender` varchar(20) DEFAULT NULL,
`telephone` varchar(100) DEFAULT NULL,
`address` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
/*Data for the table `customer` */
insert into `customer`(`id`,`name`,`gender`,`telephone`,`address`) values (1,'张三','男','021-3654987','北京市海淀区'),(2,'李四','男','021-3332222','广州市天河区'),(3,'王五','男','020-1112234','广州市番禺区'),(4,'陈六','女','020-4443335','广州市越秀区'),(5,'测试账户2','男','020-232221222','广州番禺区'),(6,'张祥','男','020-232221222','广州市越秀区');
最终效果如下:
2 编写index.jsp页面
index.jsp用于输入搜索条件:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>一点教程网-使用Servlet完成条件查询</title>
</head>
<body>
<h1>使用Servlet完成条件查询</h1>
<form action="Search" method="post">
请输入客户名称:<input type="text" name="name"/><br/>
<input type="submit" value="搜索"/>
</form>
</body>
</html>
3 编写SearchServlet
SearchServlet用于完成从数据库的查询功能:
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class SearchServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String name=request.getParameter("name");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","root");
PreparedStatement ps=con.prepareStatement("select * from customer where name like ?");
ps.setString(1,"%"+name+"%");
out.print("<table width=50% border=1>");
out.print("<caption>以下为搜索结果:</caption>");
ResultSet rs=ps.executeQuery();
/* 显示结果*/
ResultSetMetaData rsmd=rs.getMetaData();
int total=rsmd.getColumnCount();
out.print("<tr>");
for(int i=1;i<=total;i++)
{
out.print("<th>"+rsmd.getColumnName(i)+"</th>");
}
out.print("</tr>");
while(rs.next())
{
out.print("<tr><td>"+rs.getInt(1)+"</td><td>"+rs.getString(2)+ ""
+" </td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td><td>"+rs.getString(5)+"</td></tr>");
}
out.print("</table>");
}catch (Exception e2) {
e2.printStackTrace();
}finally{
out.close();
}
}
}
4 配置web.xml
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>SearchServlet</servlet-name>
<servlet-class>SearchServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SearchServlet</servlet-name>
<url-pattern>/Search</url-pattern>
</servlet-mapping>
</web-app>
5 运行测试
先访问主页,http://localhost:8080/,如下:
输入搜索条件“张”,点击“搜索”,结果如下:
本案例需要在lib目录导入mysql驱动程序,点击下面链接下载:
热门文章
优秀文章