zoukankan      html  css  js  c++  java
  • 1月5日 家庭小账本开发记录

    1、实现添加收支记录功能

    (1)Addservlet

    package com.edu.servlet;

    import java.io.IOException;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.edu.dao.Userdao;
    import com.edu.entity.User;

    @WebServlet("/Addservlet")
    public class Addservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    /*String ids = request.getParameter("id");
    int id = Integer.parseInt(ids);*/
    String username = request.getParameter("username");
    String userused = request.getParameter("userused");
    String usermoney = request.getParameter("usermoney");
    String usedate = request.getParameter("usedate");
    Date date = null;
    try {
    date = new SimpleDateFormat("yyyy-MM-dd").parse(usedate);
    } catch (ParseException e) {
    // TODO 自动生成的 catc
    e.printStackTrace();
    }
    User user = new User();
    user.setUsername(username);
    user.setUserused(userused);
    user.setUsermoney(usermoney);
    user.setUsedate(date);
    Userdao dao = new Userdao();
    if(dao.Addused(user))
    {
    System.out.println("添加成功!!");
    }else
    {
    System.out.println("添加失败!!");
    }
    request.getRequestDispatcher("/Showall.jsp").forward(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
    }

    }

    (2)dao

    public boolean Addused(User user) {
    boolean flag = false;
    int count = 0;
    String sql = "insert into userno(id,username,userused,usermoney,usedate)values(?,?,?,?,?)";
    Connection conn = DbHelper.getConnection();
    try {
    PreparedStatement pst = conn.prepareStatement(sql);
    pst.setInt(1, user.getId());
    pst.setString(2, user.getUsername());
    pst.setString(3, user.getUserused());
    pst.setString(4, user.getUsermoney());
    pst.setObject(5, user.getUsedate());
    count = pst.executeUpdate();
    pst.close();
    } catch (SQLException e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    } finally {
    if(count>0)
    flag = true;
    }

    return flag;
    }

    2、实现删除收支记录功能

    (1)Deleteservlet

    package com.edu.servlet;

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.edu.dao.Userdao;

    @WebServlet("/Deleteservlet")
    public class Deleteservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    String ids = request.getParameter("id");
    int id = Integer.parseInt(ids);
    Userdao dao = new Userdao();
    if(dao.Delused(id)) {
    System.out.println("删除成功!!!");
    }else {
    System.out.println("删除失败!!!");
    }
    request.getRequestDispatcher("/Showall.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }

    }

    (2)dao

    public boolean Delused(int id) {
    Connection conn = DbHelper.getConnection();
    // TODO 自动生成的方法存根
    boolean flag = false;
    int count =0;
    String sql = "delete from userno where id = ?";
    PreparedStatement pst = null;
    try {
    pst = conn.prepareStatement(sql);
    pst.setInt(1, id);
    count = pst.executeUpdate();
    pst.close();
    } catch (SQLException e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }finally {
    if(count>0)
    flag = true;
    }

    return flag;
    }

    3、观看JSP+Servlet+MySql+Tomcat+JDBC项目详细讲解

    4、阅读构建之法 现代软件工程(第三版)

  • 相关阅读:
    .net对象生命周期 一) 转载
    HTTP的版本 转载
    sql server sql语句判断是否有表备注并进行新增或修改
    sql server update触发器
    vs文件属性复制到输出目录 转载
    c#使用log4net记录日志 转载
    windows服务定时器 转载
    vs创建项目以后修改https为http
    消息队列 转载
    nmp 设置淘宝镜像
  • 原文地址:https://www.cnblogs.com/lx06/p/14276121.html
Copyright © 2011-2022 走看看