做个简单的Java学生考勤系统08--完善老师相关功能

Posted exodus3

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了做个简单的Java学生考勤系统08--完善老师相关功能相关的知识,希望对你有一定的参考价值。

前几篇,我们做出了老师显示,查询老师信息的功能,接下来是对老师方面功能的增强。

1、列出所有老师的方法

package servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import service.utilService;
import util.DB;

import model.TAdmin;
import model.Tkecheng;
import model.Tlaoshi;

public class laoshi_servlet extends HttpServlet
{
	public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException 
	{
        String type=req.getParameter("type");
		
		if(type.endsWith("laoshiAdd"))
		{
			laoshiAdd(req, res);
		}
		if(type.endsWith("laoshiMana"))
		{
			laoshiMana(req, res);
		}
		if(type.endsWith("laoshiDel"))
		{
			laoshiDel(req, res);
		}
		if(type.endsWith("laoshiEdit1"))
		{
			laoshiEdit1(req, res);
		}
		if(type.endsWith("laoshiEdit2"))
		{
			laoshiEdit2(req, res);
		}
		if(type.endsWith("laoshiAll"))
		{
			laoshiAll(req, res);
		}
		
		if(type.endsWith("daoru"))
		{
			daoru(req, res);
		}
	}
	
	public void dispatch(String targetURI,HttpServletRequest request,HttpServletResponse response) 
	{
		RequestDispatcher dispatch = getServletContext().getRequestDispatcher(targetURI);
		try 
		{
		    dispatch.forward(request, response);
		    return;
		} 
		catch (ServletException e) 
		{
                    e.printStackTrace();
		} 
		catch (IOException e) 
		{
			
		    e.printStackTrace();
		}
	}
	public void init(ServletConfig config) throws ServletException 
	{
		super.init(config);
	}
	
	public void destroy() 
	{
		
	}
}

方法讲解:所有的方法,包括管理老师信息,添加老师信息,查询所有老师信息,删除老师信息,编辑老师信息,用Excel导入老师信息

2、管理老师信息

public void laoshiMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
	List laoshiList=new ArrayList();
	String sql="select * from t_laoshi where del='no'";
	Object[] params={};
	DB mydb=new DB();
	try
	{
		mydb.doPstm(sql, params);
		ResultSet rs=mydb.getRs();
		while(rs.next())
		{
			Tlaoshi laoshi=new Tlaoshi();
			
			laoshi.setId(rs.getInt("id"));
			laoshi.setBianhao(rs.getString("bianhao"));
			laoshi.setXingming(rs.getString("xingming"));
			laoshi.setXingbie(rs.getString("xingbie"));
			
			laoshi.setNianling(rs.getString("nianling"));
			laoshi.setZhicheng(rs.getString("zhicheng"));
			laoshi.setLoginname(rs.getString("loginname"));
			laoshi.setLoginpw(rs.getString("loginpw"));
			laoshi.setDel(rs.getString("del"));
			
			laoshiList.add(laoshi);
	    }
		rs.close();
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
	mydb.closed();
	
	req.setAttribute("laoshiList", laoshiList);
	req.getRequestDispatcher("admin/laoshi/laoshiMana.jsp").forward(req, res);
}

方法讲解:进入管理老师的界面

3、添加老师信息

public void laoshiAdd(HttpServletRequest req,HttpServletResponse res)
{
	String bianhao=req.getParameter("bianhao");
	String xingming=req.getParameter("xingming");
	String xingbie=req.getParameter("xingbie");
	String nianling=req.getParameter("nianling");
	
	String zhicheng=req.getParameter("zhicheng");
	String loginname=req.getParameter("loginname");
	String loginpw=req.getParameter("loginpw");
	String del="no";
	
	String sql="insert into t_laoshi values(?,?,?,?,?,?,?,?)";
	Object[] params={bianhao,xingming,xingbie,nianling,zhicheng,loginname,loginpw,del};
	DB mydb=new DB();
	mydb.doPstm(sql, params);
	mydb.closed();
	
	req.setAttribute("message", "操作成功");
	req.setAttribute("path", "laoshi?type=laoshiMana");
	
       String targetURL = "/common/success.jsp";
	dispatch(targetURL, req, res);
}

4、查询所有老师信息

