做个简单的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);
	
}

方法讲解:该功能是从Excel导入老师到系统里,然后保存数据到数据库,同样的,Excel的列的顺序和老师对象的属性顺序不能乱。
以上就是老师相关的方法了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

exodus3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值