Add.java
上传用户:sxwtmm
上传日期:2022-08-11
资源大小:2183k
文件大小:3k
源码类别:

OA系统

开发平台:

Java

  1. /*
  2.  * Created on 2004-9-24
  3.  *
  4.  * To change the template for this generated file go to
  5.  * Window>Preferences>Java>Code Generation>Code and Comments
  6.  */
  7. package com.department;
  8. import java.io.IOException;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import javax.servlet.RequestDispatcher;
  13. import javax.servlet.ServletException;
  14. import javax.servlet.http.HttpServlet;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import javax.servlet.http.HttpSession;
  18. import oa.sys.Db;
  19. import oa.sys.Str;
  20. /**
  21.  ****************************************************
  22.  *类名称: Add<br>
  23.  *类功能: 增加部门信息<br>
  24.  *创建: 白伟明 2004年9月23日<br>
  25.  ****************************************************
  26.  * To change the template for this generated type comment go to
  27.  * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
  28.  */
  29. public class Add extends HttpServlet{
  30. private HttpSession session=null;
  31. private Statement stmt=null;
  32. private ResultSet rs=null;
  33. private String name,explain,sqli,sqls;
  34. private int temp=0;
  35. public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
  36. request.setCharacterEncoding("gb2312");
  37. response.setContentType("text/html; charset=gb2312");
  38. Str str=new Str();
  39. Db db=new Db();
  40. session=request.getSession();
  41. name=request.getParameter("name");
  42. explain=request.getParameter("explain");
  43. name=str.inStr(name);
  44. explain=str.inStr(explain);
  45. sqli="INSERT INTO department(name,explain) VALUES('"+name+"','"+explain+"')";
  46. sqls="SELECT * FROM department WHERE name='"+name+"'";
  47. stmt=db.getStmtread();
  48. try {
  49. rs=stmt.executeQuery(sqls);
  50. //查询添加部门是否存在
  51. if(rs.next()){
  52. session.setAttribute("depmsg","该部门已存在");
  53. }else{
  54. rs.close();
  55. stmt.close();
  56. //添加部门
  57. stmt=db.getStmt();
  58. temp=stmt.executeUpdate(sqli);
  59. if(temp>0){
  60. session.setAttribute("depmsg","添加成功");
  61. }else{
  62. session.setAttribute("depmsg","添加失败");
  63. }
  64. }
  65. } catch (SQLException e) {
  66. e.printStackTrace();
  67. }finally{
  68. db.close();
  69. RequestDispatcher dispatcher=request.getRequestDispatcher("add.jsp");
  70. dispatcher.forward(request,response);
  71. }
  72. }
  73. public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
  74. doPost(request,response);
  75. }
  76. }