public void laoshiAll(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
	List laoshiList=new ArrayList();
	String sql="select * from t_laoshi where del='no'";
	Object[] params={};
	DB mydb=new DB();
	try
	{
		mydb.doPstm(sql, params);
		ResultSet rs=mydb.getRs();
		while(rs.next())
		{
			Tlaoshi laoshi=new Tlaoshi();
			
			laoshi.setId(rs.getInt("id"));
			laoshi.setBianhao(rs.getString("bianhao"));
			laoshi.setXingming(rs.getString("xingming"));
			laoshi.setXingbie(rs.getString("xingbie"));
			
			laoshi.setNianling(rs.getString("nianling"));
			laoshi.setZhicheng(rs.getString("zhicheng"));
			laoshi.setLoginname(rs.getString("loginname"));
			laoshi.setLoginpw(rs.getString("loginpw"));
			laoshi.setDel(rs.getString("del"));
			
			laoshiList.add(laoshi);
	    }
		rs.close();
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
	mydb.closed();
	
	req.setAttribute("laoshiList", laoshiList);
	req.getRequestDispatcher("admin/laoshi/laoshiAll.jsp").forward(req, res);
}

5、删除老师信息

public void laoshiDel(HttpServletRequest req,HttpServletResponse res)
{
	
	String sql="update t_laoshi set del='yes' where id="+Integer.parseInt(req.getParameter("id"));
	Object[] params={};
	DB mydb=new DB();
	mydb.doPstm(sql, params);
	mydb.closed();
	
	req.setAttribute("message", "操作成功");
	req.setAttribute("path", "laoshi?type=laoshiMana");
	
       String targetURL = "/common/success.jsp";
	dispatch(targetURL, req, res);
}

6、编辑老师信息

public void laoshiEdit1(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
	int id=Integer.parseInt(req.getParameter("id"))	;
	
	req.setAttribute("laoshi", utilService.get_laoshi(id));
	req.getRequestDispatcher("admin/laoshi/laoshiEdit1.jsp").forward(req, res);
}
	
public void laoshiEdit2(HttpServletRequest req,HttpServletResponse res)
{
	String bianhao=req.getParameter("bianhao");
	String xingming=req.getParameter("xingming");
	String xingbie=req.getParameter("xingbie");
	String nianling=req.getParameter("nianling");
	
	String zhicheng=req.getParameter("zhicheng");
	String loginname=req.getParameter("loginname");
	String loginpw=req.getParameter("loginpw");
	int id=Integer.parseInt(req.getParameter("id"));
	
	String sql="update t_laoshi set bianhao=?,xingming=?,xingbie=?,nianling=?,zhicheng=?,loginname=?,loginpw=? where id=?";
	Object[] params={bianhao,xingming,xingbie,nianling,zhicheng,loginname,loginpw,id};
	DB mydb=new DB();
	mydb.doPstm(sql, params);
	mydb.closed();
	
	req.setAttribute("message", "操作成功");
	req.setAttribute("path", "laoshi?type=laoshiMana");
	
       String targetURL = "/common/success.jsp";
	dispatch(targetURL, req, res);
}

方法讲解:需要注意的是,laoshiEdit1方法是将老师信息带到编辑页面,laoshiEdit2方法进行正在的保存

7、用Excel导入老师信息

public void daoru(HttpServletRequest req,HttpServletResponse res) throws IOException
{
	
	String fujianXingming=req.getParameter("fujianXingming");
	String lujing = req.getSession().getServletContext().getRealPath("upload")+"/"+fujianXingming;
	
	FileInputStream fin=new FileInputStream(lujing);
    HSSFWorkbook workbook=new HSSFWorkbook(fin);//创建工作薄
    HSSFSheet sheet=workbook.getSheetAt(0);//得到工作表
    HSSFRow row=null;//对应excel的行
    HSSFCell cell=null;//对应excel的列
   
    int totalRow=sheet.getLastRowNum();//得到excel的总记录条数
    for(int i=1;i<=totalRow;i++)
    {
        row=sheet.getRow(i);
        
        cell=row.getCell(0);
        String bianhao=cell.getRichStringCellValue().toString();
        
        cell=row.getCell(1);
        String xingming=cell.getRichStringCellValue().toString();
        
        cell=row.getCell(2);
        String xingbie=cell.getRichStringCellValue().getString();
        
        cell=row.getCell(3);
        String nianling=cell.getRichStringCellValue().toString();
        
        cell=row.getCell(4);
        String zhicheng=cell.getRichStringCellValue().toString();
        
        cell=row.getCell(5);
        String loginname=cell.getRichStringCellValue().toString();
        
        cell=row.getCell(6);
        String loginpw=cell.getRichStringCellValue().toString();
        
        String del="no";
		
        String sql="insert into t_laoshi values(?,?,?,?,?,?,?,?)";
		Object[] params={bianhao,xingming,xingbie,nianling,zhicheng,loginname,loginpw,del};
		DB mydb=new DB();
		mydb.doPstm(sql, params);
		mydb.closed();
   }
    
    req.setAttribute("message", "操作成功");
	req.setAttribute("path", "laoshi?type=laoshiMana");
	
       String targetURL = "/common/success.jsp";
	dispatch(targetURL, req, res);
	
以上是关于做个简单的Java学生考勤系统08--完善老师相关功能的主要内容,如果未能解决你的问题,请参考以下文章

做个简单的Java学生考勤系统05--查询课程课表学生与老师信息

做个简单的Java学生考勤系统06--签到功能完善

做个简单的Java学生考勤系统02--工具类

做个简单的Java学生考勤系统04--签到功能的开发

做个简单的Java学生考勤系统03--登录功能的开发

做个简单的Java学生考勤系统01--实体类的创建与建表