• CRUD练习


    以Oracle中自带的emp表进行处理,对添加雇员页面进行简单的javaScript验证处理,对分页进行控制端与显示端的分离,添加模糊查找功能。

    1,添加雇员页面,AddEmp.jsp

    View Code
    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <script language="javascript">
    function validateEmpno(eno)
    {
    if(!(/^\d{4}$/.test(eno)))
    {
    document.getElementById(
    "empno_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">雇员编号必须是四位数字!</font>"
    return false;
    }
    else
    {
    document.getElementById(
    "empno_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">雇员编号输入正确!</font>"
    return true;
    }
    }
    function validateEname(ename)
    {
    if(ename=="")
    {
    document.getElementById(
    "ename_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">雇员姓名不能为空!</font>"
    return false;
    }
    else
    {
    document.getElementById(
    "ename_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">雇员姓名输入正确!</font>"
    return true;
    }
    }
    function validateJob(job)
    {
    if(job=="")
    {
    document.getElementById(
    "job_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">雇员岗位不能为空!</font>"
    return false;
    }
    else
    {
    document.getElementById(
    "job_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">雇员岗位输入正确!</font>"
    return true;
    }
    }
    function validateMgr(mgr)
    {
    if(!(/^\d{4}$/.test(mgr)))
    {
    document.getElementById(
    "mgr_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">领导编号必须是四位数字!</font>"
    return false;
    }
    else
    {
    document.getElementById(
    "mgr_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">领导编号输入正确!</font>"
    return true;
    }
    }
    function validateHiredate(hiredate)
    {
    if(!(/^\d{4}-\d{2}-\d{2}$/.test(hiredate)))
    {
    document.getElementById(
    "hiredate_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">雇佣日期必须是yyyy-mm-dd格式的日期!</font>"
    return false;
    }
    else
    {
    document.getElementById(
    "hiredate_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">雇佣日期输入正确!</font>"
    return true;
    }
    }
    function validateSal(sal)
    {
    if(!(/^\d+.\d*$/.test(sal)))
    {
    document.getElementById(
    "sal_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">工资必须是数字,可以包含小数!</font>"
    return false;
    }
    else
    {
    document.getElementById(
    "sal_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">工资输入正确!</font>"
    return true;
    }
    }
    function validateComm(comm)
    {
    if(!(/^\d+.?\d*$/.test(comm)))
    {
    document.getElementById(
    "comm_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">奖金必须是数字,可以包含小数!</font>"
    return false;
    }
    else
    {
    document.getElementById(
    "comm_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">奖金输入正确!</font>"
    return true;
    }
    }
    function validateDeptno(deptno)
    {
    if(!(/^\d{2}$/.test(deptno)))
    {
    document.getElementById(
    "deptno_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">部门编号必须是两位数字!</font>"
    return false;
    }
    else
    {
    document.getElementById(
    "deptno_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">部门编号输入正确!</font>"
    return true;
    }
    }
    function validate(numValue)
    {
    return validateEmpno(numValue.empno.value)&&
    validateEname(numValue.ename.value)
    &&
    validateJob(numValue.job.value)
    &&
    validateMgr(numValue.mgr.value)
    &&
    validateHiredate(numValue.hiredate.value)
    &&
    validateSal(numValue.sal.value)
    &&
    validateComm(numValue.comm.value)
    &&validateDeptno(numValue.deptno.value);
    }
    </script>

    <h1>添加雇员信息</h1>
    <form action="AddEmp_do.jsp" method="post" onSubmit="return validate(this.value)" >
    <table border="1" ,width="100%">
    <tr>
    <td>雇员编号:</td>
    <td><input type="text" name="empno" onBlur="validateEmpno(this.value)" /></td>
    <td><span id="empno_picMsg"><font color="red">*</font></span></td>
    </tr>
    <tr>
    <td>雇员姓名:</td>
    <td><input type="text" name="ename" onBlur="validateEname(this.value)"/></td>
    <td><span id="ename_picMsg"><font color="red">*</font></span></td>
    </tr>
    <tr>
    <td>雇员岗位:</td>
    <td><input type="text" name="job" onBlur="validateJob(this.value)"/></td>
    <td><span id="job_picMsg"><font color="red">*</font></span></td>
    </tr>
    <tr>
    <td>领导编号:</td>
    <td><input type="text" name="mgr" onBlur="validateMgr(this.value)"/></td>
    <td><span id="mgr_picMsg"><font color="red">*</font></span></td>
    </tr>
    <tr>
    <td>雇佣日期:</td>
    <td><input type="text" name="hiredate" onBlur="validateHiredate(this.value)"/></td>
    <td><span id="hiredate_picMsg"><font color="red">*</font></span></td>
    </tr>
    <tr>
    <td>雇员工资:</td>
    <td><input type="text" name="sal" onBlur="validateSal(this.value)"/></td>
    <td><span id="sal_picMsg"><font color="red">*</font></span></td>
    </tr>
    <tr>
    <td>雇员奖金:</td>
    <td><input type="text" name="comm" onBlur="validateComm(this.value)"/></td>
    <td><span id="comm_picMsg"><font color="red">*</font></span></td>
    </tr>
    <tr>
    <td>部门编号:</td>
    <td><input type="text" name="deptno" onBlur="validateDeptno(this.value)"/></td>
    <td><span id="deptno_picMsg"><font color="red">*</font></span></td>
    </tr>
    <tr>
    <input type="hidden" name="cp" value="<%=request.getParameter("cp")%>"/>
    <input type="hidden" name="ls" value="<%=request.getParameter("ls")%>"/>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="添加"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="重置"/></td>
    </tr>
    </table>
    </form>

    2,添加雇员的后台处理页面,AddEmp_do.jsp

    View Code
     1 <%@ page contentType="text/html" pageEncoding="GBK"%>
    2 <%@ page import="java.sql.*,java.text.*"%>
    3 <%
    4 request.setCharacterEncoding("GBK");
    5 %>
    6 <%
    7 int empno=Integer.parseInt(request.getParameter("empno"));
    8 String ename=request.getParameter("ename");
    9 String job=request.getParameter("job");
    10 int mgr=Integer.parseInt(request.getParameter("mgr"));
    11 java.sql.Date hiredate=null;
    12 try
    13 {
    14 hiredate=new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("hiredate")).getTime());
    15 }
    16 catch(Exception ex)
    17 {
    18
    19 }
    20 float sal=Float.parseFloat(request.getParameter("sal"));
    21 float comm=Float.parseFloat(request.getParameter("comm"));
    22 int deptnp=Integer.parseInt(request.getParameter("deptno"));
    23 %>
    24 <%
    25 String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
    26 String DBURL = "jdbc:oracle:thin:@localhost:1521:study" ;
    27 String DBUSER = "scott" ;
    28 String DBPASSWORD = "tiger" ;
    29 %>
    30 <%
    31 Connection conn=null;
    32 PreparedStatement psmt=null;
    33 ResultSet rst=null;
    34 %>
    35
    36 <%
    37 String msg="雇员添加失败!";
    38 Class.forName(DBDRIVER);
    39 conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
    40 String sql="insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values(?,?,?,?,?,?,?,?)";
    41 psmt=conn.prepareStatement(sql);
    42 psmt.setInt(1,empno);
    43 psmt.setString(2,ename);
    44 psmt.setString(3,job);
    45 psmt.setInt(4,mgr);
    46 psmt.setDate(5,hiredate);
    47 psmt.setFloat(6,sal);
    48 psmt.setFloat(7,comm);
    49 psmt.setInt(8,deptnp);
    50 try
    51 {
    52 int n=psmt.executeUpdate();
    53 if(n>0)
    54 {
    55 msg="雇员添加成功!";
    56 }
    57 }
    58 catch(Exception ex)
    59 {
    60 ex.printStackTrace();
    61 }
    62
    63 %>
    64 <script language="javascript">
    65 alert("<%=msg%>");
    66 window.location="fuck_page_source.jsp?cp=<%=request.getParameter("cp")%>&ls=<%=request.getParameter("ls")%>";
    67 </script>

    3,分页的控制端页面,fuck_page_control.jsp

    View Code
     1 <%@ page contentType="text/html" pageEncoding="GBK"%>
    2 <%
    3 int currentPage =1;
    4 int lineSize=3;
    5 long allRecorders=0;
    6 long pageSize=1;
    7 int[] lineData={1,3,5,7,9,10,12,14};
    8 String keyword=null;
    9 String url=request.getParameter("url");
    10 %>
    11 <%
    12 try
    13 {
    14 allRecorders=Long.parseLong(request.getParameter("Recorders"));
    15 }
    16 catch(Exception ex)
    17 {
    18 ex.printStackTrace();
    19 }
    20 try
    21 {
    22 currentPage=Integer.parseInt(request.getParameter("cp"));
    23 lineSize=Integer.parseInt(request.getParameter("ls"));
    24 keyword=request.getParameter("kw");
    25 }
    26 catch(Exception ex)
    27 {
    28 }
    29 if(keyword==null)
    30 {
    31 keyword="";
    32 }
    33 pageSize = (allRecorders + lineSize -1) / lineSize ;
    34
    35 if(pageSize == 0)
    36 {
    37 pageSize = 1 ;
    38 }
    39 %>
    40
    41 <center>
    42 <h1>雇员信息列表</h1><br/>
    43 <a href="AddEmp.jsp?cp=<%=currentPage%>&ls=<%=lineSize%>"> 添加雇员</a>
    44 <script language="javascript">
    45 function go(num)
    46 {
    47 document.getElementById("cp").value=num;
    48 document.pageForm.submit();
    49 }
    50 </script>
    51 <form name="pageForm" action="<%=url%>" method="post">
    52 请输入查询关键字:<input type="text" name="kw" value="<%=keyword%>"/> <input type="submit" value="查询"/><br/><br/>
    53 <input type="button" value="首页" onClick="go(1)" <%=currentPage==1?"disabled":""%>/>
    54 <input type="button" value="上一页" onclick="go(<%=currentPage-1%>)" <%=currentPage==1?"disabled":""%>/>
    55 <input type="button" value="下一页" onclick="go(<%=currentPage+1%>)" <%=currentPage==pageSize?"disabled":""%>/>
    56 <input type="button" value="尾页" onclick="go(<%=pageSize%>)" <%=currentPage==pageSize?"disabled":""%>/>
    57 跳转至<select onChange="go(this.value)">
    58 <%for(int i=1;i<=pageSize;i++)
    59 {
    60 %>
    61 <option value=<%=i%> <%=i==currentPage?"selected":""%>><%=i%></option>
    62 <%
    63 }
    64 %>
    65 </select>
    66
    67 每页显示<select name="ls" onChange="go(1)">
    68 <%for(int i=0;i<lineData.length;i++)
    69 {
    70 %>
    71 <option value=<%=lineData[i]%> <%=lineSize==lineData[i]?"selected":""%>><%=lineData[i]%></option>
    72 <%
    73 }
    74 %>
    75 </select>
    76 条记录
    77 <input type="hidden" value="1" name="cp" />
    78 </form>
    79 </center>


    4,分页的显示页面,fuck_page_source.jsp

    View Code
      1 <%@ page contentType="text/html" pageEncoding="GBK"%>
    2 <%@ page import="java.sql.*"%>
    3 <%
    4 request.setCharacterEncoding("GBK");
    5 String url="fuck_page_source.jsp";
    6 %>
    7 <%
    8 int currentPage =1;
    9 int lineSize=3;
    10 int allRecorders=0;
    11 String keyword=null;
    12 int count=0;
    13 %>
    14 <%
    15 try
    16 {
    17 currentPage=Integer.parseInt(request.getParameter("cp"));
    18 lineSize=Integer.parseInt(request.getParameter("ls"));
    19 keyword=request.getParameter("kw");
    20 }
    21 catch(Exception ex)
    22 {
    23 }
    24 if(keyword==null)
    25 {
    26 keyword="";
    27 }
    28 %>
    29 <%
    30 String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
    31 String DBURL = "jdbc:oracle:thin:@localhost:1521:study" ;
    32 String DBUSER = "scott" ;
    33 String DBPASSWORD = "tiger" ;
    34 %>
    35 <%
    36 Connection conn=null;
    37 PreparedStatement psmt=null;
    38 ResultSet rst=null;
    39 %>
    40
    41 <%
    42 Class.forName(DBDRIVER);
    43 conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
    44 String sql="select count(empno) from emp where empno like ? or ename like ? or job like ? or mgr like ? or to_char(hiredate,'yyyy-mm-dd') like ? or sal like ? or comm like ? or deptno like ?";
    45 psmt=conn.prepareStatement(sql);
    46 psmt.setString(1,"%"+keyword+"%");
    47 psmt.setString(2,"%"+keyword+"%");
    48 psmt.setString(3,"%"+keyword+"%");
    49 psmt.setString(4,"%"+keyword+"%");
    50 psmt.setString(5,"%"+keyword+"%");
    51 psmt.setString(6,"%"+keyword+"%");
    52 psmt.setString(7,"%"+keyword+"%");
    53 psmt.setString(8,"%"+keyword+"%");
    54 try
    55 {
    56 rst=psmt.executeQuery();
    57 if(rst.next())
    58 {
    59 allRecorders=rst.getInt(1);
    60 }
    61 }
    62 catch(Exception ex)
    63 {
    64 ex.printStackTrace();
    65 }
    66
    67 %>
    68
    69 <center>
    70 <jsp:include page="fuck_page_control.jsp">
    71 <jsp:param name="url" value="<%=url%>" />
    72 <jsp:param name="Recorders" value="<%=allRecorders%>" />
    73 </jsp:include>
    74 </center>
    75
    76 <table border="1" width="100%">
    77 <tr>
    78 <th>雇员编号</th>
    79 <th>雇员姓名</th>
    80 <th>雇员职位</th>
    81 <th>领导姓名</th>
    82 <th>雇佣日期</th>
    83 <th>雇员工资</th>
    84 <th>雇员奖金</th>
    85 <th>部门编号</th>
    86 <th colspan="2">操作</th>
    87 </tr>
    88
    89 <%
    90 try
    91 {
    92 sql="select * from (SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno,ROWNUM rn FROM emp WHERE (empno like ? or ename like ? or job like ? or mgr like ? or to_char(hiredate,'yyyy-mm-dd') like ? or sal like ? or comm like ? or deptno like ?) and ROWNUM<=? ORDER BY empno) temp where temp.rn>?";
    93 psmt=conn.prepareStatement(sql);
    94 psmt.setString(1,"%"+keyword+"%");
    95 psmt.setString(2,"%"+keyword+"%");
    96 psmt.setString(3,"%"+keyword+"%");
    97 psmt.setString(4,"%"+keyword+"%");
    98 psmt.setString(5,"%"+keyword+"%");
    99 psmt.setString(6,"%"+keyword+"%");
    100 psmt.setString(7,"%"+keyword+"%");
    101 psmt.setString(8,"%"+keyword+"%");
    102 psmt.setInt(9,currentPage*lineSize);
    103 psmt.setInt(10,(currentPage-1)*lineSize);
    104 rst=psmt.executeQuery();
    105 while(rst.next())
    106 {
    107 count++;
    108 int empno=rst.getInt(1);
    109 String ename=rst.getString(2);
    110 String job=rst.getString(3);
    111 String mgr=rst.getString(4);
    112 Date hiredate=rst.getDate(5);
    113 float sal=rst.getFloat(6);
    114 float comm=rst.getFloat(7);
    115 int deptno=rst.getInt(8);
    116 %>
    117 <tr>
    118 <th><%=empno%></th>
    119 <th><%=ename%></th>
    120 <th><%=job%></th>
    121 <th><%=mgr%></th>
    122 <th><%=hiredate%></th>
    123 <th><%=sal%></th>
    124 <th><%=comm%></th>
    125 <th><%=deptno%></th>
    126 <th><a href="UpdateEmp.jsp?empno=<%=empno%>&cp=<%=currentPage%>&ls=<%=lineSize%>">修改</a></th>
    127 <th><a href="DeleteEmp.jsp?empno=<%=empno%>&cp=<%=currentPage%>&ls=<%=lineSize%>">删除</a></th>
    128 </tr>
    129 <%
    130 }
    131
    132 }
    133 catch(Exception ex)
    134 {
    135 ex.printStackTrace();
    136 }
    137 finally
    138 {
    139 try
    140 {
    141 conn.close();
    142 }
    143 catch(Exception ex)
    144 {
    145 ex.printStackTrace();
    146 }
    147 }
    148 %>
    149 <%
    150 if(count==0)
    151 {
    152 %>
    153 <tr>
    154 <th colspan="8" >没有符合条件的数据!</th>
    155 </tr>
    156 <%
    157 }
    158 %>
    159 </table>

    5,更新雇员信息页面,UpdateEmp.jsp

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <%@ page import="java.sql.*"%>
    <%
    	request.setCharacterEncoding("GBK");
    %>
    <script language="javascript">
    	function validateEname(ename)
    	{
    		if(ename=="")
    		{
    			document.getElementById("ename_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">雇员姓名不能为空!</font>"
    			return false;																										
    		}
    		else
    		{
    			document.getElementById("ename_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">雇员姓名输入正确!</font>"
    			return true;
    		}
    	}
    	function validateJob(job)
    	{
    		if(job=="")
    		{
    			document.getElementById("job_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">雇员岗位不能为空!</font>"
    			return false;																										
    		}
    		else
    		{
    			document.getElementById("job_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">雇员岗位输入正确!</font>"
    			return true;
    		}
    	}
    	function validateMgr(mgr)
    	{
    		if(!(/^\d{4}$/.test(mgr)))
    		{
    			document.getElementById("mgr_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">领导编号必须是四位数字!</font>"
    			return false;																										
    		}
    		else
    		{
    			document.getElementById("mgr_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">领导编号输入正确!</font>"
    			return true;
    		}
    	}
    	function validateHiredate(hiredate)
    	{
    		if(!(/^\d{4}-\d{2}-\d{2}$/.test(hiredate)))	
    		{
    			document.getElementById("hiredate_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">雇佣日期必须是yyyy-mm-dd格式的日期!</font>"
    			return false;																										
    		}
    		else
    		{
    			document.getElementById("hiredate_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">雇佣日期输入正确!</font>"
    			return true;
    		}
    	}
    	function validateSal(sal)
    	{
    		if(!(/^\d+.\d*$/.test(sal)))
    		{
    			document.getElementById("sal_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">工资必须是数字,可以包含小数!</font>"
    			return false;																										
    		}
    		else
    		{
    			document.getElementById("sal_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">工资输入正确!</font>"
    			return true;
    		}
    	}
    	function validateComm(comm)
    	{
    		if(!(/^\d+.?\d*$/.test(comm)))		
    		{
    			document.getElementById("comm_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">奖金必须是数字,可以包含小数!</font>"
    			return false;																										
    		}
    		else
    		{
    			document.getElementById("comm_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">奖金输入正确!</font>"
    			return true;
    		}
    	}
    	function validateDeptno(deptno)
    	{
    		if(!(/^\d{2}$/.test(deptno)))
    		{
    			document.getElementById("deptno_picMsg").innerHTML="<img src=\"images/wrong.gif\">"+"<font color=\"red\">部门编号必须是两位数字!</font>"
    			return false;																										
    		}
    		else
    		{
    			document.getElementById("deptno_picMsg").innerHTML="<img src=\"images/right.gif\">"+"<font color=\"green\">部门编号输入正确!</font>"
    			return true;
    		}
    	}
    	function validate(numValue)
    	{
    		return validateEname(numValue.ename.value)&&
    					 validateJob(numValue.job.value)&&
    					 validateMgr(numValue.mgr.value)&&
    					 validateHiredate(numValue.hiredate.value)&&
    					 validateSal(numValue.sal.value)&&
    					 validateComm(numValue.comm.value)
    					 &&validateDeptno(numValue.deptno.value);
    	}
    </script>
    <%
    	int empno=0;
    	try
    	{
    		empno=Integer.parseInt(request.getParameter("empno"));
    	}
    	catch(Exception ex)
    	{
    	}
    %>
    <%
    	String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
    	String DBURL = "jdbc:oracle:thin:@localhost:1521:study" ;
    	String DBUSER = "scott" ;
    	String DBPASSWORD = "tiger" ;
    	
    	String ename=null;;
    	String job=null;
    	int mgr=0;
    	Date hiredate=null;
    	float sal=0;
    	float comm=0;
    	int deptno=0;
    %>
    <%
    	Connection conn=null;
    	PreparedStatement psmt=null;
    	ResultSet rst=null;
    %>
    
    <%
    	Class.forName(DBDRIVER);
    	conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);	
    	String sql="select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp where empno=?";
    	psmt=conn.prepareStatement(sql);
    	psmt.setInt(1,empno);
    	try
    	{
    		rst=psmt.executeQuery();
    		if(rst.next())
    		{
    			empno=rst.getInt(1);
    			ename=rst.getString(2);
    			job=rst.getString(3);
    			mgr=rst.getInt(4);
    			hiredate=rst.getDate(5);
    			sal=rst.getFloat(6);
    			comm=rst.getFloat(7);
    			deptno=rst.getInt(8);
    %>	
    <h1>修改雇员信息</h1>
    <form action="UpdateEmp_do.jsp" method="post" onSubmit="return validate(this.value)" >
    			<table border="1" ,width="100%">	
    				<tr>
    					<td>雇员编号:</td>
    					<td><input type="text" name="empno" value="<%=empno%>" readOnly="true"/></td>					
    				</tr>
    				<tr>
    					<td>雇员姓名:</td>
    					<td><input type="text" name="ename" onBlur="validateEname(this.value)" value="<%=ename%>"/></td>
    					<td><span id="ename_picMsg"><font color="red">*</font></span></td>
    				</tr>
    				<tr>
    					<td>雇员岗位:</td>
    					<td><input type="text" name="job" onBlur="validateJob(this.value)" value="<%=job%>"/></td>
    					<td><span id="job_picMsg"><font color="red">*</font></span></td>
    				</tr>
    				<tr>
    					<td>领导编号:</td>
    					<td><input type="text" name="mgr" onBlur="validateMgr(this.value)" value="<%=mgr%>"/></td>
    					<td><span id="mgr_picMsg"><font color="red">*</font></span></td>
    				</tr>
    				<tr>
    					<td>雇佣日期:</td>
    					<td><input type="text" name="hiredate" onBlur="validateHiredate(this.value)" value="<%=hiredate%>"/></td>
    					<td><span id="hiredate_picMsg"><font color="red">*</font></span></td>
    				</tr>
    				<tr>
    					<td>雇员工资:</td>
    					<td><input type="text" name="sal" onBlur="validateSal(this.value)" value="<%=sal%>"/></td>
    					<td><span id="sal_picMsg"><font color="red">*</font></span></td>
    				</tr>
    				<tr>
    					<td>雇员奖金:</td>
    					<td><input type="text" name="comm" onBlur="validateComm(this.value)" value="<%=comm%>"/></td>
    					<td><span id="comm_picMsg"><font color="red">*</font></span></td>
    				</tr>
    				<tr>
    					<td>部门编号:</td>
    					<td><input type="text" name="deptno" onBlur="validateDeptno(this.value)" value="<%=deptno%>"/></td>
    					<td><span id="deptno_picMsg"><font color="red">*</font></span></td>
    				</tr>
    				<tr>
    					<input type="hidden" name="cp" value="<%=request.getParameter("cp")%>"/>
    					<input type="hidden" name="ls" value="<%=request.getParameter("ls")%>"/>
    					<td>    <input type="submit" value="修改"/>          <input type="reset" value="重置"/></td>
    				</tr>
    			</table>
    </form>
    <%
    			}
    	}
    	catch(Exception ex)
    	{
    		ex.printStackTrace();
    	}
    	finally
    	{
    		try
    		{
    			conn.close();
    		}
    		catch(Exception ex)
    		{
    			ex.printStackTrace();
    		}
    	}
    %>
    

    5,更新雇员后台处理页面,UpdateEmp_do.jsp 

    View Code
     1 <%@ page contentType="text/html" pageEncoding="GBK"%>
    2 <%@ page import="java.sql.*,java.text.*"%>
    3 <%
    4 request.setCharacterEncoding("GBK");
    5 %>
    6 <%
    7 int empno=0;
    8 try
    9 {
    10 empno=Integer.parseInt(request.getParameter("empno"));
    11 }
    12 catch(Exception ex)
    13 {
    14
    15 }
    16 String ename=request.getParameter("ename");
    17 String job=request.getParameter("job");
    18 int mgr=Integer.parseInt(request.getParameter("mgr"));
    19 java.sql.Date hiredate=null;
    20 try
    21 {
    22 hiredate=new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("hiredate")).getTime());
    23 }
    24 catch(Exception ex)
    25 {
    26
    27 }
    28 float sal=Float.parseFloat(request.getParameter("sal"));
    29 float comm=Float.parseFloat(request.getParameter("comm"));
    30 int deptno=Integer.parseInt(request.getParameter("deptno"));
    31 %>
    32 <%
    33 String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
    34 String DBURL = "jdbc:oracle:thin:@localhost:1521:study" ;
    35 String DBUSER = "scott" ;
    36 String DBPASSWORD = "tiger" ;
    37 %>
    38 <%
    39 Connection conn=null;
    40 PreparedStatement psmt=null;
    41 ResultSet rst=null;
    42 %>
    43
    44 <%
    45 String msg="雇员修改失败!";
    46 Class.forName(DBDRIVER);
    47 conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
    48 String sql="UPDATE emp SET ename=?,job=?,mgr=?,hiredate=?,sal=?,comm=?,deptno=? WHERE empno=?";
    49 psmt=conn.prepareStatement(sql);
    50 psmt.setString(1,ename);
    51 psmt.setString(2,job);
    52 psmt.setInt(3,mgr);
    53 psmt.setDate(4,hiredate);
    54 psmt.setFloat(5,sal);
    55 psmt.setFloat(6,comm);
    56 psmt.setInt(7,deptno);
    57 psmt.setInt(8,empno);
    58 try
    59 {
    60 int n=psmt.executeUpdate();
    61 if(n>0)
    62 {
    63 msg="雇员修改成功!";
    64 }
    65 }
    66 catch(Exception ex)
    67 {
    68 ex.printStackTrace();
    69 }
    70 finally
    71 {
    72 try
    73 {
    74 conn.close();
    75 }
    76 catch(Exception ex)
    77 {
    78 ex.printStackTrace();
    79 }
    80 }
    81
    82 %>
    83 <script language="javascript">
    84 alert("<%=msg%>");
    85 window.location="fuck_page_source.jsp?cp=<%=request.getParameter("cp")%>&ls=<%=request.getParameter("ls")%>";
    86 </script>

    6,删除雇员页面,DeleteEmp.jsp

    View Code
     1 <%@ page contentType="text/html" pageEncoding="GBK"%>
    2 <%@ page import="java.sql.*"%>
    3 <%
    4 request.setCharacterEncoding("GBK");
    5 %>
    6 <%
    7 int empno=0;
    8 try
    9 {
    10 empno=Integer.parseInt(request.getParameter("empno"));
    11 }
    12 catch(Exception ex)
    13 {
    14
    15 }
    16 %>
    17 <%
    18 String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
    19 String DBURL = "jdbc:oracle:thin:@localhost:1521:study" ;
    20 String DBUSER = "scott" ;
    21 String DBPASSWORD = "tiger" ;
    22 %>
    23 <%
    24 Connection conn=null;
    25 PreparedStatement psmt=null;
    26 ResultSet rst=null;
    27 %>
    28
    29 <%
    30 String msg="雇员删除失败!";
    31 Class.forName(DBDRIVER);
    32 conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
    33 String sql="DELETE FROM EMP WHERE empno=?";
    34 psmt=conn.prepareStatement(sql);
    35 psmt.setInt(1,empno);
    36 try
    37 {
    38 int n=psmt.executeUpdate();
    39 System.out.println(n);
    40 if(n>0)
    41 {
    42 msg="雇员删除成功!";
    43 }
    44 }
    45 catch(Exception ex)
    46 {
    47 ex.printStackTrace();
    48 }
    49 finally
    50 {
    51 try
    52 {
    53 conn.close();
    54 }
    55 catch(Exception ex)
    56 {
    57 ex.printStackTrace();
    58 }
    59 }
    60
    61 %>
    62 <script language="javascript">
    63 alert("<%=msg%>");
    64 window.location="fuck_page_source.jsp?cp=<%=request.getParameter("cp")%>&ls=<%=request.getParameter("ls")%>";
    65 </script>

    -----------------总结:通过地址重写的方式完成了CRUD操作,但代码重复的太多了。






  • 相关阅读:
    华为笔试
    进程间通信——管道通信
    进程间通信——使用WM_COPYDATA消息通信
    混合高斯背景建模
    基于mean shift的目标跟踪算法
    Lukas-Kanade光流法
    【CUDA并行编程之八】Cuda实现Kmeans算法
    【CUDA并行编程之七】数组元素之和
    【CUDA并行编程之六】KNN算法的并行实现
    【CUDA并行编程之五】计算向量的欧式距离
  • 原文地址:https://www.cnblogs.com/xiongyu/p/2353328.html
Copyright © 2020-2023  润新知