mysql 网页员工登记表_作业1:小型考勤登记表
這次在廣州實(shí)習(xí)了20天,收獲還比較大。不過(guò)仍需要繼續(xù)努力。這次總共布置了兩個(gè)作業(yè),我總結(jié)一下:
登記考勤信息,查看信息——主要就是往數(shù)據(jù)庫(kù)增加數(shù)據(jù),然后再?gòu)臄?shù)據(jù)庫(kù)中讀取出來(lái)。
代碼演示:
從數(shù)據(jù)庫(kù)里面寫入數(shù)據(jù):
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("失敗");
}%>
這個(gè)是在jsp里面寫的,不是在servlet里面寫的。因?yàn)楹美斫?#xff0c;不過(guò)我現(xiàn)在已經(jīng)習(xí)慣了寫在servlet里面了。
首頁(yè)jsp,往里面寫入數(shù)據(jù):
String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
考勤記錄信息統(tǒng)計(jì)查看所有考勤信息
獲取所有數(shù)據(jù)的jsp:
List attences =aBiz.getAll();
request.setAttribute("attences", attences);%>
考勤記錄信息統(tǒng)計(jì)| ${attence.empName } | ${attence.dept } | ${attence.datetime } | ${attence.status } |
這個(gè)是把數(shù)據(jù)庫(kù)里面的數(shù)據(jù)全部讀取出來(lái)。
主要的方法實(shí)現(xiàn):
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;
}
}
連接數(shù)據(jù)庫(kù)的基本操作:
packagecom.Attence.daoImpl;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;public classBaseDao {//連接數(shù)據(jù)庫(kù)
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{//加載驅(qū)動(dòng)
Class.forName(className);//創(chuàng)建連接
connection =DriverManager.getConnection(dburl,user,password);
}//查詢方法
public ResultSet executeQuery(String sql,Object[] params) throwsSQLException{
statement=connection.prepareStatement(sql);//追加參數(shù)
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();
}
}
}
其他就是方法的聲明與調(diào)用。還有一個(gè)實(shí)體類,是員工信息:
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~
近期我要學(xué)會(huì)用小烏龜,然后將自己的小項(xiàng)目部署到Github上~加油!
總結(jié)
以上是生活随笔為你收集整理的mysql 网页员工登记表_作业1:小型考勤登记表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 下载谷歌离线地图瓦片图
- 下一篇: 项目管理:如何制作项目进度表