简单无UI学生管理系统

学生管理系统

功能

  • 添加
    简单无UI学生管理系统_第1张图片

  • 删除
    简单无UI学生管理系统_第2张图片

  • 更新

    • 在form表单里添加一个隐藏的id文本框
    • 按id更新
      简单无UI学生管理系统_第3张图片
  • 模糊查询

    • 只按姓名查询
    • 只按性别查询
    • 两者兼有
      简单无UI学生管理系统_第4张图片
  • 分页

    分页属于一个业务,里面包含多个逻辑单元

    class PageBean{
    	当前页 int currentPage
    	总页数	int totalPage
    	总记录数	int totalSize
    	每页记录数 int pageSize
    	该页的学生集合 List
    }
    

简单无UI学生管理系统_第5张图片

后台实现的代码

  • 工具包 com.yll.util
  1. JDBCUtil.java

DBUtils 用于数据库连接

package com.yll.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JDBCUtil {
	static ComboPooledDataSource dataSource = null;
	static {
		dataSource = new ComboPooledDataSource();
	}
	public static DataSource getDataSource() {
		return dataSource;
	}
	/**
	 * 获取连接对象
	 * @return
	 * @thorws SQLException
	 * */
	public static Connection getConn() throws SQLException {
		return dataSource.getConnection();
	}
	/**
	 * 释放资源
	 * @param conn
	 * @param st
	 * @param rs
	 * */
	public static void release(Connection conn,Statement st,ResultSet rs) {
		closeRs(rs);
		closeSt(st);
		closeConn(conn);
	}
	public static void release(Connection conn,Statement st) {
		closeSt(st);
		closeConn(conn);
	}
	
	public static void closeRs(ResultSet rs) {
		try {
			if(rs != null) {
				rs.close();}
		}catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}finally {
			rs = null;
		}
	}
	public static void closeSt(Statement st) {
		try {
			if(st != null) {
				st.close();}
		}catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}finally {
			st = null;
		}
	}
	public static void closeConn(Connection conn) {
		try {
			if(conn != null) {
				conn.close();}
		}catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}finally {
			conn = null;
		}
	}
}

  1. TextUtils.java

判断字符串是否为空

package com.yll.util;

public class TextUtils {
	/*
	 * 判断字符串是否为空
	 * @param s
	 * 
	 * */
	public static boolean isEmpty(CharSequence s) {
		return s == null || s.length() == 0;
	}
}

  • 包 com.yll.dao dao层
  1. StudentDao.java
package com.yll.dao;

import java.sql.SQLException;
import java.util.List;

import com.yll.domain.Student;

/* 
 * 针对学生表的数据访问
 * */
public interface StudentDao {
	/* 
	 * 现阶段用啥写啥
	 * */
	//一页显示多少条记录
	int PAGE_SIZE = 5; 
	
	/**
	 * 查询当页的学生人数
	 * @param currentPage
	 * @return
	 * @thorws SQLException
	 * */
	List<Student> findStudentByPage(int currentPage) throws SQLException;
	
	/*
	 * 查询所有学生
	 * @ return List
	 * */
	List<Student> findAll() throws SQLException;
	
	/**
	 * 添加学生
	 * @param stu 需要添加到数据库的学生对象
	 * @throws SQLException
	 * */
	void insert(Student stu) throws SQLException;
	
	/**
	 * 根据id删除学生
	 * @param sid
	 * @throws SQLException
	 * */
	void delete(int sid) throws SQLException;
	
	/** 
	  *  根据id查询单个学生对象
	 * @param id
	 * @throws SQLException
	 * */
	Student findStudentById(int sid) throws SQLException;
	
	/**
	 * 模糊查询,根据姓名或者性别或者都有
	 *@param sname
	 *@param sgender
	 *@return List
	 *@throws SQLException
	 * */
	List<Student> searchStudent(String sname,String sgender) throws SQLException;
	
	/**
	 * 更新学生信息
	 * @param stu 需要更新的学生数据
	 * @throws SQLException
	 * */
	void update (Student stu) throws SQLException;
	
	/**
	 * 查询总记录
	 * @throws SQLException
	 * */
	int findCount() throws SQLException;
}

  • 包 com.yll.dao.impl dao 功能实现
  1. StudentDaoImpl.java
