第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。
代码案例:
<%@ 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"/>
按性别查询:<select name="sgender">
<option value="">--请选择--
<option value="男">男
<option value="女">女
</select>
<input type="submit" value="查询">
<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>
进行模糊查询
查询结果
<%@ 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"/>
按性别查询:<select name="sgender">
<option value="">--请选择--
<option value="男">男
<option value="女">女
</select>
<input type="submit" value="查询">
<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后帅气小伙,良好的开发习惯;独立思考的能力;主动并且善于沟通
简书博客: 达叔小生
https://www.jianshu.com/u/c785ece603d1
结语
- 下面我将继续对 其他知识 深入讲解 ,有兴趣可以继续关注
- 小礼物走一走 or 点赞
第82节:Java中的学生管理系统的更多相关文章
-
第83节:Java中的学生管理系统分页功能
第83节:Java中的学生管理系统分页功能 分页功能一般可以做成两种,一种是物理分页,另一种是逻辑分页.这两种功能是有各自的特点的,物理分页是查询的时候,对数据库进行访问,只是查一页数据就进行返回,其 ...
-
第80节:Java中的MVC设计模式
第80节:Java中的MVC设计模式 前言 了解java中的mvc模式.复习以及回顾! 事务,设置自动连接提交关闭. setAutoCommit(false); conn.commit(); conn ...
-
第69节:Java中数据库的多表操作
第69节:Java中数据库的多表操作 前言 学习数据库的多表操作,去电商行业做项目吧!!! 达叔,理工男,简书作者&全栈工程师,感性理性兼备的写作者,个人独立开发者,我相信你也可以!阅读他的文 ...
-
第68节:Java中的MYSQL运用从小白到大牛
第68节:Java中的MYSQL运用从小白到大牛 前言 学习java必备要求,学会运用!!! 常见关系化数据库 BootStrap是轻量级开发响应式页面的框架,全局css组件,js插件.栅格系统是将页 ...
-
第79节:Java中一些要点
第79节:Java中一些要点 前言 一些知识点忘了没,可以通过一个点引出什么内容呢?做出自己的思维导图,看看自己到了哪一步了呢 内容 如果有人问jre,jdk,jvm是什么,你怎么回答呢? jre的英 ...
-
第78节:Java中的网络编程(上)
第78节:Java中的网络编程(上) 前言 网络编程涉及ip,端口,协议,tcp和udp的了解,和对socket通信的网络细节. 网络编程 OSI开放系统互连 网络编程指IO加网络 TCP/IP模型: ...
-
第77节:Java中的事务和数据库连接池和DBUtiles
第77节:Java中的事务和数据库连接池和DBUtiles 前言 看哭你,字数:8803,承蒙关照,谢谢朋友点赞! 事务 Transaction事务,什么是事务,事务是包含一组操作,这组操作里面包含许 ...
-
第76节:Java中的基础知识
第76节:Java中的基础知识 设置环境,安装操作系统,安装备份,就是镜像,jdk配置环境,eclipse下载解压即可使用,下载tomcat 折佣动态代理解决网站的字符集编码问题 使用request. ...
-
第71节:Java中HTTP和Servlet
第71节:Java中HTTP和Servlet 前言 哭着也要看完!!!字数: 学习xml和TomCat 会写xml,看懂xml 解析对象 SAXReader reader = new SAXReade ...
随机推荐
-
关于mysql数据库在输入密码后,滴的一声直接退出界面的解决办法
转自:http://www.2cto.com/database/201412/361751.html 网上搜索到的解决办法: 1.找到mysql安装目录下的bin目录路径.2.打开cmd,进入到bin ...
-
tail命令
tail命令用来取文件后几行,默认显示后10行.有多个FILE,每个都带有一个头文件名称. 语法: tail [OPTION]... [FILE]... 选项: -n#:取文件后#行,n可省略: -c ...
-
数字是否可以被3和5同时整除,use if and % (21.9.2017)
#include <stdio.h> int main(){ int a; //a是所输入的数字 printf("输入数字: "); scanf("%d&qu ...
-
python JoinableQueue在生产者消费者项目中的简单应用
class multiprocessing.JoinableQueue([maxsize]) JoinableQueue, a Queue subclass, is a queue which add ...
-
在思科路由器上配置AAA实验(Cisco PT)
1.拓扑图 Addressing Table 地址表 Device Interface IP Address Subnet Mask R1 Fa0/0 192.168.1.1 ...
-
【类与对象】--------java基础学习第六天
类与对象 1. 对于面向对象的开发来讲也分为三个过程: OOA(面向对象分析) OOD(面向对象设计) OOP(面向对象编程) 2. 面向对象的基本特征 2.1. 封装:保护内部操作(属性,方法)不被 ...
-
MongoDB在Windows系统下的安装和启动
版本选择MongoDB的版本命名规范如:x.y.z: y为奇数时表示当前版本为开发版,如:2.3.0.2.1.1: y为偶数时表示当前版本为稳定版,如:2.0.1.2.2.0: 目前官网上最新的版本为 ...
-
CentOS 性能监测命令
1.实时监测命令(watch) -d 高亮显示变化 -n 间隔多久(s) 执行后面的command #每隔1秒显示空间使用情况并列出当前目录下的列表信息 EX:watch -d -n 1 'df -h ...
-
Flask web开发之路十二
ge请求和post请求 ### get请求和post请求:1. get请求: * 使用场景:如果只对服务器获取数据,并没有对服务器产生任何影响,那么这时候使用get请求. * 传参:get请求传参是放 ...
-
前端开发利器自定义Iconfont图标
前端开发难免遇到很多地方需要图片来展示,以往我们都会使用img.background.font文件实现图片,本人使用bootstrap,但由于前端比较火的bootstrap的font库太少不能满足我们 ...