标签:[] out div parameter patch set name obj ring
计划时间 | 计划任务 | 计划是否完成 |
3.22-3.29 | 管理员的增删改查 | 是 |
3.29-4.5 | 编写活动模块 | 是 |
4.5-4.12 | 完善活动的增删改查 |
完善活动的增删改查
整合了仕庄的代码
主要编写了EventthreadServlet类中的doPost()中的增删改查:
package pintuan.action; import java.io.IOException; import java.io.PrintWriter; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import pintuan.bean.User; import pintuan.db.DBConnection; public class UserServlet extends HttpServlet { private DBConnection dbc=new DBConnection(); private static final long serialVersionUID = 1L; public UserServlet() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action=request.getParameter("action"); if(action.equals("query")){ String sql="select * from pt_user"; ResultSet rs=null; List<User> userList=new ArrayList<User>(); try { rs = dbc.doQueryRS(sql,new Object[]{}); while(rs.next()) { User user=new User(); user.setUserName(rs.getString("UserName")); user.setUserID(rs.getString("UserID")); user.setId(rs.getInt("id")); user.setPassword(rs.getString("Password")); user.setEmail(rs.getString("Email")); user.setIsDelete(rs.getInt("isDelete")); user.setIsApplied(rs.getInt("isApplied")); user.setIsProfile(rs.getInt("isProfile")); userList.add(user); } } catch (SQLException e) { e.printStackTrace(); } HttpSession session=request.getSession(); session.setAttribute("userList", userList); request.getRequestDispatcher("user_list.jsp").forward(request, response); }else if(action.equals("del")) { int id=Integer.parseInt(request.getParameter("id")); String sql="delete from pt_user where id=?"; int res=0; try { res = dbc.doUpdate(sql,new Object[]{id}); } catch (SQLException e) { e.printStackTrace(); } if(res>0) { request.getRequestDispatcher("user?action=query").forward(request, response); }else { PrintWriter out=response.getWriter(); out.print("删除失败"); out.close(); } }else if(action.equals("add")) { String UserID=request.getParameter("UserID"); String UserName = request.getParameter("UserName"); String Password = request.getParameter("Password"); String email = request.getParameter("email"); int isApplied = Integer.parseInt(request.getParameter("isApplied")); int isDelete = Integer.parseInt(request.getParameter("isDelete")); int isProfile = Integer.parseInt(request.getParameter("isProfile")); String sql="insert into pt_user(UserID,UserName,Password,email,isApplied,isDelete,isProfile) values(?,?,?,?,?,?,?)"; int res=0; try { res = dbc.doUpdate(sql,new Object[]{UserID,UserName,Password,email,isApplied,isDelete,isProfile}); } catch (SQLException e) { e.printStackTrace(); } if(res>0) { request.getRequestDispatcher("user?action=query").forward(request, response); }else { PrintWriter out=response.getWriter(); out.print("添加失败"); out.close(); } }else if(action.equals("edit")) { int id=Integer.parseInt(request.getParameter("id")); String sql="select * from pt_user where id=?"; User user=null; try { ResultSet rs=dbc.doQueryRS(sql,new Object[]{id}); if(rs.next()) { user = new User(); user.setUserID(rs.getString("UserID")); user.setUserName(rs.getString("UserName")); user.setPassword(rs.getString("Password")); user.setIsApplied(rs.getInt("isApplied")); user.setIsDelete(rs.getInt("isDelete")); user.setIsProfile(rs.getInt("isProfile")); user.setId(rs.getInt("id")); } } catch (SQLException e) { e.printStackTrace(); } HttpSession session=request.getSession(); session.setAttribute("user", user); request.getRequestDispatcher("edit_user.jsp").forward(request, response); }else if(action.equals("update")) { int id=Integer.parseInt(request.getParameter("id")); String UserID=request.getParameter("UserID"); String UserName = request.getParameter("UserName"); String Password = request.getParameter("Password"); String email = request.getParameter("email"); int isApplied = Integer.parseInt(request.getParameter("isApplied")); int isDelete = Integer.parseInt(request.getParameter("isDelete")); int isProfile = Integer.parseInt(request.getParameter("isProfile")); String sql="update pt_user set " + "UserID=?,UserName=?,Password=?,email=?," + "isApplied=?,isDelete=?,isProfile = ? where id=?"; int res=0; try { res = dbc.doUpdate(sql,new Object[]{UserID,UserName,Password,email,isApplied,isDelete,isProfile,id}); } catch (SQLException e) { e.printStackTrace(); } if(res>0) { request.getRequestDispatcher("user?action=query").forward(request, response); }else { PrintWriter out=response.getWriter(); out.print("更新失败"); out.close(); } }else if(action.equals("register")) { String UserID=request.getParameter("UserID"); String UserName = request.getParameter("UserName"); String Password = request.getParameter("Password"); String email = request.getParameter("email"); int isApplied = Integer.parseInt(request.getParameter("isApplied")); int isDelete = Integer.parseInt(request.getParameter("isDelete")); int isProfile = Integer.parseInt(request.getParameter("isProfile")); String sql="insert into pt_user(UserID,UserName,Password,email,isApplied,isDelete,isProfile) values(?,?,?,?,?,?,?)"; int res=0; try { res = dbc.doUpdate(sql,new Object[]{UserID,UserName,Password,email,isApplied,isDelete,isProfile}); } catch (SQLException e) { e.printStackTrace(); } if(res>0) { request.getRequestDispatcher("register_result_test.jsp").forward(request, response); }else { PrintWriter out=response.getWriter(); out.print("添加失败"); out.close(); } } else { } } }
标签:[] out div parameter patch set name obj ring
原文地址:http://www.cnblogs.com/gxt-smart/p/6701899.html