欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

第82节:Java中的学生管理系统

程序员文章站 2022-05-02 12:54:02
第82节:Java中的学生管理系统 学生管理系统的删除功能 删除,点击超链接,点击弹出对话框式是否进行删除,如果确定,就删除,超链接执行的是js方法,在js里访问,跳转servlet,,servlet中调用dao方法。 学生管理系统更新 fn:contains()函数 fn:contain()函数用 ......

第82节:Java中的学生管理系统

第82节:java中的学生管理系统

学生管理系统的删除功能

删除,点击超链接,点击弹出对话框式是否进行删除,如果确定,就删除,超链接执行的是js方法,在js里访问,跳转servlet,,servlet中调用dao方法。

<a href="#" onclick="dodelete(${stu.sid})">删除</a>
<script type="text/javascript">

    function dodelete(sid) {
        // 弹出对话框,点击确定,请求servlet
        var flag = confirm("是否确定删除?");
        if(flag){
            //访问servlet
            //window.location.href="deleteservlet?sid="+sid;
            location.href="deleteservlet?sid="+sid;
        }
    }
</script>
package com.dashucoding.servlet;

import java.io.ioexception;
import java.sql.sqlexception;

import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import com.dashucoding.service.studentservice;
import com.dashucoding.service.impl.studentserviceimpl;

/**
 * 用于处理删除学生
 */
public class deleteservlet extends httpservlet {

    protected void doget(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
        try {
            int sid = integer.parseint(request.getparameter("sid"));
            // system.out.println("sid="+sid);
            // 执行删除
            studentservice service = new studentserviceimpl();
            service.delete(sid);
            // 跳转到列表页
            request.getrequestdispatcher("studentlistservlet").forward(request, response);
        } catch (sqlexception e) {
            // todo auto-generated catch block
            e.printstacktrace();
        }

    }

    protected void dopost(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
        doget(request, response);
    }

}
package com.dashucoding.service.impl;

import java.sql.sqlexception;
import java.util.list;

import com.dashucoding.dao.studentdao;
import com.dashucoding.dao.impl.studentdaoimpl;
import com.dashucoding.domain.student;
import com.dashucoding.service.studentservice;
/*
 * 这是学生业务实现
 * */
public class studentserviceimpl implements studentservice{

    @override
    public list<student> findall() throws sqlexception {
        studentdao dao = new studentdaoimpl();
        return dao.findall();
    }

    @override
    public void insert(student student) throws sqlexception {
        // todo auto-generated method stub
        studentdao dao = new studentdaoimpl();
        dao.insert(student);
    }

    @override
    public void delete(int sid) throws sqlexception {
        // todo auto-generated method stub
        studentdao dao = new studentdaoimpl();
        dao.delete(sid);
    }
    
    
}
package com.dashucoding.dao;

import java.sql.sqlexception;
import java.util.list;

import com.dashucoding.domain.student;

/*
 * 这是针对学生表的数据访问
 * 
 * */
public interface studentdao {

    /*
     * 查询所有学生 list<student>
     */
    list<student> findall() throws sqlexception;

    void insert(student student) throws sqlexception;

    // sid根据id删除学生
    void delete(int sid) throws sqlexception;
}
package com.dashucoding.dao.impl;

import java.sql.connection;
import java.sql.sqlexception;
import java.util.list;

import org.apache.commons.dbutils.queryrunner;
import org.apache.commons.dbutils.handlers.beanlisthandler;

import com.dashucoding.dao.studentdao;
import com.dashucoding.domain.student;
import com.dashucoding.util.jdbcutil02;

/*
 *这是studentdao的实现,针对前面定义的规范,做出具体的实现
 * */
public class studentdaoimpl implements studentdao {
    /*
     * 查询所有学生
     */
    @override
    public list<student> findall() throws sqlexception {
        queryrunner runner = new queryrunner(jdbcutil02.getdatasource());
        return runner.query("select * from stu", new beanlisthandler<student>(student.class));
    }

