你的位置:首页 > Java教程

[Java教程]利用js日期控件重构WEB功能


开发需求:网页中的日期部门都用js日期控件重写

页面一:更新员工页面

empUpdate.jsp 中增加 onfocus 事件

入职日期:<input id="hiredate" type="text" name="hiredateTxt" value="${requestScope.empBean.hiredate}" onfocus="setday(this,'yyyy-MM-dd','2010-01-01','2020-12-30',1)" readonly="readonly"><br>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>员工更新</title><link href="http://www.cnblogs.com//web01//css/main.css" rel="stylesheet" type="text/css" /><script type="text/javascript" src="http://www.cnblogs.com//web01/js/DatePicker.js"></script></head><body><%@ include file="top.jsp" %><form action="/web01/empController" method="get">员工编号:<input type="text" disabled="disabled" value="${requestScope.empBean.empno}"><br>员工姓名:<input type="text" name="enameTxt" value="${requestScope.empBean.ename}"><br>职位:<input type="text" name="jobTxt" value="${requestScope.empBean.job}"><br>领导:<input type="text" name="mgrTxt" value="${requestScope.empBean.mgr}"><br>入职日期:<input id="hiredate" type="text" name="hiredateTxt" value="${requestScope.empBean.hiredate}" onfocus="setday(this,'yyyy-MM-dd','2010-01-01','2020-12-30',1)" readonly="readonly"><br>工资:<input type="text" name="salTxt" value="${requestScope.empBean.sal}"><br>奖金:<input type="text" name="commTxt" value="${requestScope.empBean.comm}"><br>部门:<input type="text" name="deptnoTxt" value="${requestScope.empBean.deptno}"><br><input type="submit" value="Save"><input type="hidden" name="callTp" value="empSave"><input type="hidden" name="empno" value="${requestScope.empBean.empno}"><br/></form><%@ include file="bottom.jsp" %></body></html>

前台传到后台的日期格式是 yyyy-mm-dd,在java端进行格式化去掉“-”后变成 yyyymmdd格式的字符串

所以增加了一个处理String的类StringUtil.java。

package com.test.common.util;public class StringUtil {    public static String formatString(String dateStringWithLine){    String dateString = null;    if (dateStringWithLine != null) {      dateString = dateStringWithLine.replace("-", "");    }    return dateString;  }}

在service层调用SQL之前处理字符串 StringUtil.formatString(empBean.getHiredate())

  // 更新emp信息  public int empSave(EmpBean empBean) {    int updateResulInt = 0;        BaseDao baseDao = new BaseDao();    try {      conn = baseDao.dbConnection();    } catch (SQLException e1) {      e1.printStackTrace();    }        StringBuffer sqlBf = new StringBuffer();    sqlBf.setLength(0);        sqlBf.append("UPDATE EMP SET ENAME = ?                \n");    sqlBf.append("       , JOB = ?                 \n");    sqlBf.append("       , MGR = ?                 \n");    sqlBf.append("       , HIREDATE = TO_DATE(?, 'YYYYMMDD')    \n");    sqlBf.append("       , SAL = ?                 \n");    sqlBf.append("       , COMM = ?                \n");    sqlBf.append("       , DEPTNO = ?               \n");    sqlBf.append("WHERE EMPNO = ?                     \n");        try {      pstmt = conn.prepareStatement(sqlBf.toString());      idx = 1;      pstmt.setString(idx++, empBean.getEname());      pstmt.setString(idx++, empBean.getJob());      pstmt.setInt(idx++, empBean.getMgr());      pstmt.setString(idx++, StringUtil.formatString(empBean.getHiredate()));      pstmt.setDouble(idx++, empBean.getSal());      pstmt.setDouble(idx++, empBean.getComm());      pstmt.setInt(idx++, empBean.getDeptno());      pstmt.setInt(idx++, empBean.getEmpno());            updateResulInt = pstmt.executeUpdate();    } catch (SQLException e) {      e.printStackTrace();    } finally {      baseDao.dbDisconnection(null, pstmt, conn);    }        return updateResulInt;  }