package com.yll.dao.impl;

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 org.apache.commons.dbutils.handlers.ScalarHandler;

import com.yll.dao.StudentDao;
import com.yll.domain.Student;
import com.yll.util.JDBCUtil;
import com.yll.util.TextUtils;

public class StudentDaoImpl implements StudentDao{

	/**
	 * 查询所有学生
	 * */
	@Override
	public List<Student> findAll() throws SQLException {
		QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
		return runner.query("select * from stu;", new BeanListHandler<Student>(Student.class));
	}

	/**
	 * 插入
	 * */
	@Override
	public void insert(Student stu) throws SQLException {
		QueryRunner Runner = new QueryRunner(JDBCUtil.getDataSource());
		Runner.update("insert into stu values(null, ?, ?, ?, ?, ?, ?)",
				stu.getSname(),stu.getGender(),stu.getPhone(),stu.getBirthday(),
				stu.getHobby(),stu.getInfo()
				);
	}
	
	/**
	 * 删除
	 * */
	@Override
	public void delete(int sid) throws SQLException {
		QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
		runner.update("delete from stu where sid = ?;",sid);
	}
	
	/**
	 * 查询单个学生
	 * */
	@Override
	public Student findStudentById(int sid) throws SQLException {
		QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
		return runner.query("select * from stu where sid = ?",new BeanHandler<Student>(Student.class) ,sid);
	}
	
	/**
	 * 更新
	 * */
	@Override
	public void update(Student stu) throws SQLException {
		QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
		runner.update("update stu set sname=?,gender=?,phone=?,birthday=?,hobby=?,info=? where sid = ?", 
				stu.getSname(),stu.getGender(),stu.getPhone(),stu.getBirthday(),
				stu.getHobby(),stu.getInfo(),stu.getSid()
				);
	}
	/**
	 * 根据name 或 sgender查询
	 * */
	@Override
	public List<Student> searchStudent(String sname, String sgender) throws SQLException {
		QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
		String sql = "select * from stu where 1=1";
		List<String> list = new ArrayList<String> ();
		
		//判断有没有姓名,如果有,就拼到sql语句中
		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() );
	}
	/**
	 * 查询单页学生
	 * */
	@Override
	public List<Student> findStudentByPage(int currentPage) throws SQLException {
		QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
		return runner.query("select * from stu limit ? offset ?;", new BeanListHandler<Student>(Student.class), PAGE_SIZE, (currentPage-1)*PAGE_SIZE);
	}

	@Override
	public int findCount() throws SQLException {
		QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
		//用于处理平均值,总的个数
		Long result = (Long) runner.query("select COUNT(*) from stu;",new ScalarHandler());
		return result.intValue();
	}

}

  • 包com.yll.service service层
  1. StudentService.java
package com.yll.service;

import java.sql.SQLException;
import java.util.List;

import com.yll.domain.PageStudentBean;
import com.yll.domain.Student;

/* 
 * 这是学生业务处理规范
 * 
 * */
public interface StudentService {
	
	/**
	 * 查询所有学生
	 * @ return List
	 * */
	List<Student> findAll() throws SQLException;
	
	/**
	 * 添加学生
	 * @param stu 需要添加到数据库的学生对象
	 * @throws SQLException
	 * */
	void insert(Student stu) throws SQLException;
	
	/**
	 * 根据id删除学生
	 * @param sid
	 * @throws SQLLLException
	 * */
	void delete(int sid) throws SQLException;
	
	/** 
	  *  根据id查询单个学生对象
	 * @param id
	 * @throws SQLException
	 * */
	Student findStudentById(int sid) throws SQLException;
	
	/**
	 * 更新学生信息
	 * @param stu 需要更新的学生数据
	 * @throws SQLException
	 * */
	void update (Student stu) throws SQLException;
	
	/**
	 * 模糊查询,根据姓名或者性别或者都有
	 *@param sname
	 *@param sgender
	 *@return List
	 *@throws SQLException
	 * */
	List<Student> searchStudent(String sname,String sgender) throws SQLException;
	
	/**
	 * 查询当页的数据
	 * @param currentPage
	 * @return
	 * @thorws SQLException
	 * */
	 PageStudentBean findStudentByPage(int currentPage) throws SQLException;
	
}

  • 包 com.service.impl service功能实现
  1. StudentServiceImpl.java
package com.yll.service.impl;

import java.sql.SQLException;
import java.util.List;

import com.yll.dao.StudentDao;
import com.yll.dao.impl.StudentDaoImpl;
import com.yll.domain.PageStudentBean;
import com.yll.domain.Student;
import com.yll.service.StudentService;

/* 
 * 
 * 这是学生业务的实现
 * */
public class StudentServiceImpl implements StudentService{

	@Override
	public List<Student> findAll() throws SQLException {
//		QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
//		return runner.query("select * from stu;", new BeanListHandler(Student.class));
		StudentDao dao = new StudentDaoImpl();
		return dao.findAll();
	}

	@Override
	public void insert(Student stu) throws SQLException {
		StudentDao dao = new StudentDaoImpl();
		dao.insert(stu);
	}

	@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 {
		StudentDao dao = new StudentDaoImpl();
		return dao.findStudentById(sid);
	}

	@Override
	public void update(Student stu) throws SQLException {
		StudentDao dao = new StudentDaoImpl();
		dao.update(stu);
	}

	@Override
	public List<Student> searchStudent(String sname, String sgender) throws SQLException {
		StudentDao dao = new StudentDaoImpl();
		return dao.searchStudent(sname, sgender);
	}

	
	@Override
	public PageStudentBean findStudentByPage(int currentPage) throws SQLException {
		//封装分页的该页数据
		PageStudentBean<Student> pageBean = new PageStudentBean<Student>();
		pageBean.setCurrentPage(currentPage); //当前页
		pageBean.setPageSize(StudentDao.PAGE_SIZE); //每页的记录数
		List<Student> list = new StudentDaoImpl().findStudentByPage(currentPage); //当页的学生集合
		pageBean.setList(list);
		//总的记录数 
		int totalSize = new StudentDaoImpl().findCount();
		pageBean.setTotalSize(totalSize);
		//总的页数
		pageBean.setTotalPage(totalSize%StudentDao.PAGE_SIZE == 0 ? totalSize/StudentDao.PAGE_SIZE : totalSize/StudentDao.PAGE_SIZE + 1);
		return pageBean;
	}
}

  • 包 com.yll.domain 相当于javaBean的作用
  1. Student.java
package com.yll.domain;

import java.util.Date;

/* 
 *  这是学生封装的对象 bean
 * */
public class Student {
	
	private int sid;
	private String sname;
	private String gender;
	private String phone;
	private String hobby;
	private Date birthday;
	private String info;
	
	
	public Student() {}
	
	public Student(String sname, String gender, String phone, String hobby, Date birthday, String info) {
		super();
		this.sname = sname;
		this.gender = gender;
		this.phone = phone;
		this.hobby = hobby;
		this.birthday = birthday;
		this.info = info;
	}
	
	public Student(int sid,String sname, String gender, String phone, String hobby, Date birthday, String info) {
		super();
		this.sid = sid;
		this.sname = sname;
		this.gender = gender;
		this.phone = phone;
		this.hobby = hobby;
		this.birthday = birthday;
		this.info = info;
	}
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getHobby() {
		return hobby;
	}
	public void setHobby(String hobby) {
		this.hobby = hobby;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	
	
}

  1. PageStudentBean.java
package com.yll.domain;

import java.util.List;

/*
 * 这是一个用于封装了分页的数据
 * 里面包含:
 * 	该页的学生集合数据
 * 	总的记录数
 * 	总的页数
 * 	当前页
 * 	每页显示的记录数
 * 
 * */
public class PageStudentBean<T> {
	private int currentPage; //当前页
	private int totalPage; //总页数
	private int pageSize; //每页的记录数
	private int totalSize; //总的记录数
	private List<T> list; //当前页的学生集合
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalSize() {
		return totalSize;
	}
	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	
} 

