zoukankan      html  css  js  c++  java
  • 学习实例.文章管理.文章类.Article.java

    package business;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.text.*;
    import java.sql.ResultSet;
    
    public class Article extends HttpServlet {
      String ID,Title,Content,Author,AddDate;
      private static final long serialVersionUID = 3617859655330969141L;
      //数据库操作对象
      DBUtil Conn;
    
      /**
       * Constructor of the object.
       */
      public Article() {
        super();
        Conn = new DBUtil();
      }
    
      public String getAddDate() {
        return AddDate;
      }
    
      public void setAddDate(String addDate) {
        AddDate = addDate;
      }
    
      public String getAuthor() {
        return Author;
      }
    
      public void setAuthor(String author) {
        Author = author;
      }
    
      public String getContent() {
        return Content;
      }
    
      public void setContent(String content) {
        Content = content;
      }
    
      public String getTitle() {
        return Title;
      }
    
      public void setTitle(String title) {
        Title = title;
      }
      
      public String getID() {
        return ID;
      }
    
      public void setID(String id) {
        ID = id;
      }
    
      /**
       * Destruction of the servlet. <br>
       */
      public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
        Conn.destroy();
      }
    
      /**
        
       */
      public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
        try{
          request.setCharacterEncoding("GBK");
          response.setContentType("text/html");
          response.setCharacterEncoding("GBK");
          PrintWriter out = response.getWriter();
          out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
          out.println("<HTML>");
          out.println("  <HEAD><TITLE>保存文章</TITLE></HEAD>");
          out.println("  <BODY>");
          String Action=request.getParameter("Action");
          ID = request.getParameter("ID");
          if(Action=="Del"){
            if(!ID.matches("[0-9]+")){
              out.print("参数错误!");
            }
            boolean blnTemp = delete();
            if(blnTemp){
              
            }
          }else{
            out.print("错误的请求!");
          }
          out.println("  </BODY>");
          out.println("</HTML>");
          out.flush();
          out.close();  
        }catch(Exception e){
          System.out.print("Article doGet:" + e.getMessage());
        }
      }
    
      /**
      POST取值
       */
      public void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
        try{
        
          request.setCharacterEncoding("GBK");
          response.setCharacterEncoding("GBK");
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          //取得客户端数据
          String Action=request.getParameter("Action");
          ID = request.getParameter("ID");
          Title = request.getParameter("Title");
          Content = request.getParameter("Content");
          Author = request.getParameter("Author");
          AddDate = GetDate();
          //根据Action执行操作
          int intTemp = 0;
          if(Action.equals("SaveAdd")){
            intTemp = Insert(request);
          }else if(Action.equals("SaveEdit")){
            if(ID.matches("[0-9]+")){
              intTemp = Update(request);
            }
          }
          //输出结果
          out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
          out.println("<HTML>");
          out.println("  <HEAD><TITLE>保存文章</TITLE></HEAD>");
          out.println("  <BODY>");
    
          if(intTemp>0){
            out.print("保存数据成功!");
            out.print("<a href=../index.jsp>返回</a>");
          }else{
            
          }
          out.println("  </BODY>");
          out.println("</HTML>");
          out.flush();
          out.close();
        }catch(Exception e){
          System.out.print("Article doPost:" + e.getMessage());
        }
      }
      //写入
      private int Insert(HttpServletRequest request){
        String sql;
        int intTemp=0;
        sql = "";
        try{
          request.setCharacterEncoding("GBK");
          sql = "Insert Into Articles (Title,Content,Author,AddDate) Values ('";
          sql += Title;
          sql += "','" + Content;
          sql += "','" + Author;
          sql += "','" + AddDate;
          sql += "')";
          intTemp = Conn.Update(sql);
        }catch(Exception e){
          System.out.print("Article Insert:" + e.getMessage());
        }
        return intTemp;
      }
      //更新
      private int Update(HttpServletRequest request){
        String sql;
        int intTemp=0;
        try{
          request.setCharacterEncoding("GBK");
          sql = "Update Articles Set ";
          sql += "Title='" + Title;
          sql += "',Content='" + Content;
          sql += "',Author='" + Author;
          sql += "' where ID=" + ID;
          intTemp = Conn.Update(sql);
        }catch(Exception e){
          System.out.print("Article Insert:" + e.getMessage());
        }
        return intTemp;
      }
      //删除
      private boolean delete() throws Exception{
        boolean blnTemp = false;
        select();
        if(Title!=null){
          String sql = "delete From Articles where ID=" + ID;
          int intTemp = 0;
          intTemp = Conn.Execute(sql, intTemp);
          if(intTemp>0){blnTemp = true;}
        }
        return blnTemp;
      }
      //查询
      public void select(){
        try{
          String sql;
          sql = "select * From Articles where ID=" + ID;
          ResultSet rs = Conn.Execute(sql);
          while(rs.next()){
            ID = rs.getString("ID");
            Title = rs.getString("Title");
            Content = rs.getString("Content");
            Author = rs.getString("Author");
          }
        }catch(Exception e){
          System.out.print("Article Select:" + e.getMessage());
        }
      }
      /**
       * Initialization of the servlet. <br>
       *
       * @throws ServletException if an error occure
       */
      public void init() throws ServletException {
        try{
          
        }catch(Exception e){
          System.out.print("Article Init:" + e.getMessage());
        }
      }
      private String GetDate(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        java.util.Date currentTime = new java.util.Date();//得到当前系统时间 
        String str_date1 = formatter.format(currentTime); //将日期时间格式化 
        //String str_date2 = currentTime.toString(); //将Date型日期时间转换成字符串形式 
        return str_date1;
      }
    }
  • 相关阅读:
    API下载文件
    c# 测试网络连接
    C# Word 插入签名图片
    c# word文档合并
    c# 文件筛选
    e
    基本初等函数(Basic elementary function)
    前端性能优化学习
    解决点击穿透的最佳实践
    ObjectARX通过选定的实体获取所有组名示例
  • 原文地址:https://www.cnblogs.com/blackice/p/2618270.html
Copyright © 2011-2022 走看看