这次在广州实习了20天,收获还比较大。不过仍需要继续努力。这次总共布置了两个作业,我总结一下:
登记考勤信息,查看信息——主要就是往数据库增加数据,然后再从数据库中读取出来。
代码演示:
从数据库里面写入数据:
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上~加油!