  • 包com.yll.servlet 服务器
  1. AddStudentServlet.java
package com.yll.serlvet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

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 com.yll.domain.Student;
import com.yll.service.StudentService;
import com.yll.service.impl.StudentServiceImpl;

/**
 * 用于处理学生的添加请求
 * Servlet implementation class AddStudentServlet
 */
@WebServlet("/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			//获取客户端提交的数据
			request.setCharacterEncoding("UTF-8");
			String sname = request.getParameter("sname");
			String gender = request.getParameter("gender");
			String phone = request.getParameter("phone");
			String birthday = request.getParameter("birthday");
//			String hobby = request.getParameter("hobby");//单个
			String info = request.getParameter("info");
			
			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 stu = new Student(sname, gender, phone, hobby, date, info);
			StudentService service = new StudentServiceImpl();
			service.insert(stu);
			//3.跳转到列表页
			
			/*这里直接跳转到页面上,那么一个页面会被重新翻译一次,上面那个request的
			 * 的请求存放的数据是没有了;
			 * */
			//request.getRequestDispatcher("list.jsp").forward(request, response);
			
			//servlet除了能跳jsp还能跳servlet
			request.getRequestDispatcher("StudentListServlet").forward(request, response);

		} catch (Exception e) {
			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);
	}

}

  1. DeleteStudentServlet.java
package com.yll.serlvet;

import java.io.IOException;

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 com.yll.service.StudentService;
import com.yll.service.impl.StudentServiceImpl;

/**
 * 用于处理删除学生
 * Servlet implementation class DeleteStudentServlet
 */
@WebServlet("/DeleteStudentServlet")
public class DeleteStudentServlet 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();
			service.delete(sid);
			//跳转到列表页
			request.getRequestDispatcher("StudentListServlet").forward(request, response);;
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * @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);
	}

}

  1. EditStudentServlet.java
package com.yll.serlvet;

import java.io.IOException;

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 com.yll.domain.Student;
import com.yll.service.StudentService;
import com.yll.service.impl.StudentServiceImpl;

/**
 *  处理单个学生信息更新
 * 查询一个学生的信息,然后跳转到更新页面
 * Servlet implementation class UpdateStudentServlet
 */
@WebServlet("/EditStudentServlet")
public class EditStudentServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			//1.接收id
			int sid = Integer.parseInt(request.getParameter("sid"));
			//2.查询学生数据
			StudentService service = new StudentServiceImpl();
			Student stu = service.findStudentById(sid);
			//3.显示数据   存数据
			request.setAttribute("stu", stu);
			//跳转
			request.getRequestDispatcher("update.jsp").forward(request, response);;
		} catch (Exception e) {
			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);
	}

}

  1. SearchStudentServlet.java
package com.yll.serlvet;

import java.io.IOException;
import java.util.List;

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 com.yll.domain.Student;
import com.yll.service.StudentService;
import com.yll.service.impl.StudentServiceImpl;

/**
 * Servlet implementation class SearchStudentServlet
 */
@WebServlet("/SearchStudentServlet")
public class SearchStudentServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		try {
			//1.取到要查询的关键数据
			String sname = request.getParameter("sname");
			String sgender = request.getParameter("sgender");
			//2.找service 去查询
			StudentService service = new StudentServiceImpl();
			List<Student> list = service.searchStudent(sname, sgender);
			System.out.println("list.size:"+list.size());
			
			//3.跳转界面
			request.setAttribute("list", list);
			request.getRequestDispatcher("list.jsp").forward(request, response);;
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * @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);
	}

}

  1. StudentListPageServlet.java
package com.yll.serlvet;

import java.io.IOException;

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 com.yll.domain.PageStudentBean;
import com.yll.service.StudentService;
import com.yll.service.impl.StudentServiceImpl;

/**
 * Servlet implementation class StudentListPageServlet
 */
@WebServlet("/StudentListPageServlet")
public class StudentListPageServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			//1.获取需要显示的页码数
			int currentPage = Integer.parseInt(request.getParameter("currentPage"));
			//2.根据指定的页数,去获取该页的数据回来
			StudentService service = new StudentServiceImpl();
			PageStudentBean pageBean = service.findStudentByPage(currentPage);
			request.setAttribute("pageBean", pageBean);
			//3.跳转页面
			request.getRequestDispatcher("listPage.jsp").forward(request, response);
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

  1. StudentListServlet.java