    @override
    public void insert(student student) throws sqlexception {
        // todo auto-generated method stub
        queryrunner runner = new queryrunner(jdbcutil02.getdatasource());
        runner.update("insert into stu values(null, ?,?,?,?,?,?)", 
                student.getsname(), 
                student.getgender(),
                student.getphone(), 
                student.getbirthday(), 
                student.gethobby(), 
                student.getinfo()
                );
    }

    @override
    public void delete(int sid) throws sqlexception {
        // todo auto-generated method stub

        queryrunner runner = new queryrunner(jdbcutil02.getdatasource());
        runner.update("delete from stu where sid=?", sid);
        
    }

}

学生管理系统更新

fn:contains()函数

fn:contain()函数用于确定一个字符串是否包含指定的子串,函数的语法格式如下:

<c:if test="${fn:contains()"></c:if>
fn:contains
tests if an input string contains the specified substring.

更新,点击列表上的按钮进行更新,跳转editservlet,根据id查询这个学生的所有信息,跳转到更新的页面,显示在浏览器,修改后提交到updateservlet,提交数据要带id,获取数据,调用service和调用dao。

代码案例:

第82节:Java中的学生管理系统

<%@ page language="java" contenttype="text/html; charset=utf-8"
    pageencoding="utf-8"%>
    
 <%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>
<!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>

<script type="text/javascript">

    function dodelete(sid) {
        // 弹出对话框,点击确定,请求servlet
        var flag = confirm("是否确定删除?");
        if(flag){
            //访问servlet
            //window.location.href="deleteservlet?sid="+sid;
            location.href="deleteservlet?sid="+sid;
        }
    }
</script>

</head>
<body>
    <form action="searchstudentservlet" method="post">
        <table border="1" width="700">
        
            <tr >
                <td colspan="8">
                    
                    按姓名查询:<input type="text" name="sname"/>
                    &nbsp;
                    按性别查询:<select name="sgender">
                                <option value="">--请选择--
                                <option value="男">男
                                <option value="女">女
                              </select>
                    &nbsp;&nbsp;&nbsp;
                    <input type="submit" value="查询">
                    &nbsp;&nbsp;&nbsp;
                    <a href="add.jsp">添加</a>
                </td>
            </tr>
        
          <tr align="center">
            <td>编号</td>
            <td>姓名</td>
            <td>性别</td>
            <td>电话</td>
            <td>生日</td>
            <td>爱好</td>
            <td>简介</td>
            <td>操作</td>
          </tr>
          
              <c:foreach items="${list }" var="stu">
                  <tr align="center">
                    <td>${stu.sid }</td>
                    <td>${stu.sname }</td>
                    <td>${stu.gender }</td>
                    <td>${stu.phone }</td>
                    <td>${stu.birthday }</td>
                    <td>${stu.hobby }</td>
                    <td>${stu.info }</td>
                    <td><a href="editservlet?sid=${stu.sid }">更新</a>   <a href="#" onclick="dodelete(${stu.sid})">删除</a></td>
                  </tr>
              </c:foreach>
          </table>
      </form>
</body>
</html>
package com.dashucoding.servlet;

import java.io.ioexception;
import java.sql.sqlexception;

import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import com.dashucoding.domain.student;
import com.dashucoding.service.studentservice;
import com.dashucoding.service.impl.studentserviceimpl;

/**
 * 处理单个学生的更新,查询学生的信息,跳转到更新的页面
 */
public class editservlet extends httpservlet {
    protected void doget(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
        try {
            // 接收id
            int sid = integer.parseint(request.getparameter("sid"));
            // 查询学生数据
            studentservice service = new studentserviceimpl();
            student stu = service.findstudentbyid(sid);

            // 显示数据
            // 存储数据
            request.setattribute("stu", stu);
            // 跳转
            request.getrequestdispatcher("edit.jsp").forward(request, response);

        } catch (sqlexception e) {
            // todo auto-generated catch block
            e.printstacktrace();
        }

    }

    /**
     * @see httpservlet#dopost(httpservletrequest request, httpservletresponse
     *      response)
     */
    protected void dopost(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
        // todo auto-generated method stub
        doget(request, response);
    }

}
package com.dashucoding.dao;

import java.sql.sqlexception;
import java.util.list;

