1. package com.hanqi;
  2.  
  3. import java.io.IOException;
  4. import java.sql.*;
  5. import java.text.SimpleDateFormat;
  6.  
  7. import javax.servlet.ServletException;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11.  
  12. import org.omg.PortableInterceptor.RequestInfoOperations;
  13.  
  14. import com.hanqi.*;
  15. import com.hanqi.dao.User;
  16. import com.hanqi.dao.UserDal;
  17.  
  18. import java.util.*;
  19. import java.util.Date;
  20. /**
  21. * Servlet implementation class SaveUser
  22. */
  23. public class SaveUser extends HttpServlet {
  24. private static final long serialVersionUID = 1L;
  25. private User user;
  26.  
  27. /**
  28. * @see HttpServlet#HttpServlet()
  29. */
  30. public SaveUser() {
  31. super();
  32. // TODO Auto-generated constructor stub
  33. }
  34.  
  35. /**
  36. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  37. */
  38. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  39.  
  40. //转码,固定写法
  41. request.setCharacterEncoding("UTF-8");
  42. response.setContentType("text/html;charset=UTF-8");
  43. response.setCharacterEncoding("UTF-8");
  44.  
  45. Date dt = new Date();//获取当前时间
  46. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化时间
  47. String sj = sdf.format(dt);//将时间转换为字符串格式
  48.  
  49. //获取内容
  50. String userID = request.getParameter("userid");
  51. String userName = request.getParameter("username");
  52. String pw = request.getParameter("password");
  53. //判断输入的内容
  54. if(userID == null || userID.trim().length() == 0)
  55. {
  56. //输出内容
  57. response.getWriter().append("用户代码不能为空");
  58. }
  59. else if(userName == null || userName.trim().length() == 0)
  60. {
  61. response.getWriter().append("用户姓名不能为空");
  62. }
  63. else
  64. {
  65.  
  66. User user = new User();
  67.  
  68. user.setUserid(userID);
  69. user.setUsername(userName);
  70. user.setPassword(pw);
  71. user.setuserTime(sj);
  72.  
  73. //调用模型层
  74.  
  75. UserDal ud = new UserDal();
  76.  
  77. try {
  78.  
  79. ud.insert(user);
  80.  
  81. response.sendRedirect("FindUserList");
  82.  
  83. } catch (Exception e) {
  84.  
  85. response.getWriter().append("保存数据失败");
  86.  
  87. e.printStackTrace();
  88. }
  89. }
  90. }
  91.  
  92. /**
  93. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  94. */
  95. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  96. // TODO Auto-generated method stub
  97. doGet(request, response);
  98. }
  99.  
  100. }

增加

  1. package com.hanqi;
  2.  
  3. import java.io.IOException;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8.  
  9. import com.hanqi.common.DBHelper;
  10.  
  11. import java.sql.*;
  12. import java.util.Date;
  13. import com.hanqi.dao.*;
  14.  
  15. /**
  16. * Servlet implementation class DeleteUser
  17. */
  18. public class DeleteUser extends HttpServlet {
  19. private static final long serialVersionUID = 1L;
  20.  
  21. /**
  22. * @see HttpServlet#HttpServlet()
  23. */
  24. public DeleteUser() {
  25. super();
  26. // TODO Auto-generated constructor stub
  27. }
  28.  
  29. /**
  30. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  31. */
  32. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  33.  
  34. //转码
  35. request.setCharacterEncoding("UTF-8");
  36. response.setContentType("text/html;charset=UTF-8");
  37. response.setCharacterEncoding("UTF-8");
  38.  
  39. String userid = request.getParameter("userid");
  40. if(userid != null && userid.trim().length() > 0)
  41. {
  42. UserDal ud = new UserDal();
  43.  
  44. try {
  45.  
  46. ud.delete(userid);
  47.  
  48. response.sendRedirect("FindUserList");
  49.  
  50. } catch (Exception e) {
  51.  
  52. response.getWriter().append("删除数据失败");
  53.  
  54. e.printStackTrace();
  55. }
  56. }
  57. }
  58.  
  59. /**
  60. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  61. */
  62. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  63. // TODO Auto-generated method stub
  64. doGet(request, response);
  65. }
  66.  
  67. }