package com.yll.serlvet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

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 com.yll.domain.Student;
import com.yll.service.StudentService;
import com.yll.service.impl.StudentServiceImpl;

/**
 * 负责查询所有的学生信息,然后呈现到list.jsp页面上
 * Servlet implementation class StudentListServlet
 */
@WebServlet("/StudentListServlet")
public class StudentListServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		try {
			//1.查询处理出来所有的学生了
			StudentService service = new StudentServiceImpl();
			List<Student> list = service.findAll();
			for(Student stu:list) {
				System.out.println("stu:"+stu.getSid());
			}
			//2.把数据存到作用域里
			request.setAttribute("list", list);
			
			//3.跳转页面
			request.getRequestDispatcher("list.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);
	}

}

  1. UpdateStudentServlet.java
package com.yll.serlvet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

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 com.yll.domain.Student;
import com.yll.service.StudentService;
import com.yll.service.impl.StudentServiceImpl;

/**
 * Servlet implementation class UpdateStudentServlet
 */
@WebServlet("/UpdateStudentServlet")
public class UpdateStudentServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			//获取客户端提交的数据
			request.setCharacterEncoding("UTF-8");
			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 hobby = request.getParameter("hobby");//单个
			String info = request.getParameter("info");
			
			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 stu = new Student(sid, sname, gender, phone, hobby, date, info);
			//3.更新数据库
			StudentService service = new StudentServiceImpl();
			service.update(stu);
			//4.跳转界面
			request.getRequestDispatcher("StudentListServlet").forward(request, response);

		} catch (Exception e) {
			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);
	}

}

  • 数据库配置 c3p0-config.xml

<c3p0-config>

	
  <default-config>
    <property name="driverClass">com.mysql.cj.jdbc.Driverproperty>
    <property name="jdbcUrl">jdbc:mysql://localhost/stu?serverTimezone=UTCproperty>
    <property name="user">rootproperty>
    <property name="password">52151property>
    
    
    <property name="initialPoolSize">10property>
    <property name="maxIdleTime">30property>
    <property name="maxPoolSize">100property>
    <property name="minPoolSize">10property>
    <property name="maxStatements">200property>
  default-config>
  
   
  <named-config name="oracle"> 
    <property name="acquireIncrement">50property>
    <property name="initialPoolSize">100property>
    <property name="minPoolSize">50property>
    <property name="maxPoolSize">1000property>

    
    <property name="maxStatements">0property> 
    <property name="maxStatementsPerConnection">5property>

    
    <user-overrides user="master-of-the-universe"> 
      <property name="acquireIncrement">1property>
      <property name="initialPoolSize">1property>
      <property name="minPoolSize">1property>
      <property name="maxPoolSize">5property>
      <property name="maxStatementsPerConnection">50property>
    user-overrides>
  named-config>
 
c3p0-config>
	

简陋的前端代码

  • index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta charset="UTF-8">
<title>首页title>
head>
<body>
<H3><a href="StudentListServlet">显示所有学生列表a>H3>
<H3><a href="StudentListPageServlet?currentPage=1">分页显示所有学生列表a>H3>
body>
html>
  • add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta charset="UTF-8">
<title>添加学生页面title>
head>
<body>
	<form method="post" action="AddStudentServlet"> 
		<table>
			<tr>
				<td>姓名td>
				<td><input type="text" name="sname" />td>
				
			tr>
			<tr>
				<td>性别td>
				<td>
				<input type="radio" name="gender" value="" /><input type="radio" name="gender" value="" />td>
			tr>
			<tr>
				<td>电话td>
				<td><input type="text" name="phone" />td>
			tr>
			<tr>
				<td>生日td>
				<td><input type="text" name="birthday" />td>
			tr>
			<tr>
				<td>爱好td>
				<td>
				<input type="checkbox" name="hobby" value="游泳" />游泳
				<input type="checkbox" name="hobby" value="篮球" />篮球
				<input type="checkbox" name="hobby" value="足球" />足球
				<input type="checkbox" name="hobby" value="看书" />看书
				<input type="checkbox" name="hobby" value="写字" />写字
				td>
			tr>
			<tr>
				<td>简介td>
				<td><textarea name="info" rows="3" cols="20">textarea> td>
			tr>
			<tr>
				<td colspan="2"> <input type="submit" value="添加" /> td>
			tr>
		table>
	form>
	
