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;
}