import com.dashucoding.domain.student;

/*
 * 这是针对学生表的数据访问
 * 
 * */
public interface studentdao {

    /*
     * 查询所有学生 list<student>
     */
    list<student> findall() throws sqlexception;

    void insert(student student) throws sqlexception;

    // sid根据id删除学生
    void delete(int sid) throws sqlexception;

    // 根据id查询单个学生对象
    student findstudentbyid(int sid) throws sqlexception;

    // 更新学生信息
    void update(student student) throws sqlexception;
}
package com.dashucoding.dao.impl;

import java.sql.connection;
import java.sql.sqlexception;
import java.util.list;

import org.apache.commons.dbutils.queryrunner;
import org.apache.commons.dbutils.handlers.beanhandler;
import org.apache.commons.dbutils.handlers.beanlisthandler;

import com.dashucoding.dao.studentdao;
import com.dashucoding.domain.student;
import com.dashucoding.util.jdbcutil02;

/*
 *这是studentdao的实现,针对前面定义的规范,做出具体的实现
 * */
public class studentdaoimpl implements studentdao {
    /*
     * 查询所有学生
     */
    @override
    public list<student> findall() throws sqlexception {
        queryrunner runner = new queryrunner(jdbcutil02.getdatasource());
        return runner.query("select * from stu", new beanlisthandler<student>(student.class));
    }

    @override
    public void insert(student student) throws sqlexception {
        // todo auto-generated method stub
        queryrunner runner = new queryrunner(jdbcutil02.getdatasource());
        runner.update("insert into stu values(null, ?,?,?,?,?,?)", 
                student.getsname(), 
                student.getgender(),
                student.getphone(), 
                student.getbirthday(), 
                student.gethobby(), 
                student.getinfo()
                );
    }

    @override
    public void delete(int sid) throws sqlexception {
        // todo auto-generated method stub

        queryrunner runner = new queryrunner(jdbcutil02.getdatasource());
        runner.update("delete from stu where sid=?", sid);
        
    }

    @override
    public student findstudentbyid(int sid) throws sqlexception {
        // todo auto-generated method stub
        queryrunner runner = new queryrunner(jdbcutil02.getdatasource());
        
        return runner.query("select * from stu where sid = ?", new beanhandler<student>(student.class), sid);
    }

    @override
    public void update(student student) throws sqlexception {
        // todo auto-generated method stub
        queryrunner runner = new queryrunner(jdbcutil02.getdatasource());
        runner.update("update stu set sname=?, gender=?, phone=?, birthday=?, hobby=?, info=? where sid=?",
                student.getsname(), 
                student.getgender(),
                student.getphone(), 
                student.getbirthday(), 
                student.gethobby(), 
                student.getinfo(),
                student.getsid());
    }

}
package com.dashucoding.service;

import java.sql.sqlexception;
import java.util.list;

import com.dashucoding.domain.student;

/*
 * 这是学生的业务处理规范
 * */
public interface studentservice {

    /*
     * 查询所有学生 list<student>
     */
    list<student> findall() throws sqlexception;

    void insert(student student) throws sqlexception;

    // sid根据id删除学生
    void delete(int sid) throws sqlexception;

    // 根据id查询单个学生对象
    student findstudentbyid(int sid) throws sqlexception;

    // 更新学生信息
    void update(student student) throws sqlexception;
}
package com.dashucoding.service.impl;

import java.sql.sqlexception;
import java.util.list;

import com.dashucoding.dao.studentdao;
import com.dashucoding.dao.impl.studentdaoimpl;
import com.dashucoding.domain.student;
import com.dashucoding.service.studentservice;

/*
 * 这是学生业务实现
 * */
public class studentserviceimpl implements studentservice {

    @override
    public list<student> findall() throws sqlexception {
        studentdao dao = new studentdaoimpl();
        return dao.findall();
    }

    @override
    public void insert(student student) throws sqlexception {
        // todo auto-generated method stub
        studentdao dao = new studentdaoimpl();
        dao.insert(student);
    }

    @override
    public void delete(int sid) throws sqlexception {
        // todo auto-generated method stub
        studentdao dao = new studentdaoimpl();
        dao.delete(sid);
    }

    @override
    public student findstudentbyid(int sid) throws sqlexception {
        // todo auto-generated method stub
        studentdao dao = new studentdaoimpl();
        return dao.findstudentbyid(sid);
    }

    @override
    public void update(student student) throws sqlexception {
        // todo auto-generated method stub
        studentdao dao = new studentdaoimpl();
        dao.update(student);
    }

}
package com.dashucoding.servlet;

import java.io.ioexception;
import java.text.simpledateformat;
import java.util.arrays;
import java.util.date;

import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import com.dashucoding.domain.student;
import com.dashucoding.service.studentservice;
import com.dashucoding.service.impl.studentserviceimpl;

/**
 * servlet implementation class updateservlet
 */
public class updateservlet extends httpservlet {

    protected void doget(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
        request.setcharacterencoding("utf-8");
        try {
            // 1. 获取客户端提交上来的数据
            int sid = integer.parseint(request.getparameter("sid"));
            string sname = request.getparameter("sname");
            string gender = request.getparameter("gender");
            string phone = request.getparameter("phone");
            string birthday = request.getparameter("birthday"); 
            string info = request.getparameter("info");
            // string hobby = request.getparameter("hobby");
            string[] h = request.getparametervalues("hobby");

            string hobby = arrays.tostring(h);
            hobby = hobby.substring(1, hobby.length() - 1);
            // 2. 添加到数据库

            date date = new simpledateformat("yyyy-mm-dd").parse(birthday);
            student student = new student(sid, sname, gender, phone, hobby, info, date);

            // 2. 更新数据库数据
            studentservice service = new studentserviceimpl();
            service.update(student);

            // 3. 跳转界面
            request.getrequestdispatcher("studentlistservlet").forward(request, response);
        } catch (exception e) {
            e.printstacktrace();
        }
    }

    protected void dopost(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
        // todo auto-generated method stub
        doget(request, response);
    }

}
package com.dashucoding.servlet;

import java.io.ioexception;
import java.sql.sqlexception;
import java.util.list;

import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import com.dashucoding.dao.studentdao;
import com.dashucoding.dao.impl.studentdaoimpl;
import com.dashucoding.domain.student;
import com.dashucoding.service.studentservice;
import com.dashucoding.service.impl.studentserviceimpl;

public class studentlistservlet extends httpservlet {

    protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        try {
            // 查询所有的学生
            studentservice service = new studentserviceimpl();
            list<student> list = service.findall();
            // 把数据存储到作用域中
            request.setattribute("list", list);
            
            // 跳转页面
            request.getrequestdispatcher("list.jsp").forward(request,response);
        } catch (sqlexception e) {
            // todo auto-generated catch block
            e.printstacktrace();
        }
    }

    protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        doget(request, response);
    }

}
<%@ page language="java" contenttype="text/html; charset=utf-8"
    pageencoding="utf-8"%>
    
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
 
<!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>

</head>


<body>

<h3>更新学生页面</h3>

<form method="post" action="updateservlet">
    <input type="hidden" name="sid" value="${stu.sid }">
  <table border="1" width="600">
  <tr>
    <td>姓名</td>
    <td><input type="text" name="sname" value="${stu.sname }"></td>
  </tr>
  <tr>
    <td>性别</td>
    <td>
        <input type="radio" name="gender" value="男" <c:if test="${stu.gender == '男'}">checked</c:if>>男
        <input type="radio" name="gender" value="女" <c:if test="${stu.gender == '女'}">checked</c:if>>女
    </td>
  </tr>
  <tr>
    <td>电话</td>
    <td><input type="text" name="phone" value="${stu.phone }"></td>
  </tr>
  <tr>
    <td>生日</td>
    <td><input type="text" name="birthday" value="${stu.birthday }"></td>
  </tr>
  <tr>
    <td>爱好</td>
    
    
    <td>
        <input type="checkbox" name="hobby" value="游泳" <c:if test="${fn:contains(stu.hobby,'游泳') }">checked</c:if>>游泳
        <input type="checkbox" name="hobby" value="篮球" <c:if test="${fn:contains(stu.hobby,'篮球') }">checked</c:if>>篮球
        <input type="checkbox" name="hobby" value="足球" <c:if test="${fn:contains(stu.hobby,'足球') }">checked</c:if>>足球
        <input type="checkbox" name="hobby" value="看书" <c:if test="${fn:contains(stu.hobby,'看书') }">checked</c:if>>看书
        <input type="checkbox" name="hobby" value="写字" <c:if test="${fn:contains(stu.hobby,'写字') }">checked</c:if>>写字
    
    </td>
  </tr>
  <tr>
    <td>简介</td>
    <td><textarea name="info" rows="3" cols="20">${stu.info }</textarea></td>
  </tr>
  <tr>
    <td colspan="2"> <input type="submit" value="更新"> </td>
  </tr>
  </table>
   </form>
</body>
</html>

第82节:Java中的学生管理系统

进行模糊查询

查询结果
第82节:Java中的学生管理系统

第82节:Java中的学生管理系统

<%@ page language="java" contenttype="text/html; charset=utf-8"
    pageencoding="utf-8"%>
    
 <%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>
<!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>

<script type="text/javascript">

    function dodelete(sid) {
        // 弹出对话框,点击确定,请求servlet
        var flag = confirm("是否确定删除?");
        if(flag){
            //访问servlet
            //window.location.href="deleteservlet?sid="+sid;
            location.href="deleteservlet?sid="+sid;
        }
    }
</script>

</head>
<body>
    <form action="searchstudentservlet" method="post">
        <table border="1" width="700">
        
            <tr >
                <td colspan="8">
                    
                    按姓名查询:<input type="text" name="sname"/>
                    &nbsp;
                    按性别查询:<select name="sgender">
                                <option value="">--请选择--
                                <option value="男">男
                                <option value="女">女
                              </select>
                    &nbsp;&nbsp;&nbsp;
                    <input type="submit" value="查询">
                    &nbsp;&nbsp;&nbsp;
                    <a href="add.jsp">添加</a>
                </td>
            </tr>
        
          <tr align="center">
            <td>编号</td>
            <td>姓名</td>
            <td>性别</td>
            <td>电话</td>
            <td>生日</td>
            <td>爱好</td>
            <td>简介</td>
            <td>操作</td>
          </tr>
          
              <c:foreach items="${list }" var="stu">
                  <tr align="center">
                    <td>${stu.sid }</td>
                    <td>${stu.sname }</td>
                    <td>${stu.gender }</td>
                    <td>${stu.phone }</td>
                    <td>${stu.birthday }</td>
                    <td>${stu.hobby }</td>
                    <td>${stu.info }</td>
                    <td><a href="editservlet?sid=${stu.sid }">更新</a>   <a href="#" onclick="dodelete(${stu.sid})">删除</a></td>
                  </tr>
              </c:foreach>
          </table>
      </form>
</body>
</html>
package com.dashucoding.util;

public class textutils {

    /**
     * 判断某一个字符串是否为空。
     * 
     * @param s
     * @return
     */
    public static boolean isempty(charsequence s) {
        return s == null || s.length() == 0;
    }
}
package com.dashucoding.dao;

import java.sql.sqlexception;
import java.util.list;

import com.dashucoding.domain.student;

/*
 * 这是针对学生表的数据访问
 * 
 * */
public interface studentdao {

    // 根据姓名或性别,查询
    list<student> searchstudent(string sname, string sgender) throws sqlexception;

    /*
     * 查询所有学生 list<student>
     */
    list<student> findall() throws sqlexception;

    void insert(student student) throws sqlexception;

    // sid根据id删除学生
    void delete(int sid) throws sqlexception;

    // 根据id查询单个学生对象
    student findstudentbyid(int sid) throws sqlexception;

    // 更新学生信息
    void update(student student) throws sqlexception;
}
package com.dashucoding.dao.impl;

import java.sql.connection;
import java.sql.sqlexception;
import java.util.arraylist;
import java.util.list;

