package com.hanqi; import java.io.IOException; import java.sql.*; import java.text.SimpleDateFormat; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.omg.PortableInterceptor.RequestInfoOperations; import com.hanqi.*; import com.hanqi.dao.User; import com.hanqi.dao.UserDal; import java.util.*; import java.util.Date; /** * Servlet implementation class SaveUser */ public class SaveUser extends HttpServlet { private static final long serialVersionUID = 1L; private User user; /** * @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"); Date dt = new Date();//获取当前时间 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化时间 String sj = sdf.format(dt);//将时间转换为字符串格式 //获取内容 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 { User user = new User(); user.setUserid(userID); user.setUsername(userName); user.setPassword(pw); user.setuserTime(sj); //调用模型层 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.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hanqi.common.DBHelper; import java.sql.*; import java.util.Date; import com.hanqi.dao.*; /** * Servlet implementation class 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(); } } } /** * @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.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hanqi.common.DBHelper; import com.hanqi.dao.User; import com.hanqi.dao.UserDal; /** * Servlet implementation class 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("用户代码不能为空"); } 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 java.sql.ResultSet; import java.sql.Statement; import java.util.*; import javax.servlet.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hanqi.dao.User; import com.hanqi.dao.UserDal; /** * Servlet implementation class 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("ShowUserList.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); } }
查找
package com.hanqi.dao; import java.sql.*; import java.util.ArrayList; import com.hanqi.common.*; //关于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,user_time) values (?,?,?,?) "; pst = conn.prepareStatement(sql); pst.setString(1, user.getUserid()); pst.setString(2, user.getUsername()); pst.setString(3, user.getPassword()); pst.setString(4, user.getuserTime()); rtn = pst.executeUpdate(); } catch(Exception ex) { throw ex; } finally { try { pst.close(); } catch(Exception e) { 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 e) { 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(3, user.getUserid()); pst.setString(1, user.getUsername()); pst.setString(2, user.getPassword()); rtn = pst.executeUpdate(); } catch(Exception ex) { throw ex; } finally { try { pst.close(); } catch(Exception e) { 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")); u.setuserTime(rs.getString("user_time")); rtn.add(u); //取一个数就放一个数 } } } catch(Exception ex) { throw ex; } finally { try { pst.close(); } catch(Exception e) { conn.close(); } } } return rtn; } }
M
时间: 2024-10-06 20:47:37