页面效果是


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>添加员工</title><link href="http://www.cnblogs.com//web01/css/main.css" rel="stylesheet" type="text/css" /><script type="text/javascript" src="http://www.cnblogs.com//web01/js/DatePicker.js"></script></head><body><%@ include file="top.jsp" %><form action="/web01/empController" method="get">员工姓名:<input type="text" name="enameTxt" value="" maxlength="10"><br>职位:<input type="text" name="jobTxt" value="" maxlength="9"><br>领导号:<input type="text" name="mgrTxt" value="" maxlength="4"><br>入职日期:<input id="hiredate" type="text" name="hiredateTxt" value="" onfocus="setday(this,'yyyy-MM-dd','2010-01-01','2020-12-30',1)" readonly="readonly"><br>工资:<input type="text" name="salTxt" value="" maxlength="7"><br>奖金:<input type="text" name="commTxt" value="" maxlength="7"><br>部门编号:<input type="text" name="deptnoTxt" value="" maxlength="2"><br><input type="submit" value="Add"><input type="hidden" name="callTp" value="empAdd"><br/></form><%@ include file="bottom.jsp" %></body></html>

添加员工的service层调用SQL之前处理字符串 StringUtil.formatString(empBean.getHiredate())

 

  // 添加新的员工  public int empAdd(EmpBean emp) {    int insertInt = 0;    BaseDao baseDao = new BaseDao();    try {      conn = baseDao.dbConnection();    } catch (SQLException e1) {      e1.printStackTrace();    }        int nextEmpno = this.getNextEmpno();        StringBuffer sqlBf = new StringBuffer();    sqlBf.setLength(0);        sqlBf.append("INSERT INTO EMP(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)    \n");    sqlBf.append("     VALUES(?                             \n");    sqlBf.append("        , ?                             \n");    sqlBf.append("        , ?                             \n");    sqlBf.append("        , ?                             \n");    sqlBf.append("        , TO_DATE(?, 'YYYYMMDD')                   \n");    sqlBf.append("        , ?                             \n");    sqlBf.append("        , ?                             \n");    sqlBf.append("        , ?)                             \n");        try {      pstmt = conn.prepareStatement(sqlBf.toString());      idx = 1;      pstmt.setInt(idx++, nextEmpno);      pstmt.setString(idx++, emp.getEname());      pstmt.setString(idx++, emp.getJob());      pstmt.setInt(idx++, emp.getMgr());      pstmt.setString(idx++, StringUtil.formatString(emp.getHiredate()));      pstmt.setDouble(idx++, emp.getSal());      pstmt.setDouble(idx++, emp.getComm());      pstmt.setInt(idx++, emp.getDeptno());            insertInt = pstmt.executeUpdate();    } catch (SQLException e) {      e.printStackTrace();    } finally {      baseDao.dbDisconnection(rs, pstmt, conn);    }        return insertInt;  }

 

员工的service的全部代码