删除

  1. package com.hanqi;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7.  
  8. import javax.servlet.ServletException;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12.  
  13. import com.hanqi.common.DBHelper;
  14. import com.hanqi.dao.User;
  15. import com.hanqi.dao.UserDal;
  16.  
  17. /**
  18. * Servlet implementation class EditUser
  19. */
  20. public class EditUser extends HttpServlet {
  21. private static final long serialVersionUID = 1L;
  22.  
  23. /**
  24. * @see HttpServlet#HttpServlet()
  25. */
  26. public EditUser() {
  27. super();
  28. // TODO Auto-generated constructor stub
  29. }
  30.  
  31. /**
  32. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  33. */
  34. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  35.  
  36. request.setCharacterEncoding("UTF-8");
  37. response.setContentType("text/html;charset=UTF-8");
  38. response.setCharacterEncoding("UTF-8");
  39.  
  40. //接受相关参数
  41. String userID = request.getParameter("userid");
  42. String userName = request.getParameter("username");
  43. String pw = request.getParameter("password");
  44. //判断输入的内容
  45. if(userID == null || userID.trim().length() == 0)
  46. {
  47. //输出内容
  48. response.getWriter().append("用户代码不能为空");
  49. }
  50. else if(userName == null || userName.trim().length() == 0)
  51. {
  52. response.getWriter().append("用户姓名不能为空");
  53. }
  54. else
  55. {
  56. User user = new User();
  57.  
  58. user.setUserid(userID);
  59. user.setUsername(userName);
  60. user.setPassword(pw);
  61.  
  62. UserDal ud = new UserDal();
  63.  
  64. try
  65. {
  66.  
  67. ud.update(user);
  68.  
  69. response.sendRedirect("FindUserList");
  70.  
  71. }
  72. catch(Exception e)
  73. {
  74.  
  75. response.getWriter().append("修改数据失败");
  76.  
  77. e.printStackTrace();
  78. }
  79. }
  80.  
  81. }
  82.  
  83. /**
  84. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  85. */
  86. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  87. // TODO Auto-generated method stub
  88. doGet(request, response);
  89. }
  90.  
  91. }

修改

  1. package com.hanqi;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9. import java.util.*;
  10.  
  11. import javax.servlet.*;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15.  
  16. import com.hanqi.dao.User;
  17. import com.hanqi.dao.UserDal;
  18.  
  19. /**
  20. * Servlet implementation class FindUserList
  21. */
  22. public class FindUserList extends HttpServlet {
  23. private static final long serialVersionUID = 1L;
  24.  
  25. /**
  26. * @see HttpServlet#HttpServlet()
  27. */
  28. public FindUserList() {
  29. super();
  30. // TODO Auto-generated constructor stub
  31. }
  32.  
  33. /**
  34. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  35. */
  36. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  37.  
  38. //
  39. request.setCharacterEncoding("UTF-8");
  40. response.setContentType("text/html;charset=UTF-8");
  41. response.setCharacterEncoding("UTF-8");
  42. UserDal ud = new UserDal();
  43. try
  44. {
  45. ArrayList<User> al = ud.getListAll();
  46. request.setAttribute("userlist", al);
  47. }
  48. catch(Exception e)
  49. {
  50. response.getWriter().append("查找数据失败");
  51. e.printStackTrace();
  52. }
  53.  
  54. //页面跳转
  55. request.getRequestDispatcher("ShowUserList.jsp").forward(request,response);
  56.  
  57. }
  58.  
  59. /**
  60. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  61. */
  62. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  63. // TODO Auto-generated method stub
  64. doGet(request, response);
  65. }
  66.  
  67. }

