package com.hanqi.dao; import java.sql.*; import com.hanqi.common.*; import java.util.*; //关于User表的数据库操作类 public class UserDal { //增 public int insert(User user) throws Exception { int rtn=-1; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if(conn != null) { try { String sql = "insert into T_User (user_id,user_name,password) values (?,?,?)"; pst = conn.prepareStatement(sql); pst.setString(1, user.getUserID()); pst.setString(2, user.getUserName()); pst.setString(3, user.getPassword()); rtn=pst.executeUpdate(); } catch(Exception ex) { throw ex; } finally { try { pst.close(); } catch(Exception ex) { } conn.close(); } } return rtn; } //删 public int delete(String user_id) throws Exception { int rtn=-1; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if(conn != null) { try { String sql = "delete T_User where user_id = ?"; pst = conn.prepareStatement(sql); pst.setString(1, user_id); rtn=pst.executeUpdate(); } catch(Exception ex) { throw ex; } finally { try { pst.close(); } catch(Exception ex) { } conn.close(); } } return rtn; } //改 public int update(User user) throws Exception { int rtn=-1; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if(conn != null) { try { String sql = "update T_User set user_name=?,password=? where user_id=?"; pst = conn.prepareStatement(sql); pst.setString(1, user.getUserName()); pst.setString(2, user.getPassword()); pst.setString(3, user.getUserID()); rtn=pst.executeUpdate(); } catch(Exception ex) { throw ex; } finally { try { pst.close(); } catch(Exception ex) { } conn.close(); } } return rtn; } //查 public ArrayList<User> getListAll() throws Exception { ArrayList<User> rtn = new ArrayList<User>(); Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if(conn != null) { try { String sql = "select * from t_user"; pst = conn.prepareStatement(sql); ResultSet rs = pst.executeQuery(); if (rs != null) { while(rs.next()) { User u = new User(); u.setUserID(rs.getString("user_id")); u.setUserName(rs.getString("user_name")); u.setPassword(rs.getString("password")); rtn.add(u); } } } catch(Exception ex) { throw ex; } finally { try { pst.close(); } catch(Exception ex) { } conn.close(); } } return rtn; } }
package com.hanqi; 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 java.sql.*; import com.hanqi.dao.*; /** * Servlet implementation class saveUser */ @WebServlet("/SaveUser") public class SaveUser extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SaveUser() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String userid = request.getParameter("userid"); String username = request.getParameter("username"); String pw = request.getParameter("password"); if(userid == null ||userid.trim().length() == 0) { response.getWriter().append("用户代码不能为空"); } else if(username == null ||username.trim().length() == 0) { response.getWriter().append("用户名称不能为空"); } else { username = new String(username.getBytes("ISO-8859-1"),"UTF-8"); User user =new User(); user.setUserID(userid); user.setUserName(username); user.setPassword(pw); //调用模型层 UserDal ud = new UserDal(); try { ud.insert(user); response.sendRedirect("FindUserList"); } catch(Exception e) { response.getWriter().append("保存数据失败"); e.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.hanqi; 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.hanqi.common.DBHelper; import java.sql.*; import java.util.*; import com.hanqi.dao.*; /** * Servlet implementation class DeleteUser */ @WebServlet("/DeleteUser") public class DeleteUser extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DeleteUser() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String userid = request.getParameter("userid"); if (userid != null && userid.trim().length()>0) { UserDal ud = new UserDal(); try { ud.delete(userid); response.sendRedirect("FindUserList"); } catch (Exception e) { response.getWriter().append("删除数据失败"); e.printStackTrace(); } } else { response.getWriter().append("userid不能为空"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.hanqi; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; 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.hanqi.common.DBHelper; import com.hanqi.dao.*; /** * Servlet implementation class EditUser */ @WebServlet("/EditUser") public class EditUser extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public EditUser() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String userid = request.getParameter("userid"); String username = request.getParameter("username"); String pw = request.getParameter("password"); if(userid == null ||userid.trim().length() == 0) { response.getWriter().append("用户ID不能为空"); } else if(username == null ||username.trim().length() == 0) { response.getWriter().append("用户名称不能为空"); } else { User user = new User(); user.setUserID(userid); user.setUserName(username); user.setPassword(pw); UserDal ud = new UserDal(); try { ud.update(user); response.sendRedirect("FindUserList"); } catch (Exception e) { response.getWriter().append("修改数据失败"); e.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.hanqi; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; 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.hanqi.common.DBHelper; import com.hanqi.dao.*; /** * Servlet implementation class EditUser */ @WebServlet("/EditUser") public class EditUser extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public EditUser() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String userid = request.getParameter("userid"); String username = request.getParameter("username"); String pw = request.getParameter("password"); if(userid == null ||userid.trim().length() == 0) { response.getWriter().append("用户ID不能为空"); } else if(username == null ||username.trim().length() == 0) { response.getWriter().append("用户名称不能为空"); } else { User user = new User(); user.setUserID(userid); user.setUserName(username); user.setPassword(pw); UserDal ud = new UserDal(); try { ud.update(user); response.sendRedirect("FindUserList"); } catch (Exception e) { response.getWriter().append("修改数据失败"); e.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.hanqi; 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.hanqi.dao.User; import java.sql.*; import java.util.*; import com.hanqi.dao.*; /** * Servlet implementation class FindUserList */ @WebServlet("/FindUserList") public class FindUserList extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FindUserList() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); UserDal ud = new UserDal(); try { ArrayList<User> al = ud.getListAll(); request.setAttribute("userlist",al); } catch(Exception e) { response.getWriter().append("查找数据失败"); e.printStackTrace(); } request.getRequestDispatcher("ShowUserLiset.jsp").forward(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }