五、按姓名查询的JSP+jdbc+Servlet

因为按要求查询都是差不多一样的,所以举个栗子

详细说按姓名查询,然后其他的查询就贴代码了

-----------------------------------------------------------

需要的文件:

JSP页面:SearchName.jsp

Bean类:AddMessageBean.java  

Dao文件:AddMessageDao.java

Servlet文件:SearchName.java

Servlet配置:web.xml

---------------------------------------------------------------

SearchName.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import ="bean.AddMessageBean" %>  
<%@ page import ="java.util.List" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>按姓名查询</title>
<link rel="stylesheet" type="text/css" href="css/daohanglan1.css"/>
<link rel="stylesheet" type="text/css" href="css/tablecss.css"/>
</head>
<body>
	<div>
		<ul class="nav">  
		    <li><a href="AddMessage.jsp">学生录入</a></li>
		    <li><a href="AddWorkTime.jsp">工时录入</a></li>  
		    <li><a href="TimeManage.jsp">工时管理</a></li>   
		</ul>  
	</div>
	<div>
		<ul class="nav">  
		    <li><a href="TimeManage.jsp" id="btn1">全部信息</a></li>
		    <li><a href="SearchName.jsp" id="btn2">按姓名查询</a></li>  
		    <li><a href="SearchSex.jsp" id="btn3">按性别查询</a></li>  
		    <li><a href="SearchYear.jsp" id="btn4">按年份查询</a></li>
		    <li><a href="SearchID.jsp" id="btn5">按学号查询</a></li> 
		    <li><a href="SearchIns.jsp" id="btn6">按学院查询</a></li> 
		    <li><a href="SearchPro.jsp" id="btn7">按专业查询</a></li> 
		    <li><a href="SearchLevel.jsp" id="btn8">按贫困等级查询</a></li>   
		</ul>  
	</div>
	<div>
		<form action="SearchName" method="post">	<!-- Servlet名字为SearchName -->
		请输入姓名:<input type="text" name="name" >
		<input type="submit" value="查询" />
		</form>
		<hr/>
	</div>
    <div>
		<table border="1.5" bordercolor="black" style="border-collapse:collapse;margin-left: auto; margin-right: auto;">
			<tr><th>学号</th><th>学生姓名</th><th>性别</th><th>入学年份</th><th>学院</th><th>专业</th><th>贫困等级</th><th>详细信息</th><th>其他操作</th></tr>
			<%
			//新建一个AddMessageBean类的列表,名字为SearchName
			//getAttribute:将SearchName信息的值给SearchName
				List<AddMessageBean> SearchName=(List<AddMessageBean>)request.getAttribute("SearchName");
				if(SearchName!=null&&!SearchName.isEmpty()){
					for(AddMessageBean times : SearchName){
			%>
			<tr>
			<td><%=times.getNum() %></td>	<!-- 用对应的get方法,将times类的值返回 -->
			<td><%=times.getName() %></td>
			<td><%=times.getSex() %></td>
			<td><%=times.getYear() %></td>
			<td><%=times.getCollege() %></td>
			<td><%=times.getMajor() %></td>
			<td><%=times.getPoor() %></td>
			<td>
			<a href="Detail?num=<%=times.getNum() %>">查看</a> 	<!-- 调用servlet类Detail,并且num是等于getNum返回的值 -->
			</td>
			<td>
			<a href="Delete?num=<%=times.getNum() %>">删除</a>	<!-- 调用servlet类Delete,并且num是等于getNum返回的值 -->
			</td>
			<%
					}
				}
			%>
		</table>
	</div>
</body>
</html>

----------------------------------------------------------------------

这一部分,点击查询按钮后,根据action的值,查找web.xml

		<form action="SearchName" method="post">	<!-- Servlet名字为SearchName -->
		请输入姓名:<input type="text" name="name" >
		<input type="submit" value="查询" />
		</form>

---------------------------------------------------

web.xml中

        <servlet>
  	<servlet-name>SearchName</servlet-name>
  	<servlet-class>servlet.SearchName</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>SearchName</servlet-name>
  	<url-pattern>/SearchName</url-pattern>
  </servlet-mapping>

url-pattern等于action的值

所以对应的servlet-name名字为SearchName,而名字为SearchName的<servlet>调用的servlet为servlet包中的SearchName

-------------------------------------------------------------

SearchName.java

package servlet;
import java.io.*;
import java.sql.*;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;
import dao.AddMessageDao;
import bean.AddMessageBean;
public class SearchName extends HttpServlet{
	private static final long serialVersionUID = 1L;  
	  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        this.doPost(req, resp);  
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {
    	//设置编码,防止乱码
    	req.setCharacterEncoding("UTF-8"); 
    	resp.setCharacterEncoding("UTF-8");
    	//将JSP中name="name"的值赋给Sring类型的name
    	String name=req.getParameter("name");
    	//新建一个AddMessageDao
    	AddMessageDao dao = new AddMessageDao();
    	String sql = "select * from stumessage where name ='";
        sql += name + "'";
        //创建一个列表,调用dao的SearchName
    	List<AddMessageBean> SearchName = dao.SearchName(name);  
        //保存查询的信息用setAttribute将值给SearchName    
        req.setAttribute("SearchName", SearchName);
        //转发请求  
        req.getRequestDispatcher("/SearchName.jsp").forward(req, resp);  
    }
}

--------------------------------------------------------

AddMessageDao中的SearchName函数

    public List<AddMessageBean> SearchName(String Name) {  
  	  
        Connection conn = null;  
        Statement stmt = null;  
        List<AddMessageBean> classList= new ArrayList<AddMessageBean>();  
        try {  
            // 获取连接  
        	conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","root"); 
            // 整理一条SQL语句  
            String sql = "select * from stumessage where name ='";
            sql += Name + "'";
            // 创建执行sql的对象 
            stmt = conn.createStatement();  
            //执行sql语句  
            ResultSet rs =stmt.executeQuery(sql);  
            //遍历结果集  
            while(rs.next()){  
                String num =rs.getString("num");  
                String name=rs.getString("name");  
                String sex =rs.getString("sex");  
                String college =rs.getString("college");  
                String year =rs.getString("year");  
                String major =rs.getString("major");  
                String poor =rs.getString("poor");  
                AddMessageBean bean = new AddMessageBean();  
                bean.setNum(num);  
                bean.setName(name);
                bean.setSex(sex);
                bean.setCollege(college);
                bean.setYear(year);
                bean.setMajor(major);
                bean.setPoor(poor);  
                classList.add(bean);  
            }  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
        return classList;  
    }

然后按照姓名查询就可以了


-----------------------------------------------------------------------

说一下,重点

SearchName.java的req.setAttribute("SearchName", SearchName);是将值赋给名字为"SearchName"

然后在页面SearchName.jsp的request.getAttribute("SearchName");就是将"SearchName"名字的值取出来

-------------------------------------------------------

然后下面几张我就把其他查询的代码都贴出来先吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值