团队项目——第一阶段冲刺7

一、前言

1.昨天完成了:

第三方登录

2.今天完成了:

将用户爱好选择存入数据库

二、成果展示

 三、逻辑代码

1.web端-servlet

@WebServlet("/news")
public class NewsServlet_ extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private NewsDao dao = new NewsDao();
    public NewsServlet_() {
        super();
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         response.setHeader("content-type", "text/html;charset=UTF-8");
         request.setCharacterEncoding("UTF-8");

            String method = request.getParameter("method");
            System.out.println(method);
            if (method.equals("allnews")){
                allnews(request,response);
            }else if (method.equals("search")){
                search(request,response);
            }else if (method.equals("addUser")){
                addUser(request,response);
            }else if (method.equals("addComment")){
                addComment(request,response);
            }else if (method.equals("commentList")){
                commentList(request,response);
            }else if (method.equals("setLove")){
                setLove(request,response);
            }
    }

    
    /**
     * @param request
     * @param response
     */
    private void setLove(HttpServletRequest request, HttpServletResponse response) {
        String love = request.getParameter("love");
        String name = request.getParameter("name");
        try {
            boolean f = dao.setLove(love, name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * @param request
     * @param response
     * @throws IOException 
     */
    private void commentList(HttpServletRequest request, HttpServletResponse response) throws IOException {
         List<Comment> news = new ArrayList<Comment>();
         String nid = request.getParameter("nid");
         System.out.println(nid);
            try {
                news = dao.commentList(Integer.valueOf(nid));
            } catch (SQLException e) {
                e.printStackTrace();
            }

            Gson gson = new Gson();
            String s = gson.toJson(news);
            response.getWriter().write(s);
        
    }


    private void addComment(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        String name = request.getParameter("name");
        String content = request.getParameter("content");
        String nid = request.getParameter("nid");
        Comment comment = new Comment();
        comment.setContent(content);
        comment.setName(name);
        comment.setNid(Integer.valueOf(nid));
        System.out.println(comment.toString());
        try {
            boolean f = dao.addComment(comment);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    private void addUser(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        try {
            boolean f = dao.addUser(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    private void search(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String word = request.getParameter("word");
        List<News> news = new ArrayList<News>();
        try {
            news = dao.search(word);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Gson gson = new Gson();
        String s = gson.toJson(news);
        response.getWriter().write(s);
    }

    private void allnews(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<News> news = new ArrayList<News>();
        try {
            news = dao.newsList();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        Gson gson = new Gson();
        String s = gson.toJson(news);
        response.getWriter().write(s);
    }

}
View Code

2.web端-dao

public class NewsDao {

    public List<Comment> commentList(int nid) throws SQLException {
        QueryRunner qr = new QueryRunner(DBUtils.getDataSource());
        String sql = "select * from comment where nid= ?";
        List<Comment> query = qr.query(sql, new BeanListHandler<Comment>(Comment.class),nid);
        return query;
    }
    
    public boolean addComment(Comment comment) throws SQLException {
        QueryRunner qr = new QueryRunner(DBUtils.getDataSource());
        String sql = "insert into comment (nid,name,content) values(?,?,?)";
        int update = qr.update(sql,comment.getNid(),comment.getName(),comment.getContent());
        if (update > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean addUser(String username) throws SQLException {
        QueryRunner qr = new QueryRunner(DBUtils.getDataSource());
        String sql = "insert into user (username) values(?)";
        int update = qr.update(sql,username);
        if (update > 0) {
            return true;
        } else {
            return false;
        }
    }
    
    public boolean deleteBad() throws SQLException {
        QueryRunner qr =new QueryRunner(DBUtils.getDataSource());
        String sql="delete from newslist  where zw = null or zw=?";
        int n = qr.update(sql,"");
        if (n > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean deleteAll() throws SQLException {
        QueryRunner qr =new QueryRunner(DBUtils.getDataSource());
        String sql="delete from newslist  ";
        int n = qr.update(sql);
        if (n > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     *
     * @param zw
     * @param id
     * @return
     * @throws SQLException
     */
    public boolean zw(String zw,int id) throws SQLException {

        QueryRunner qr = new QueryRunner(DBUtils.getDataSource());
        String sql = "update newslist set zw = ?   where id=? ";
        int n = qr.update(sql, zw,id);
        if (n > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     *
     * @return List<News>
     * @throws SQLException
     */
    public List<News> newsList() throws SQLException {
        QueryRunner qr = new QueryRunner(DBUtils.getDataSource());
        String sql = "select * from newslist where url != null  or url != '17KK0006|2145432'or url != ?";
        List<News> query = qr.query(sql, new BeanListHandler<News>(News.class),"");
        return query;
    }
    
    

    /**
     *
     * @param world
     * @return
     * @throws SQLException
     */
    public List<News> search(String world) throws SQLException {
        QueryRunner qr = new QueryRunner(DBUtils.getDataSource());
        String sql = "select * from newslist where  title like '%"+world+"%' limit 0 , 5";
        System.out.println(sql);
        List<News> query = qr.query(sql, new BeanListHandler<News>(News.class));
        return query;
    }

    /**
     *
     * @param news
     * @return
     * @throws SQLException
     */
    public boolean add(News news) throws SQLException {
        QueryRunner qr = new QueryRunner(DBUtils.getDataSource());
        String sql = "insert into newslist (source,title,priority,url,commentCount,digest,imgsrc,ptime,type) " +
                "values(?,?,?,?,?,?,?,?,?)";
        int update = qr.update(sql,news.getSource(),news.getTitle(),news.getPriority(),news.getUrl(),news.getCommentCount(),
                news.getDigest(),news.getImgsrc(),news.getPtime(),news.getType());
        if (update > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param love
     * @return
     * @throws SQLException 
     */
    public boolean setLove(String love ,String name) throws SQLException {

         QueryRunner qr = new QueryRunner(DBUtils.getDataSource());
            String sql = "update user set love = ?   where username=? ";
            int n = qr.update(sql, love,name);
            if (n > 0) {
                return true;
            } else {
                return false;
            }
    }

}
View Code

 

posted @ 2020-04-21 19:42  嘉禾旧木  阅读(131)  评论(0编辑  收藏  举报