import org.apache.commons.dbutils.queryrunner;
import org.apache.commons.dbutils.handlers.beanhandler;
import org.apache.commons.dbutils.handlers.beanlisthandler;

import com.dashucoding.dao.studentdao;
import com.dashucoding.domain.student;
import com.dashucoding.util.jdbcutil02;
import com.dashucoding.util.textutils;

/*
 *这是studentdao的实现,针对前面定义的规范,做出具体的实现
 * */
public class studentdaoimpl implements studentdao {
    /*
     * 查询所有学生
     */
    @override
    public list<student> findall() throws sqlexception {
        queryrunner runner = new queryrunner(jdbcutil02.getdatasource());
        return runner.query("select * from stu", new beanlisthandler<student>(student.class));
    }

    @override
    public void insert(student student) throws sqlexception {
        // todo auto-generated method stub
        queryrunner runner = new queryrunner(jdbcutil02.getdatasource());
        runner.update("insert into stu values(null, ?,?,?,?,?,?)", 
                student.getsname(), 
                student.getgender(),
                student.getphone(), 
                student.getbirthday(), 
                student.gethobby(), 
                student.getinfo()
                );
    }

    @override
    public void delete(int sid) throws sqlexception {
        // todo auto-generated method stub

        queryrunner runner = new queryrunner(jdbcutil02.getdatasource());
        runner.update("delete from stu where sid=?", sid);
        
    }

    @override
    public student findstudentbyid(int sid) throws sqlexception {
        // todo auto-generated method stub
        queryrunner runner = new queryrunner(jdbcutil02.getdatasource());
        
        return runner.query("select * from stu where sid = ?", new beanhandler<student>(student.class), sid);
    }

    @override
    public void update(student student) throws sqlexception {
        // todo auto-generated method stub
        queryrunner runner = new queryrunner(jdbcutil02.getdatasource());
        runner.update("update stu set sname=?, gender=?, phone=?, birthday=?, hobby=?, info=? where sid=?",
                student.getsname(), 
                student.getgender(),
                student.getphone(), 
                student.getbirthday(), 
                student.gethobby(), 
                student.getinfo(),
                student.getsid());
    }

    // 模糊查询
    @override
    public list<student> searchstudent(string sname, string sgender) throws sqlexception {
        // todo auto-generated method stub
        
        /*system.out.println(sname + sgender);*/
        
        queryrunner runner = new queryrunner(jdbcutil02.getdatasource());
        
        /*
         * string sql = "select * from stu where sname=? or sgender=?";
         * select * from stu where sname like ?;
         * select * from stu where gender = ?;
         * select * from stu where sname like ? and gender = ?;
         * 如果两个都没有就查询所有
         * sql = "select * from stu"
         * if(姓名){
         *  sql = sql + "where sname like ?";
         * }
         * if(性别){
         *  sql = sql + "where gender = ?";
         * }
         * 
         * string sql = "select * from stu where 1=1";
         * if(姓名){
         *  sql = sql + " and sname like ? ";
         * }
         * if(性别){
         *  sql = sql + " and gender = ? ";
         * }
         * */
        
        string sql = "select * from stu where 1=1";
        
        list<string> list = new arraylist<string>();
        
        if(!textutils.isempty(sname)) {
            sql = sql + " and sname like ? ";
            list.add("%"+sname+"%");
        }
        
        if(!textutils.isempty(sgender)) {
            sql = sql + " and gender = ? ";
            list.add(sgender);
        }
        /*list.toarray()*/
        
        return runner.query(sql, new beanlisthandler<student>(student.class),list.toarray());
        
    }

}
package com.dashucoding.service;

import java.sql.sqlexception;
import java.util.list;

import com.dashucoding.domain.student;

/*
 * 这是学生的业务处理规范
 * */
public interface studentservice {
    // 根据姓名或性别,查询
    list<student> searchstudent(string sname, string sgender) throws sqlexception;

    /*
     * 查询所有学生 list<student>
     */
    list<student> findall() throws sqlexception;

    void insert(student student) throws sqlexception;

    // sid根据id删除学生
    void delete(int sid) throws sqlexception;

