mysql 网页员工登记表_作业1:小型考勤登记表

这次在广州实习了20天,收获还比较大。不过仍需要继续努力。这次总共布置了两个作业,我总结一下:

登记考勤信息,查看信息——主要就是往数据库增加数据,然后再从数据库中读取出来。

44a0baeb7a6f41e7f0b900421b4fc62a.png

6cf9487f721122c727928601b1be9008.png

63ff079ecee49729ebe25efce29c8c6d.png

代码演示:

从数据库里面写入数据:

String name= request.getParameter("name");

String dept= request.getParameter("dept");

String datetime= request.getParameter("datetime");

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-mm-dd");

Date date=sdf.parse(datetime);int status =Integer.valueOf(request.getParameter("status")).intValue();

attence a= newattence();

a.setEmpName(name);

a.setDept(dept);

a.setDatetime(date);

a.setStatus(status);

AttenceBiz attenceBiz= newAttenceBizImpl();boolean result =attenceBiz.addAttence(a);if(result){//response.sendRedirect("index.jsp");

out.print("成功");

}else{//request.getRequestDispatcher("add.jsp").forward(request, response);

out.print("失败");

}%>

这个是在jsp里面写的,不是在servlet里面写的。因为好理解,不过我现在已经习惯了写在servlet里面了。

首页jsp,往里面写入数据:

String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>

考勤记录信息统计
考勤记录信息统计表
姓名
所属部门
考勤日期
考勤状态

正常

迟到

早退

休假

外出

查看所有考勤信息

获取所有数据的jsp:

List attences =aBiz.getAll();

request.setAttribute("attences", attences);%>

考勤记录信息统计
考勤记录信息统计表
员工姓名所属部门考勤日期考勤状态
${attence.empName }${attence.dept }${attence.datetime }${attence.status }

这个是把数据库里面的数据全部读取出来。

主要的方法实现:

packagecom.Attence.daoImpl;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.List;importcom.Attence.dao.AttenceDao;importcom.Seraphjin.Attence.model.attence;public class AttenceDaoImpl extends BaseDao implementsAttenceDao {

ArrayList attences = new ArrayList();

@Overridepublic ListgetAll() {try{

openConnection();

String sql= "select * from attence";

ResultSet resultSet= executeQuery(sql, null);while(resultSet.next()) {

attence a= newattence();

a.setId(resultSet.getInt("id"));

a.setEmpName(resultSet.getString("empName"));

a.setDept(resultSet.getString("dept"));

a.setDatetime(resultSet.getDate("datetime"));

a.setStatus(resultSet.getInt("status"));

attences.add(a);

}

}catch(ClassNotFoundException e) {

e.printStackTrace();

}catch(SQLException e) {

e.printStackTrace();

}finally{

closeResourse();

}returnattences;

}

@Overridepublic booleanaddAttence(attence a) {boolean result = false;try{

openConnection();

String sql="insert into attence value(?,?,?,?,?)";

result=excute(sql, newObject[]{

a.getId(),

a.getEmpName(),

a.getDept(),

a.getDatetime(),

a.getStatus()

});

}catch(ClassNotFoundException e) {

e.printStackTrace();

}catch(SQLException e) {

e.printStackTrace();

}finally{

closeResourse();

}returnresult;

}

@Overridepublic boolean deleteAttence(intid) {boolean result = false;try{

openConnection();

String sql="delete from attence where id = ?";

result= excute(sql, newObject[]{id});

}catch(ClassNotFoundException e) {

e.printStackTrace();

}catch(SQLException e) {

e.printStackTrace();

}finally{

closeResourse();

}returnresult;

}

@Overridepublic booleanupdateAttence(attence a) {boolean result = false;try{

openConnection();

String sql= "update attence set empName = ?, dept =?, datetime=?,status=? where id=?";

result= excute(sql, newObject[]{

a.getId()

});

}catch(ClassNotFoundException e) {

e.printStackTrace();

}catch(SQLException e) {

e.printStackTrace();

}finally{

closeResourse();

}returnresult;

}

}

连接数据库的基本操作:

packagecom.Attence.daoImpl;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;public classBaseDao {//连接数据库

private String className = "com.mysql.jdbc.Driver";private String dburl = "jdbc:mysql://localhost/ZJJexe";private String user = "root";private String password = "root";privateConnection connection;privatePreparedStatement statement;privateResultSet resultSet;public void openConnection() throwsClassNotFoundException, SQLException{//加载驱动

Class.forName(className);//创建连接

connection =DriverManager.getConnection(dburl,user,password);

}//查询方法

public ResultSet executeQuery(String sql,Object[] params) throwsSQLException{

statement=connection.prepareStatement(sql);//追加参数

if(params !=null){int i=1;for(Object object : params) {

statement.setObject(i, object);

i++;

}

}

resultSet=statement.executeQuery();returnresultSet;

}//更新

public boolean excute(String sql,Object[] params) throwsSQLException {

statement=connection.prepareStatement(sql);if(params !=null){int i=1;for(Object object : params) {

statement.setObject(i, object);

i++;

}

}int updateCount =statement.executeUpdate();if (updateCount != 0) {return true;

}else{return false;

}

}//释放资源

public voidcloseResourse(){try{if(resultSet != null){

resultSet.close();

}if(statement != null){

statement.close();

}if(connection != null){

connection.close();

}

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

其他就是方法的声明与调用。还有一个实体类,是员工信息:

packagecom.Seraphjin.Attence.model;importjava.util.Date;public classattence {private intid;privateString empName;privateString dept;privateDate datetime;private intstatus;public intgetId() {returnid;

}public void setId(intid) {this.id =id;

}publicString getEmpName() {returnempName;

}public voidsetEmpName(String empName) {this.empName =empName;

}publicString getDept() {returndept;

}public voidsetDept(String dept) {this.dept =dept;

}publicDate getDatetime() {returndatetime;

}public voidsetDatetime(Date datetime) {this.datetime =datetime;

}public intgetStatus() {returnstatus;

}public void setStatus(intstatus) {this.status =status;

}

}

OK~

近期我要学会用小乌龟,然后将自己的小项目部署到Github上~加油!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值