body>
html>
  • update.jsp
<%@ 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"%>

<html>
<head>
<meta charset="UTF-8">
<title>添加学生页面title>
head>
<body>
	<form method="post" action="UpdateStudentServlet"> 
		
		<table>
			<tr>
				<td>姓名td>
				<td><input type="text" name="sname" value="${stu.sname }"/>td>
				
			tr>
			<tr>
				<td>性别td>
				<td>
				<input type="radio" name="gender" value=""  if test="${stu.gender == ''  }">checkedc:if> /> 男
				<input type="radio" name="gender" value=""  if test="${stu.gender == ''  }">checkedc: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="游泳"  if test="${fn:contains(stu.hobby,'游泳') }">checkedc:if> />游泳
				<input type="checkbox" name="hobby" value="篮球"  if test="${fn:contains(stu.hobby,'篮球') }">checkedc:if>/>篮球
				<input type="checkbox" name="hobby" value="足球"  if test="${fn:contains(stu.hobby,'足球') }">checkedc:if>/>足球
				<input type="checkbox" name="hobby" value="看书"  if test="${fn:contains(stu.hobby,'看书') }">checkedc:if>/>看书
				<input type="checkbox" name="hobby" value="写字"  if test="${fn:contains(stu.hobby,'写字') }">checkedc: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>
  • list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
<meta charset="UTF-8">
<title>学生列表页title>
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="EditStudentServlet?sid=${stu.sid}">更新a>     <a href="#" onclick="doDeleteStu(${stu.sid})">删除a>td>
		tr>
		 c:forEach>
		
		
		table> 
 form>
body>
<script type="text/javascript">

	function doDeleteStu(sid){
		var flag = confirm("是否要确定删除?");
		if(flag) {
			window.location.href="DeleteStudentServlet?sid="+sid;
		}else{
			
		}
	}

script>
html>
  • listPage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
<meta charset="UTF-8">
<title>学生列表页title>
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="${pageBean.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="EditStudentServlet?sid=${stu.sid}">更新a>     <a href="#" onclick="doDeleteStu(${stu.sid})">删除a>td>
		tr>
		 c:forEach>
		<tr>
			
				第${pageBean.currentPage } / ${pageBean.totalPage }  
				每页显示 ${pageBean.pageSize }条  
				总记录数  ${pageBean.totalSize } 
				<c:if test="${pageBean.currentPage != 1 }">
					 <a href="StudentListPageServlet?currentPage=1">首页a> |
					  <a href="StudentListPageServlet?currentPage=${pageBean.currentPage-1 }">上一页a>
				c:if>
				<c:forEach begin="1" end="${pageBean.totalPage }" var="i">
					<c:if test="${pageBean.currentPage == i }">
						${i }
					c:if>
					<c:if test="${pageBean.currentPage != i }">
						<a href="StudentListPageServlet?currentPage=${i }">${i }a>
					c:if>
				c:forEach>
				<c:if test="${pageBean.currentPage != pageBean.totalPage }">
					 <a href="StudentListPageServlet?currentPage=${pageBean.currentPage+1 }">下一页a> |
					  <a href="StudentListPageServlet?currentPage=${pageBean.totalPage }">尾页a>
				c:if>
			td>
		tr>
		table> 
 form>
body>
<script type="text/javascript">

	function doDeleteStu(sid){
		var flag = confirm("是否要确定删除?");
		if(flag) {
			window.location.href="DeleteStudentServlet?sid="+sid;
		}else{
			
		}
	}

script>
html>

数据库

数据库简单写一个表就行啦(库好像被我删了,所以没有截图)
主要属性也就是Dao里学生bean的变量
sid;sname;gender; phone;hobby; birthday; info;

虽然仅仅是一个看似简单的项目,但是学这个花了我两天的时间。每个功能的实现都对我有一定的启发,所以把代码放上来。这给我的感觉是,以后要尊重每一个小到不能再小的功能,不能眼高手低。哎,加油吧。

你可能感兴趣的:(JAVA,JAVA,WEB)