查找

  1. package com.hanqi.dao;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5.  
  6. import com.hanqi.common.*;
  7.  
  8. //关于User表的数据库操作类
  9. public class UserDal
  10. {
  11.  
  12. //增
  13. public int insert(User user) throws Exception
  14. {
  15. int rtn = -1;
  16.  
  17. Connection conn = DBHelper.getConnection();
  18.  
  19. PreparedStatement pst = null;
  20.  
  21. if (conn != null)
  22. {
  23. try{
  24. String sql = "insert into T_User (user_id, user_name, password,user_time) values (?,?,?,?) ";
  25.  
  26. pst = conn.prepareStatement(sql);
  27.  
  28. pst.setString(1, user.getUserid());
  29. pst.setString(2, user.getUsername());
  30. pst.setString(3, user.getPassword());
  31. pst.setString(4, user.getuserTime());
  32. rtn = pst.executeUpdate();
  33. }
  34. catch(Exception ex)
  35. {
  36. throw ex;
  37. }
  38. finally
  39. {
  40. try
  41. {
  42. pst.close();
  43. }
  44. catch(Exception e)
  45. {
  46. conn.close();
  47. }
  48. }
  49. }
  50.  
  51. return rtn;
  52. }
  53.  
  54. //删
  55.  
  56. public int delete(String user_id) throws Exception
  57. {
  58. int rtn = -1;
  59.  
  60. Connection conn = DBHelper.getConnection();
  61.  
  62. PreparedStatement pst = null;
  63.  
  64. if (conn != null)
  65. {
  66. try{
  67. String sql = "delete T_User where user_id = ?";
  68.  
  69. pst = conn.prepareStatement(sql);
  70.  
  71. pst.setString(1, user_id);
  72.  
  73. rtn = pst.executeUpdate();
  74. }
  75. catch(Exception ex)
  76. {
  77. throw ex;
  78. }
  79. finally
  80. {
  81. try
  82. {
  83. pst.close();
  84. }
  85. catch(Exception e)
  86. {
  87. conn.close();
  88. }
  89. }
  90. }
  91.  
  92. return rtn;
  93. }
  94.  
  95. //改
  96. public int update(User user) throws Exception
  97. {
  98. int rtn = -1;
  99.  
  100. Connection conn = DBHelper.getConnection();
  101.  
  102. PreparedStatement pst = null;
  103.  
  104. if (conn != null)
  105. {
  106. try
  107. {
  108. String sql = "update T_User set user_name = ?,password = ? "
  109. + "where user_id = ? ";
  110.  
  111. pst = conn.prepareStatement(sql);
  112.  
  113. pst.setString(3, user.getUserid());
  114. pst.setString(1, user.getUsername());
  115. pst.setString(2, user.getPassword());
  116.  
  117. rtn = pst.executeUpdate();
  118. }
  119. catch(Exception ex)
  120. {
  121. throw ex;
  122. }
  123. finally
  124. {
  125. try
  126. {
  127. pst.close();
  128. }
  129. catch(Exception e)
  130. {
  131. conn.close();
  132. }
  133. }
  134. }
  135.  
  136. return rtn;
  137. }
  138.  
  139. //查
  140. public ArrayList<User> getListAll() throws Exception
  141. {
  142. ArrayList<User> rtn = new ArrayList<User>();
  143.  
  144. Connection conn = DBHelper.getConnection();
  145.  
  146. PreparedStatement pst = null;
  147.  
  148. if (conn != null)
  149. {
  150. try
  151. {
  152.  
  153. String sql = "select * from t_user";
  154.  
  155. pst = conn.prepareStatement(sql);
  156.  
  157. ResultSet rs = pst.executeQuery();
  158. if(rs != null)
  159. {
  160. while(rs.next())
  161. {
  162. User u = new User();
  163.  
  164. u.setUserid(rs.getString("user_id"));
  165. u.setUsername(rs.getString("user_name"));
  166. u.setPassword(rs.getString("password"));
  167. u.setuserTime(rs.getString("user_time"));
  168. rtn.add(u); //取一个数就放一个数
  169. }
  170. }
  171. }
  172. catch(Exception ex)
  173. {
  174. throw ex;
  175. }
  176. finally
  177. {
  178. try
  179. {
  180. pst.close();
  181. }
  182. catch(Exception e)
  183. {
  184. conn.close();
  185. }
  186. }
  187. }
  188. return rtn;
  189. }
  190. }

M