    // 根据id查询单个学生对象
    student findstudentbyid(int sid) throws sqlexception;

    // 更新学生信息
    void update(student student) throws sqlexception;
}
package com.dashucoding.service.impl;

import java.sql.sqlexception;
import java.util.list;

import com.dashucoding.dao.studentdao;
import com.dashucoding.dao.impl.studentdaoimpl;
import com.dashucoding.domain.student;
import com.dashucoding.service.studentservice;

/*
 * 这是学生业务实现
 * */
public class studentserviceimpl implements studentservice {

    @override
    public list<student> findall() throws sqlexception {
        studentdao dao = new studentdaoimpl();
        return dao.findall();
    }

    @override
    public void insert(student student) throws sqlexception {
        // todo auto-generated method stub
        studentdao dao = new studentdaoimpl();
        dao.insert(student);
    }

    @override
    public void delete(int sid) throws sqlexception {
        // todo auto-generated method stub
        studentdao dao = new studentdaoimpl();
        dao.delete(sid);
    }

    @override
    public student findstudentbyid(int sid) throws sqlexception {
        // todo auto-generated method stub
        studentdao dao = new studentdaoimpl();
        return dao.findstudentbyid(sid);
    }

    @override
    public void update(student student) throws sqlexception {
        // todo auto-generated method stub
        studentdao dao = new studentdaoimpl();
        dao.update(student);
    }

    @override
    public list<student> searchstudent(string sname, string sgender) throws sqlexception {
        // todo auto-generated method stub
        studentdao dao = new studentdaoimpl();
        return dao.searchstudent(sname, sgender);
    }

}
package com.dashucoding.servlet;

import java.io.ioexception;
import java.sql.sqlexception;
import java.util.list;

import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import com.dashucoding.domain.student;
import com.dashucoding.service.studentservice;
import com.dashucoding.service.impl.studentserviceimpl;

/**
 * servlet implementation class searchstudentservlet
 */
public class searchstudentservlet extends httpservlet {

    protected void doget(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
        request.setcharacterencoding("utf-8");
        try {
            // 取到了要查询的关键数据
            string sname = request.getparameter("sname");
            string sgender = request.getparameter("sgender");

            // 找service查询
            studentservice service = new studentserviceimpl();
            list<student> list = service.searchstudent(sname, sgender);
            
            /*for(student student : list) {
                system.out.println("stu=" + student);
            }*/
            
            request.setattribute("list", list);
            // 跳转界面
            request.getrequestdispatcher("list.jsp").forward(request, response);
        } catch (sqlexception e) {
            // todo auto-generated catch block
            e.printstacktrace();
        }

    }

    protected void dopost(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
        // todo auto-generated method stub
        doget(request, response);
    }

}
package com.dashucoding.servlet;

import java.io.ioexception;
import java.sql.sqlexception;
import java.util.list;

import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import com.dashucoding.dao.studentdao;
import com.dashucoding.dao.impl.studentdaoimpl;
import com.dashucoding.domain.student;
import com.dashucoding.service.studentservice;
import com.dashucoding.service.impl.studentserviceimpl;

public class studentlistservlet extends httpservlet {

    protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        try {
            // 查询所有的学生
            studentservice service = new studentserviceimpl();
            list<student> list = service.findall();
            // 把数据存储到作用域中
            request.setattribute("list", list);
            
            // 跳转页面
            request.getrequestdispatcher("list.jsp").forward(request,response);
        } catch (sqlexception e) {
            // todo auto-generated catch block
            e.printstacktrace();
        }
    }

    protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        doget(request, response);
    }

}

结言

好了,欢迎在留言区留言,与大家分享你的经验和心得。

感谢你学习今天的内容,如果你觉得这篇文章对你有帮助的话,也欢迎把它分享给更多的朋友,感谢。

达叔小生:往后余生,唯独有你
you and me, we are family !
90后帅气小伙,良好的开发习惯;独立思考的能力;主动并且善于沟通
简书博客: 达叔小生

结语

  • 下面我将继续对 其他知识 深入讲解 ,有兴趣可以继续关注
  • 小礼物走一走 or 点赞