package com.test.biz.service;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import com.test.biz.bean.EmpBean;import com.test.common.dao.BaseDao;import com.test.common.util.StringUtil;public class EmpService {  private int idx = 1;  Connection conn = null;  PreparedStatement pstmt = null;  ResultSet rs = null;      public ArrayList<EmpBean> getEmpList(EmpBean eb){    ArrayList<EmpBean> empList = new ArrayList<EmpBean>();    BaseDao baseDao = new BaseDao();    try {      conn = baseDao.dbConnection();    } catch (SQLException e1) {      e1.printStackTrace();    }    // 3. 执行SQL语句    StringBuffer sqlBf = new StringBuffer();    sqlBf.setLength(0);        sqlBf.append("SELECT EMPNO                       \n");    sqlBf.append("   , ENAME                       \n");    sqlBf.append("   , JOB                        \n");    sqlBf.append("   , MGR                        \n");    sqlBf.append("   , TO_CHAR(HIREDATE, 'YYYYMMDD') HIREDATE      \n");    sqlBf.append("   , SAL                        \n");    sqlBf.append("   , COMM                       \n");    sqlBf.append("   , DEPTNO                      \n");    sqlBf.append("FROM  EMP                        \n");    sqlBf.append("WHERE  ENAME LIKE UPPER(?) || '%'            \n");    sqlBf.append("ORDER BY EMPNO                      \n");    try {      pstmt = conn.prepareStatement(sqlBf.toString());      idx = 1;      pstmt.setString(idx++, eb.getEname());            // 4. 获取结果集      rs = pstmt.executeQuery();      while (rs.next()) {        EmpBean emp = new EmpBean();                emp.setEmpno(rs.getInt("empno"));        emp.setEname(rs.getString("ename"));        emp.setJob(rs.getString("job"));        emp.setMgr(rs.getInt("mgr"));        emp.setHiredate(rs.getString("hiredate"));        emp.setSal(rs.getDouble("sal"));        emp.setComm(rs.getDouble("comm"));        emp.setDeptno(rs.getInt("deptno"));                empList.add(emp);      }          } catch (SQLException e) {      e.printStackTrace();    } finally {      baseDao.dbDisconnection(rs, pstmt, conn);    }        return empList;  }  // 利用empno查询单条员工信息  public EmpBean empById(int empno) {    EmpBean emp = new EmpBean();        BaseDao baseBao = new BaseDao();    try {      conn = baseBao.dbConnection();    } catch (SQLException e) {      e.printStackTrace();    }        StringBuffer sqlBf = new StringBuffer();    sqlBf.setLength(0);        sqlBf.append("SELECT  EMPNO                    \n");    sqlBf.append("    , ENAME                    \n");    sqlBf.append("    , JOB                     \n");    sqlBf.append("    , MGR                     \n");    sqlBf.append("    , TO_CHAR(HIREDATE, 'YYYY-MM-DD') HIREDATE  \n");    sqlBf.append("    , SAL                     \n");    sqlBf.append("    , COMM                    \n");    sqlBf.append("    , DEPTNO                   \n");    sqlBf.append("FROM   EMP                     \n");    sqlBf.append("WHERE  EMPNO = ?                  \n");        try {      pstmt = conn.prepareStatement(sqlBf.toString());      idx = 1;      pstmt.setInt(idx++, empno);            rs = pstmt.executeQuery();      if (rs.next()) {        emp.setEmpno(rs.getInt("EMPNO"));        emp.setEname(rs.getString("ENAME"));        emp.setJob(rs.getString("JOB"));        emp.setMgr(rs.getInt("MGR"));        emp.setHiredate(rs.getString("HIREDATE"));        emp.setSal(rs.getDouble("SAL"));        emp.setComm(rs.getDouble("COMM"));        emp.setDeptno(rs.getInt("DEPTNO"));      }    } catch (SQLException e) {      e.printStackTrace();    } finally {      baseBao.dbDisconnection(rs, pstmt, conn);    }        return emp;  }  // 更新emp信息  public int empSave(EmpBean empBean) {    int updateResulInt = 0;        BaseDao baseDao = new BaseDao();    try {      conn = baseDao.dbConnection();    } catch (SQLException e1) {      e1.printStackTrace();    }        StringBuffer sqlBf = new StringBuffer();    sqlBf.setLength(0);        sqlBf.append("UPDATE EMP SET ENAME = ?                \n");    sqlBf.append("       , JOB = ?                 \n");    sqlBf.append("       , MGR = ?                 \n");    sqlBf.append("       , HIREDATE = TO_DATE(?, 'YYYYMMDD')    \n");    sqlBf.append("       , SAL = ?                 \n");    sqlBf.append("       , COMM = ?                \n");    sqlBf.append("       , DEPTNO = ?               \n");    sqlBf.append("WHERE EMPNO = ?                     \n");        try {      pstmt = conn.prepareStatement(sqlBf.toString());      idx = 1;      pstmt.setString(idx++, empBean.getEname());      pstmt.setString(idx++, empBean.getJob());      pstmt.setInt(idx++, empBean.getMgr());      pstmt.setString(idx++, StringUtil.formatString(empBean.getHiredate()));      pstmt.setDouble(idx++, empBean.getSal());      pstmt.setDouble(idx++, empBean.getComm());      pstmt.setInt(idx++, empBean.getDeptno());      pstmt.setInt(idx++, empBean.getEmpno());            updateResulInt = pstmt.executeUpdate();    } catch (SQLException e) {      e.printStackTrace();    } finally {      baseDao.dbDisconnection(null, pstmt, conn);    }        return updateResulInt;  }  // 获取下一个员工号  public int getNextEmpno() {    int nextEmpno = 0;    StringBuffer sqlBf = new StringBuffer();    sqlBf.setLength(0);        sqlBf.append("SELECT MAX(EMPNO) + 1   AS EMPNO \n");    sqlBf.append("FROM  EMP              \n");        try {      pstmt = conn.prepareStatement(sqlBf.toString());            rs = pstmt.executeQuery();      if (rs.next()) {        nextEmpno = rs.getInt("EMPNO");      }    } catch (SQLException e) {      e.printStackTrace();    }         return nextEmpno;  }  // 添加新的员工  public int empAdd(EmpBean emp) {    int insertInt = 0;    BaseDao baseDao = new BaseDao();    try {      conn = baseDao.dbConnection();    } catch (SQLException e1) {      e1.printStackTrace();    }        int nextEmpno = this.getNextEmpno();        StringBuffer sqlBf = new StringBuffer();    sqlBf.setLength(0);        sqlBf.append("INSERT INTO EMP(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)    \n");    sqlBf.append("     VALUES(?                             \n");    sqlBf.append("        , ?                             \n");    sqlBf.append("        , ?                             \n");    sqlBf.append("        , ?                             \n");    sqlBf.append("        , TO_DATE(?, 'YYYYMMDD')                   \n");    sqlBf.append("        , ?                             \n");    sqlBf.append("        , ?                             \n");    sqlBf.append("        , ?)                             \n");        try {      pstmt = conn.prepareStatement(sqlBf.toString());      idx = 1;      pstmt.setInt(idx++, nextEmpno);      pstmt.setString(idx++, emp.getEname());      pstmt.setString(idx++, emp.getJob());      pstmt.setInt(idx++, emp.getMgr());      pstmt.setString(idx++, StringUtil.formatString(emp.getHiredate()));      pstmt.setDouble(idx++, emp.getSal());      pstmt.setDouble(idx++, emp.getComm());      pstmt.setInt(idx++, emp.getDeptno());            insertInt = pstmt.executeUpdate();    } catch (SQLException e) {      e.printStackTrace();    } finally {      baseDao.dbDisconnection(rs, pstmt, conn);    }        return insertInt;  }  // 删除一名员工  public int empDelete(int empno) {    int deleteResulInt = 0;        BaseDao baseDao = new BaseDao();    try {      conn = baseDao.dbConnection();    } catch (SQLException e1) {      e1.printStackTrace();    }        StringBuffer sqlBf = new StringBuffer();    sqlBf.setLength(0);        sqlBf.append("DELETE FROM EMP      \n");    sqlBf.append("WHERE EMPNO = ?      \n");        try {      pstmt = conn.prepareStatement(sqlBf.toString());      idx = 1;      pstmt.setInt(idx++, empno);            deleteResulInt = pstmt.executeUpdate();    } catch (SQLException e) {      e.printStackTrace();    } finally {      baseDao.dbDisconnection(null, pstmt, conn);    }        return deleteResulInt;  }  }