做个简单的Java学生考勤系统07--完善学生相关功能
Posted exodus3
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了做个简单的Java学生考勤系统07--完善学生相关功能相关的知识,希望对你有一定的参考价值。
前几篇,我们做出了学生显示,查询学生信息的功能,接下来是对学生方面功能的增强。
1、列出所有学生的方法
package servlet;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 util.DB;
import model.TAdmin;
import model.Tkecheng;
import model.Tlaoshi;
import model.Txuesheng;
public class xuesheng_servlet extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException
{
String type=req.getParameter("type");
if(type.endsWith("xueshengAdd"))
{
xueshengAdd(req, res);
}
if(type.endsWith("xueshengMana"))
{
xueshengMana(req, res);
}
if(type.endsWith("xueshengDel"))
{
xueshengDel(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 xueshengMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
List xueshengList=new ArrayList();
String sql="select * from t_xuesheng where del='no'";
Object[] params={};
DB mydb=new DB();
try
{
mydb.doPstm(sql, params);
ResultSet rs=mydb.getRs();
while(rs.next())
{
Txuesheng xuesheng=new Txuesheng();
xuesheng.setId(rs.getInt("id"));
xuesheng.setXuehao(rs.getString("xuehao"));
xuesheng.setXingming(rs.getString("xingming"));
xuesheng.setXingbie(rs.getString("xingbie"));
xuesheng.setNianling(rs.getString("nianling"));
xuesheng.setBanji(rs.getString("banji"));
xuesheng.setLoginname(rs.getString("loginname"));
xuesheng.setLoginpw(rs.getString("loginpw"));
xuesheng.setDel(rs.getString("del"));
xueshengList.add(xuesheng);
}
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
mydb.closed();
req.setAttribute("xueshengList", xueshengList);
req.getRequestDispatcher("admin/xuesheng/xueshengMana.jsp").forward(req, res);
}
方法讲解:进入管理学生的界面
3、添加学生方法
public void xueshengAdd(HttpServletRequest req,HttpServletResponse res)
{
String xuehao=req.getParameter("xuehao");
String xingming=req.getParameter("xingming");
String xingbie=req.getParameter("xingbie");
String nianling=req.getParameter("nianling");
String banji=req.getParameter("banji");
String loginname=req.getParameter("loginname");
String loginpw=req.getParameter("loginpw");
String del="no";
String sql="insert into t_xuesheng values(?,?,?,?,?,?,?,?)";
Object[] params={xuehao,xingming,xingbie,nianling,banji,loginname,loginpw,del};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
req.setAttribute("message", "操作成功");
req.setAttribute("path", "xuesheng?type=xueshengMana");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
方法讲解:增加学生信息,跳转到成功页面
4、删除学生
public void xueshengDel(HttpServletRequest req,HttpServletResponse res)
{
String sql="update t_xuesheng 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", "xuesheng?type=xueshengMana");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
方法讲解:删除学生信息,跳转到成功页面
5、导入学生信息
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 xuehao=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 banji=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_xuesheng values(?,?,?,?,?,?,?,?)";
Object[] params={xuehao,xingming,xingbie,nianling,banji,loginname,loginpw,del};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
}
req.setAttribute("message", "操作成功");
req.setAttribute("path", "xuesheng?type=xueshengMana");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
方法讲解:该功能是从Excel导入学生到系统里,然后保存数据到数据库,值得注意的是,Excel里面的列要对齐,要统一,比如第一列是学号,第二列是姓名,第三列是性别等等,顺序不能乱
以上是关于做个简单的Java学生考勤系统07--完善学生相关功能的主要内容,如果未能解决你的问题,请参考以下文章