20151210--MVC的更多相关文章

  1. Asp.Net Mvc 使用WebUploader 多图片上传

    来博客园有一个月了,哈哈.在这里学到了很多东西.今天也来试着分享一下学到的东西.希望能和大家做朋友共同进步. 最近由于项目需要上传多张图片,对于我这只菜鸟来说,以前上传图片都是直接拖得控件啊,而且还是 ...

  2. .Net Core MVC 网站开发(Ninesky) 2.4、添加栏目与异步方法

    在2.3中完成依赖注入后,这次主要实现栏目的添加功能.按照前面思路栏目有三种类型,常规栏目即可以添加子栏目也可以选择是否添加内容,内容又可以分文章或其他类型,所以还要添加一个模块功能.这次主要实现栏目 ...

  3. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库

    在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...

  4. ASP.NET Core MVC/WebAPi 模型绑定探索

    前言 相信一直关注我的园友都知道,我写的博文都没有特别枯燥理论性的东西,主要是当每开启一门新的技术之旅时,刚开始就直接去看底层实现原理,第一会感觉索然无味,第二也不明白到底为何要这样做,所以只有当你用 ...

  5. ASP.NET Core 中文文档 第四章 MVC(3.8)视图中的依赖注入

    原文:Dependency injection into views 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:孟帅洋(书缘) ASP.NET Core 支持在视图中使用 依赖 ...

  6. 开源:Taurus.MVC 框架

    为什么要创造Taurus.MVC: 记得被上一家公司忽悠去负责公司电商平台的时候,情况是这样的: 项目原版是外包给第三方的,使用:WebForm+NHibernate,代码不堪入目,Bug无限,经常点 ...

  7. Taurus.MVC 2.2 开源发布:WebAPI 功能增强(请求跨域及Json转换)

    背景: 1:有用户反馈了关于跨域请求的问题. 2:有用户反馈了参数获取的问题. 3:JsonHelper的增强. 在综合上面的条件下,有了2.2版本的更新,也因此写了此文. 开源地址: https:/ ...

  8. Taurus.MVC 2.0 开源发布:WebAPI开发教程

    背景: 有用户反映,Tausus.MVC 能写WebAPI么? 能! 教程呢? 嗯,木有! 好吧,刚好2.0出来,就带上WEBAPI教程了! 开源地址: https://github.com/cyq1 ...

  9. 使用Visual Studio 2015 开发ASP.NET MVC 5 项目部署到Mono/Jexus

    最新的Mono 4.4已经支持运行asp.net mvc5项目,有的同学听了这句话就兴高采烈的拿起Visual Studio 2015创建了一个mvc 5的项目,然后部署到Mono上,浏览下发现一堆错 ...

  10. .NetCore MVC中的路由(2)在路由中使用约束

    p { margin-bottom: 0.25cm; direction: ltr; color: #000000; line-height: 120%; orphans: 2; widows: 2 ...

随机推荐

  1. Android使用Google推荐的联网框架Volley,让连接网络更加简单

    大家好.随着技术的进步.科技的发达,非常少有应用是单机的了,大部分都须要联网訪问server,曾经我们都用 httpclient和httpurlconnection,感觉是不是非常麻烦,而Google ...

  2. C# DropDownList绑定文件夹

    首先创建一个类,类名称为FileControl, /// <summary> /// 获取制定文件夹下面的文件夹 /// </summary> /// <param na ...

  3. 理解js异步的概念

    js引擎在执行的时候是单线程的,这是大家都知道的.我们先来看一段代码: <html> <head> <meta http-equiv="Content-Type ...

  4. UVA 1610 Party Games

    题意: 给出一系列字符串,构造出一个字符串大于等于其中的一半,小于另一半. 分析: 取大小为中间的两个a,b(a<b).实际上就是找出第一个小于b的同时大于等于a的字符串,直接构造即可. 代码: ...

  5. SQL递归查询(with cte as) 物料分解

    需求 最近在做一个MRP的项目,需要根据生产下达的计划从原始无聊表中分解出成品所需要的原材料和数量. 参考 http://www.cnblogs.com/xqhppt/archive/2011/02/ ...

  6. DW 做一个table表 对单元格进行合并

    编辑前的代码 <body> <table width="500" border="0" bgcolor='#000000' backgroun ...

  7. EC读书笔记系列之18:条款47、48

    条款47 请使用traits classes表现类型信息 记住: ★Traits classes使得“类型相关信息”在编译期可用.它们以templates和“templates特化”完成实现 ★整合重 ...

  8. switch 与 whille相互套用

    一直自以为还比较了解C++,这两天写个小工具结果出现了个bug,查了几个小时.现在才发现我这么水. switch是C++后来推出了,目的在于提高代码结构清晰度. 但是switch与while连用时是有 ...

  9. linux中断--进程上下文和中断上下文

    一.前言 中断发生以后,CPU跳到内核设置好的中断处理代码中去,由这部分内核代码来处理中断.这个处理过程中的上下文就是中断上下文. 为什么可能导致睡眠的函数都不能在中断上下文中使用呢? 首先睡眠的含义 ...

  10. js数组(一)

    一.创建数组两种方法: 1Array构造 var colors = new Array();2.字面量表示什么事字面量,如 var colors = ["red","gr ...