您好,欢迎访问三七文档
JSP内置对象1、完成雇员列表,列表时取得部门名称(尽量不关联查)登陆成功后,通过2秒跳转的形式进入列表页if(rs.next()){response.setHeader(refresh,2;URL=+request.getContextPath()+/pages/emp/emp_list.jsp);%用户登陆成功!br/当前登陆用户为:%=rs.getString(1)%%}else{%jsp:forwardpage=/index.jsp/jsp:forward%}修改列表,先查询出所有的部门,并保存到Map中。body%!finalStringDBDRIVER=oracle.jdbc.driver.OracleDriver;finalStringDBURL=jdbc:oracle:thin:@localhost:1521:MLDN;finalStringDBUSER=scott;finalStringDBPASSWORD=tiger;Connectionconn=null;%%ResultSetrs=null;PreparedStatementpstmt=null;PreparedStatementpstmtDept=null;ResultSetrsDept=null;//接收传递过来的编号Stringdeptno=request.getParameter(deptno);try{//连接数据库Class.forName(DBDRIVER);conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);//先查询出全部的部门,信息保存到Map对象中Mapmap=newHashMap();StringdeptSql=SELECTdeptno,dnameFROMdept;pstmtDept=conn.prepareStatement(deptSql);rsDept=pstmtDept.executeQuery();while(rsDept.next()){map.put(rsDept.getInt(1),rsDept.getString(2));}Stringsql=SELECT*FROMemp;pstmt=conn.prepareStatement(sql);rs=pstmt.executeQuery();%tableborder=1width=80%trtd雇员编号/tdtd雇员姓名/tdtd雇员职位/tdtd雇员领导/tdtd入职时间/tdtd薪水/tdtd奖金/tdtd部门名称/td/tr%while(rs.next()){%trtd%=rs.getInt(1)%/tdtd%=rs.getString(2)%/tdtd%=rs.getString(3)%/tdtd%=rs.getInt(4)%/tdtd%=rs.getDate(5)%/tdtd%=rs.getDouble(6)%/tdtd%=rs.getDouble(7)%/tdtd%=map.get(rs.getInt(8))%/td/tr%}%/table%}catch(Exceptione){e.printStackTrace();}finally{if(rs!=null)rs.close();if(rsDept!=null)rsDept.close();if(pstmt!=null)pstmt.close();if(pstmtDept!=null)pstmtDept.close();if(conn!=null)conn.close();}%/body在列表页,加入一个超连接,连接到添加页%@pagelanguage=javaimport=java.sql.*,java.util.*pageEncoding=GBK%!DOCTYPEHTMLPUBLIC-//W3C//DTDHTML4.01Transitional//ENhtmlheadtitle雇员添加/title/headbody%@includefile=/pages/dbc.jsp%%try{%formaction=%=request.getContextPath()%/pages/emp/emp_insert_do.jspmethod=post雇员编号:inputtype=textname=empno/br/雇员姓名:inputtype=textname=ename/br/雇员职位:inputtype=textname=job/br/雇员领导:selectname=mgroptionvalue=0无/option%Stringsql=SELECTempno,enameFROMemp;pstmt=conn.prepareStatement(sql);rs=pstmt.executeQuery();while(rs.next()){%optionvalue=%=rs.getInt(1)%%=rs.getString(2)%/option%}%/selectbr/入职时间:inputtype=textname=hiredate/br/薪水:inputtype=textname=sal/br/奖金:inputtype=textname=comm/br/部门:selectname=deptno%sql=SELECTdeptno,dnameFROMdept;pstmt=conn.prepareStatement(sql);rs=pstmt.executeQuery();while(rs.next()){%optionvalue=%=rs.getInt(1)%%=rs.getString(2)%/option%}%/selectbr/inputtype=submitvalue=添加//form%}catch(Exceptione){e.printStackTrace();}finally{if(conn!=null)conn.close();}%/body/html提交后需要在insert_do.jsp中进行数据库操作。%@pagelanguage=javaimport=java.sql.*,java.util.*,java.text.*pageEncoding=GBK%!DOCTYPEHTMLPUBLIC-//W3C//DTDHTML4.01Transitional//ENhtmlheadtitle雇员添加/title/headbody%@includefile=/pages/dbc.jsp%%try{intempno=Integer.parseInt(request.getParameter(empno));Stringename=request.getParameter(ename);Stringjob=request.getParameter(job);intmgr=Integer.parseInt(request.getParameter(mgr));SimpleDateFormatsf=newSimpleDateFormat(yyyy-MM-dd);java.util.Datehiredate=sf.parse(request.getParameter(hiredate));doublesal=Double.parseDouble(request.getParameter(sal));doublecomm=Double.parseDouble(request.getParameter(comm));intdeptno=Integer.parseInt(request.getParameter(deptno));StringsearchSql=SELECTempnoFROMempWHEREempno=?;pstmt=conn.prepareStatement(searchSql);pstmt.setInt(1,empno);rs=pstmt.executeQuery();if(rs.next()){%输入雇员编号重复,请返回重新输入!br/inputtype=buttonvalue=返回onclick=history.go(-1);/%}else{//编写sql语句Stringsql=INSERTINTOempVALUES(?,?,?,?,?,?,?,?);pstmt=conn.prepareStatement(sql);pstmt.setInt(1,empno);pstmt.setString(2,ename);pstmt.setString(3,job);pstmt.setInt(4,mgr);pstmt.setDate(5,newjava.sql.Date(hiredate.getTime()));pstmt.setDouble(6,sal);pstmt.setDouble(7,comm);pstmt.setInt(8,deptno);if(pstmt.executeUpdate()0){response.setHeader(refresh,2;URL=+request.getContextPath()+/pages/emp/emp_list.jsp);%添加成功!br/2秒后返回列表页。%}}}catch(NumberFormatExceptione){%输入的数字格式不合法,请返回重新输入!br/inputtype=buttonvalue=返回onclick=history.go(-1);/%}catch(ParseExceptione){%入职时间格式不正确,按照年-月-日的格式输入,例如:2010-11-12br/inputtype=buttonvalue=返回onclick=history.go(-1);/%}catch(Exceptione){e.printStackTrace();}finally{if(conn!=null)conn.close();}%/body/html列表页加入一个按钮%while(rs.next()){%trtd%=rs.getInt(1)%/tdtd%=rs.getString(2)%/tdtd%=rs.getString(3)%/tdtd%=rs.getInt(4)%/tdtd%=rs.getDate(5)%/tdtd%=rs.getDouble(6)%/tdtd%=rs.getDouble(7)%/tdtd%=map.get(rs.getInt(8))%/tdtdinputtype=buttonvalue=删除onclick=del('%=rs.getInt(1)%');//td/tr%}%scripttype=text/javascriptfunctiondel(empno){if(confirm(确定要删除该数据吗?)){location=%=request.getContextPath()%/pages/emp/emp_delete.jsp?empno=+empno;}}/script完成修改,先在列表页加入超连接或按钮。functionupd(empno){location=%=request.getContextPath
本文标题:JSP内置对象
链接地址:https://www.777doc.com/doc-4458202 .html