IGeekShop案例12-实现注册功能和激活功能

你的名字 2024-04-17 14:13 200阅读 0赞

IGeekShop案例12-实现注册功能和激活功能

1WebContent部分

1 register.jsp代码修改

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head></head>
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <title>会员注册</title>
  8. <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
  9. <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
  10. <script src="js/bootstrap.min.js" type="text/javascript"></script>
  11. <!-- 引入自定义css文件 style.css -->
  12. <link rel="stylesheet" href="css/style.css" type="text/css" />
  13. <style>
  14. body {
  15. margin-top: 20px;
  16. margin: 0 auto;
  17. }
  18. .carousel-inner .item img {
  19. width: 100%;
  20. height: 300px;
  21. }
  22. font {
  23. color: #3164af;
  24. font-size: 18px;
  25. font-weight: normal;
  26. padding: 0 10px;
  27. }
  28. </style>
  29. <script type="text/javascript">
  30. /* 实现验证码点击改变效果 */
  31. function changeImge(obj){
  32. obj.src="${pageContext.request.contextPath }/checkImg?time="+new Date().getTime();
  33. }
  34. </script>
  35. </head>
  36. <body>
  37. <!-- 引入header.jsp -->
  38. <jsp:include page="/header.jsp"></jsp:include>
  39. <div class="container"
  40. style="width: 100%; background: url('image/regist_bg.jpg');">
  41. <div class="row">
  42. <div class="col-md-2"></div>
  43. <div class="col-md-8"
  44. style="background: #fff; padding: 40px 80px; margin: 30px; border: 7px solid #ccc;">
  45. <font>会员注册</font>USER REGISTER
  46. <form class="form-horizontal" action="${pageContext.request.contextPath }/register" method="post" style="margin-top: 5px;">
  47. <div class="form-group">
  48. <label for="username" class="col-sm-2 control-label">用户名</label>
  49. <div class="col-sm-6">
  50. <input type="text" class="form-control" id="username" name="username"
  51. placeholder="请输入用户名">
  52. </div>
  53. </div>
  54. <div class="form-group">
  55. <label for="inputPassword3" class="col-sm-2 control-label">密码</label>
  56. <div class="col-sm-6">
  57. <input type="password" class="form-control" id="inputPassword3" name="password"
  58. placeholder="请输入密码">
  59. </div>
  60. </div>
  61. <div class="form-group">
  62. <label for="confirmpwd" class="col-sm-2 control-label">确认密码</label>
  63. <div class="col-sm-6">
  64. <input type="password" class="form-control" id="confirmpwd"
  65. placeholder="请输入确认密码">
  66. </div>
  67. </div>
  68. <div class="form-group">
  69. <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
  70. <div class="col-sm-6">
  71. <input type="email" class="form-control" id="inputEmail3" name="email"
  72. placeholder="Email">
  73. </div>
  74. </div>
  75. <div class="form-group">
  76. <label for="usercaption" class="col-sm-2 control-label">姓名</label>
  77. <div class="col-sm-6">
  78. <input type="text" class="form-control" id="usercaption" name="name"
  79. placeholder="请输入姓名">
  80. </div>
  81. </div>
  82. <div class="form-group opt">
  83. <label for="inlineRadio1" class="col-sm-2 control-label">性别</label>
  84. <div class="col-sm-6">
  85. <label class="radio-inline"> <input type="radio"
  86. name="sex" id="sex1" value="male">
  87. </label> <label class="radio-inline"> <input type="radio"
  88. name="sex" id="sex2" value="female">
  89. </label>
  90. </div>
  91. </div>
  92. <div class="form-group">
  93. <label for="date" class="col-sm-2 control-label">出生日期</label>
  94. <div class="col-sm-6">
  95. <input type="date" class="form-control" name="birthday">
  96. </div>
  97. </div>
  98. <div class="form-group">
  99. <label for="date" class="col-sm-2 control-label">验证码</label>
  100. <div class="col-sm-3">
  101. <input type="text" class="form-control" name="checkCode">
  102. </div>
  103. <div class="col-sm-2">
  104. <img src="${pageContext.request.contextPath }/checkImg" onclick="changeImge(this)"/>
  105. </div>
  106. </div>
  107. <div class="form-group">
  108. <div class="col-sm-offset-2 col-sm-10">
  109. <input type="submit" width="100" value="注册" name="submit"
  110. style="background: url('./images/register.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0); height: 35px; width: 100px; color: white;">
  111. <div>${registInfo }</div>
  112. </div>
  113. </div>
  114. </form>
  115. </div>
  116. <div class="col-md-2"></div>
  117. </div>
  118. </div>
  119. <!-- 引入footer.jsp -->
  120. <jsp:include page="/footer.jsp"></jsp:include>
  121. </body>
  122. </html>

2 registerFail.jsp代码

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <h2>对不起,注册失败,请联系管理员 010-68888888</h2>
  11. </body>
  12. </html>

3 registerSuccess.jsp代码

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <h2>恭喜您注册成功,请赶快前往注册邮箱激活您的帐户</h2>
  11. <div>
  12. <span><a href="http://mail.163.com/">网易163邮箱</a></span>
  13. <span><a href="http://mail.126.com/">网易126邮箱</a></span>
  14. <span><a href="https://mail.qq.com/cgi-bin/loginpage">腾讯QQ邮箱</a></span>
  15. </div>
  16. </body>
  17. </html>

2 web层

1RegisterServlet代码

注意自动映射封装的时候,如何进行类型的转换。如String 转换成date类型。本例中获取到的birthday需要转成date类型。

  1. package www.test.web.servlet;
  2. import java.io.IOException;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.sql.SQLException;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.util.Map;
  9. import javax.mail.MessagingException;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import org.apache.commons.beanutils.BeanUtils;
  15. import org.apache.commons.beanutils.ConvertUtils;
  16. import org.apache.commons.beanutils.Converter;
  17. import www.test.domain.User;
  18. import www.test.service.UserService;
  19. import www.test.utils.CommonsUtils;
  20. import www.test.utils.MailUtils;
  21. public class RegisterServlet extends HttpServlet {
  22. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  23. // 解决乱码问题
  24. request.setCharacterEncoding("UTF-8");
  25. // 判断验证码是否正确
  26. // 获得页面输入的验证
  27. String checkCode_client = request.getParameter("checkCode");
  28. // 获得生成图片的文字的验证码
  29. String checkCode_session = (String) request.getSession().getAttribute("checkcode_session");
  30. // 比对页面的和生成图片的文字的验证码是否一致
  31. if (!checkCode_session.equals(checkCode_client)) {
  32. request.setAttribute("registInfo", "您的验证码不正确");
  33. request.getRequestDispatcher("/register.jsp").forward(request, response);
  34. return; // 验证码输入错误的话,就没有必要获取输入的用户名和密码等其他信息
  35. }
  36. // 获得表单数据
  37. Map<String, String[]> properties = request.getParameterMap();
  38. User user = new User();
  39. try {
  40. // 自己指定一个类型转换器(将String转成Date)
  41. // ConvertUtils.register(converter, clazz);
  42. // converter : 转换器接口 clazz:要转换成的类型
  43. ConvertUtils.register(new Converter() {
  44. @Override
  45. // value要转换的值
  46. public Object convert(Class clazz, Object value) {
  47. // 将string转成date
  48. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  49. Date date = null;
  50. try {
  51. date = dateFormat.parse(value.toString());
  52. } catch (ParseException e) {
  53. e.printStackTrace();
  54. }
  55. return date;
  56. }
  57. }, Date.class);
  58. // 进行映射封装
  59. BeanUtils.populate(user, properties);
  60. } catch (IllegalAccessException | InvocationTargetException e) {
  61. e.printStackTrace();
  62. }
  63. // 到这里user就封装了
  64. // 下面手动封装剩余的项
  65. // private String uid;
  66. user.setUid(CommonsUtils.getUUID());
  67. // private String telephone;
  68. user.setTelephone(null);
  69. // private int state;//是否激活
  70. user.setState(0);
  71. // private String code;//激活码
  72. String activeCode = CommonsUtils.getUUID();
  73. user.setCode(activeCode);
  74. // 将user传递给service层
  75. UserService service = new UserService();
  76. boolean isRegisterSuccess =true;
  77. try {
  78. isRegisterSuccess = service.regist(user);
  79. } catch (SQLException e) {
  80. e.printStackTrace();
  81. }
  82. //是否注册成功
  83. if(isRegisterSuccess){
  84. //发送激活邮件
  85. String emailMsg = "恭喜您注册成功,请点击下面的连接进行激活账户"
  86. +"<a href='http://localhost:8080/WEBTestShop28/active?activeCode="+activeCode+"'>"
  87. + "http://localhost:8080/WEBTestShop28/active?activeCode="+activeCode+"</a>";
  88. String subject = "激活邮件";
  89. try {
  90. MailUtils.sendMail(user.getEmail(), subject, emailMsg);
  91. } catch (MessagingException e) {
  92. e.printStackTrace();
  93. }
  94. //跳转到注册成功页面
  95. response.sendRedirect(request.getContextPath()+"/registerSuccess.jsp");
  96. }else{
  97. //跳转到失败的提示页面
  98. response.sendRedirect(request.getContextPath()+"/registerFail.jsp");
  99. }
  100. }
  101. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  102. doGet(request, response);
  103. }
  104. }

2 ActiveServlet代码

  1. package www.test.web.servlet;
  2. import java.io.IOException;
  3. import java.sql.SQLException;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import www.test.service.UserService;
  9. public class ActiveServlet extends HttpServlet {
  10. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11. //获得激活码
  12. String activeCode = request.getParameter("activeCode");
  13. //将获取到的激活码传递给service层
  14. UserService service = new UserService();
  15. boolean isActiveSuccess = true;
  16. try {
  17. isActiveSuccess = service.active(activeCode);
  18. } catch (SQLException e) {
  19. e.printStackTrace();
  20. }
  21. if(isActiveSuccess){
  22. //激活成功跳转到登录页面
  23. response.sendRedirect(request.getContextPath()+"/login.jsp");
  24. }else{ //激活失败跳转到注册页面
  25. response.sendRedirect(request.getContextPath()+"/register.jsp");
  26. }
  27. }
  28. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  29. doGet(request, response);
  30. }
  31. }

3 CheckImgServlet代码

  1. package www.test.web.servlet;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.image.BufferedImage;
  7. import java.io.BufferedReader;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Random;
  13. import javax.imageio.ImageIO;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.http.HttpServlet;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18. /**
  19. * 验证码生成程序
  20. */
  21. public class CheckImgServlet extends HttpServlet {
  22. // 集合中保存所有成语
  23. private List<String> words = new ArrayList<String>();
  24. @Override
  25. public void init() throws ServletException {
  26. // 初始化阶段,读取new_words.txt
  27. // web工程中读取文件,必须使用绝对磁盘路径
  28. String path = getServletContext().getRealPath("/WEB-INF/new_words.txt");
  29. try {
  30. BufferedReader reader = new BufferedReader(new FileReader(path));
  31. String line;
  32. while ((line = reader.readLine()) != null) {
  33. words.add(line);
  34. }
  35. reader.close();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. public void doGet(HttpServletRequest request, HttpServletResponse response)
  41. throws ServletException, IOException {
  42. // 禁止缓存
  43. // response.setHeader("Cache-Control", "no-cache");
  44. // response.setHeader("Pragma", "no-cache");
  45. // response.setDateHeader("Expires", -1);
  46. int width = 120;
  47. int height = 30;
  48. // 步骤一 绘制一张内存中图片
  49. BufferedImage bufferedImage = new BufferedImage(width, height,
  50. BufferedImage.TYPE_INT_RGB);
  51. // 步骤二 图片绘制背景颜色 ---通过绘图对象
  52. Graphics graphics = bufferedImage.getGraphics();// 得到画图对象 --- 画笔
  53. // 绘制任何图形之前 都必须指定一个颜色
  54. graphics.setColor(getRandColor(200, 250));
  55. graphics.fillRect(0, 0, width, height);
  56. // 步骤三 绘制边框
  57. graphics.setColor(Color.WHITE);
  58. graphics.drawRect(0, 0, width - 1, height - 1);
  59. // 步骤四 四个随机数字
  60. Graphics2D graphics2d = (Graphics2D) graphics;
  61. // 设置输出字体
  62. graphics2d.setFont(new Font("宋体", Font.BOLD, 18));
  63. Random random = new Random();// 生成随机数
  64. int index = random.nextInt(words.size());
  65. String word = words.get(index);// 获得成语
  66. // 定义x坐标
  67. int x = 10;
  68. for (int i = 0; i < word.length(); i++) {
  69. // 随机颜色
  70. graphics2d.setColor(new Color(20 + random.nextInt(110), 20 + random
  71. .nextInt(110), 20 + random.nextInt(110)));
  72. // 旋转 -30 --- 30度
  73. int jiaodu = random.nextInt(60) - 30;
  74. // 换算弧度
  75. double theta = jiaodu * Math.PI / 180;
  76. // 获得字母数字
  77. char c = word.charAt(i);
  78. // 将c 输出到图片
  79. graphics2d.rotate(theta, x, 20);
  80. graphics2d.drawString(String.valueOf(c), x, 20);
  81. graphics2d.rotate(-theta, x, 20);
  82. x += 30;
  83. }
  84. // 将验证码内容保存session
  85. request.getSession().setAttribute("checkcode_session", word);
  86. // 步骤五 绘制干扰线
  87. graphics.setColor(getRandColor(160, 200));
  88. int x1;
  89. int x2;
  90. int y1;
  91. int y2;
  92. for (int i = 0; i < 30; i++) {
  93. x1 = random.nextInt(width);
  94. x2 = random.nextInt(12);
  95. y1 = random.nextInt(height);
  96. y2 = random.nextInt(12);
  97. graphics.drawLine(x1, y1, x1 + x2, x2 + y2);
  98. }
  99. // 将上面图片输出到浏览器 ImageIO
  100. graphics.dispose();// 释放资源
  101. //将图片写到response.getOutputStream()中
  102. ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
  103. }
  104. public void doPost(HttpServletRequest request, HttpServletResponse response)
  105. throws ServletException, IOException {
  106. doGet(request, response);
  107. }
  108. /**
  109. * 取其某一范围的color
  110. *
  111. * @param fc
  112. * int 范围参数1
  113. * @param bc
  114. * int 范围参数2
  115. * @return Color
  116. */
  117. private Color getRandColor(int fc, int bc) {
  118. // 取其随机颜色
  119. Random random = new Random();
  120. if (fc > 255) {
  121. fc = 255;
  122. }
  123. if (bc > 255) {
  124. bc = 255;
  125. }
  126. int r = fc + random.nextInt(bc - fc);
  127. int g = fc + random.nextInt(bc - fc);
  128. int b = fc + random.nextInt(bc - fc);
  129. return new Color(r, g, b);
  130. }
  131. }

3 service层

  1. package www.test.service;
  2. import java.sql.SQLException;
  3. import www.test.dao.UserDao;
  4. import www.test.domain.User;
  5. public class UserService {
  6. //注冊
  7. public boolean regist(User user) throws SQLException {
  8. UserDao dao = new UserDao();
  9. int row = dao.regist(user);
  10. return row>0?true:false;
  11. }
  12. //激活
  13. public boolean active(String activeCode) throws SQLException {
  14. UserDao dao = new UserDao();
  15. int row = dao.active(activeCode);
  16. return row>0?true:false;
  17. }
  18. }

4 dao层代码

  1. package www.test.dao;
  2. import java.sql.SQLException;
  3. import org.apache.commons.dbutils.QueryRunner;
  4. import www.test.domain.User;
  5. import www.test.utils.C3P0Utils;
  6. public class UserDao {
  7. //注冊
  8. public int regist(User user) throws SQLException {
  9. QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
  10. String sql ="insert into user values (?,?,?,?,?,?,?,?,?,?);";
  11. int row = qr.update(sql, user.getUid(),user.getUsername(),user.getPassword(),
  12. user.getName(),user.getEmail(),user.getTelephone(),
  13. user.getBirthday(),user.getSex(),user.getState(),user.getCode());
  14. return row;
  15. }
  16. //激活
  17. public int active(String activeCode) throws SQLException {
  18. QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
  19. String sql ="update user set state=? where code=?";
  20. int row = qr.update(sql, 1,activeCode);
  21. return row;
  22. }
  23. }

5 utils部分

1 CommonsUtils代码

  1. package www.test.utils;
  2. import java.util.UUID;
  3. public class CommonsUtils {
  4. public static String getUUID(){
  5. return UUID.randomUUID().toString().replaceAll("-", "");
  6. }
  7. }

2 MD5Utils代码

  1. package www.test.utils;
  2. import java.math.BigInteger;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. public class MD5Utils {
  6. /**
  7. * 使用md5的算法进行加密
  8. */
  9. public static String md5(String plainText) {
  10. byte[] secretBytes = null;
  11. try {
  12. secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes());
  13. } catch (NoSuchAlgorithmException e) {
  14. throw new RuntimeException("没有md5这个算法!");
  15. }
  16. String md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字
  17. // 如果生成数字未满32位,需要前面补0
  18. for (int i = 0; i < 32 - md5code.length(); i++) {
  19. md5code = "0" + md5code;
  20. }
  21. return md5code;
  22. }
  23. public static void main(String[] args) {
  24. System.out.println(md5("123"));
  25. }
  26. }

3 MailUtils代码

  1. package www.test.utils;
  2. import java.security.GeneralSecurityException;
  3. import java.util.Properties;
  4. import javax.mail.Authenticator;
  5. import javax.mail.Message;
  6. import javax.mail.MessagingException;
  7. import javax.mail.PasswordAuthentication;
  8. import javax.mail.Session;
  9. import javax.mail.Transport;
  10. import javax.mail.internet.AddressException;
  11. import javax.mail.internet.InternetAddress;
  12. import javax.mail.internet.MimeMessage;
  13. import javax.mail.internet.MimeMessage.RecipientType;
  14. import com.sun.mail.util.MailSSLSocketFactory;
  15. public class MailUtils {
  16. //email:邮件发给谁 subject:主题 emailMsg:邮件的内容
  17. public static void sendMail(String email, String subject, String emailMsg)
  18. throws AddressException, MessagingException {
  19. // 1.创建一个程序与邮件服务器会话对象 Session
  20. Properties props = new Properties();
  21. props.setProperty("mail.transport.protocol", "SMTP");//发邮件的协议
  22. props.setProperty("mail.host", "smtp.126.com");//发送邮件的服务器地址
  23. props.setProperty("mail.smtp.auth", "true");// 指定验证为true
  24. //开启SSL加密 ,QQ邮箱必须开启ssl加密才可以。
  25. MailSSLSocketFactory sf = null;
  26. try {
  27. sf = new MailSSLSocketFactory();
  28. } catch (GeneralSecurityException e) {
  29. e.printStackTrace();
  30. }
  31. sf.setTrustAllHosts(true);
  32. props.put("mail.smtp.ssl.enable", "true");
  33. props.put("mail.smtp.ssl.socketFactory", sf);
  34. // 创建验证器
  35. Authenticator auth = new Authenticator() {
  36. public PasswordAuthentication getPasswordAuthentication() {
  37. return new PasswordAuthentication("luojun281", "123456ljp");//发邮件的账号的验证 12345为授权码,并非密码
  38. }
  39. };
  40. //这个是邮箱服务器的session 和之前学的session不是同一个session
  41. Session session = Session.getInstance(props, auth);
  42. // 2.创建一个Message,它相当于是邮件内容
  43. Message message = new MimeMessage(session);
  44. message.setFrom(new InternetAddress("luojun281@126.com")); // 设置发送者
  45. message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者
  46. message.setSubject(subject);//邮件的主题
  47. message.setContent(emailMsg, "text/html;charset=utf-8");
  48. // 3.创建 Transport用于将邮件发送
  49. Transport.send(message);
  50. }
  51. }

4 c3p0utils代码

  1. package www.test.utils;
  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import javax.sql.DataSource;
  7. import com.mchange.v2.c3p0.ComboPooledDataSource;
  8. public class C3P0Utils {
  9. // 1 获得Connection ----- 从连接池中获取
  10. private static DataSource dataSource = new ComboPooledDataSource();
  11. // 2 创建ThreadLocal 存储的类型是Connection
  12. private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
  13. // 3 直接可以获取一个连接池
  14. public static DataSource getDataSource() {
  15. return dataSource;
  16. }
  17. // 4 直接获取一个连接
  18. public static Connection getConnection() throws SQLException {
  19. return dataSource.getConnection();
  20. }
  21. // 5 获取绑定到ThreadLocal上的连接对象
  22. public static Connection getCurrentConnection() throws SQLException {
  23. //从ThreadLocal寻找 当前线程是否有对应Connection
  24. Connection con = tl.get();
  25. if (con == null) {
  26. //获得新的connection
  27. con = dataSource.getConnection();
  28. //将conn资源绑定到ThreadLocal(map)上
  29. tl.set(con);
  30. }
  31. return con;
  32. }
  33. // 6 开启事务
  34. public static void startTransaction() throws SQLException {
  35. Connection con = getCurrentConnection();
  36. if (con != null) {
  37. con.setAutoCommit(false);
  38. }
  39. }
  40. // 7 事务回滚
  41. public static void rollback() throws SQLException {
  42. Connection con = getCurrentConnection();
  43. if (con != null) {
  44. con.rollback();
  45. }
  46. }
  47. // 8 提交并且 关闭资源及从ThreadLocall中释放
  48. public static void commitAndRelease() throws SQLException {
  49. Connection con = getCurrentConnection();
  50. if (con != null) {
  51. con.commit(); // 事务提交
  52. con.close();// 关闭资源
  53. tl.remove();// 从线程绑定中移除
  54. }
  55. }
  56. // 9 关闭资源方法
  57. public static void close(ResultSet rs, Statement st, Connection con) throws SQLException {
  58. if (rs != null) {
  59. rs.close();
  60. }
  61. if (st != null) {
  62. st.close();
  63. }
  64. if (con != null) {
  65. con.close();
  66. }
  67. }
  68. }

6 domain部分

1 user类

  1. package www.test.domain;
  2. import java.util.Date;
  3. public class User {
  4. /*`uid` varchar(32) NOT NULL,
  5. `username` varchar(20) DEFAULT NULL,
  6. `password` varchar(20) DEFAULT NULL,
  7. `name` varchar(20) DEFAULT NULL,
  8. `email` varchar(30) DEFAULT NULL,
  9. `telephone` varchar(20) DEFAULT NULL,
  10. `birthday` date DEFAULT NULL,
  11. `sex` varchar(10) DEFAULT NULL,
  12. `state` int(11) DEFAULT NULL,
  13. `code` varchar(64) DEFAULT NULL,*/
  14. private String uid;
  15. private String username;
  16. private String password;
  17. private String name;
  18. private String email;
  19. private String telephone;
  20. private Date birthday;
  21. private String sex;
  22. private int state;//是否激活
  23. private String code;//激活码
  24. public String getUid() {
  25. return uid;
  26. }
  27. public void setUid(String uid) {
  28. this.uid = uid;
  29. }
  30. public String getUsername() {
  31. return username;
  32. }
  33. public void setUsername(String username) {
  34. this.username = username;
  35. }
  36. public String getPassword() {
  37. return password;
  38. }
  39. public void setPassword(String password) {
  40. this.password = password;
  41. }
  42. public String getName() {
  43. return name;
  44. }
  45. public void setName(String name) {
  46. this.name = name;
  47. }
  48. public String getEmail() {
  49. return email;
  50. }
  51. public void setEmail(String email) {
  52. this.email = email;
  53. }
  54. public String getTelephone() {
  55. return telephone;
  56. }
  57. public void setTelephone(String telephone) {
  58. this.telephone = telephone;
  59. }
  60. public Date getBirthday() {
  61. return birthday;
  62. }
  63. public void setBirthday(Date birthday) {
  64. this.birthday = birthday;
  65. }
  66. public String getSex() {
  67. return sex;
  68. }
  69. public void setSex(String sex) {
  70. this.sex = sex;
  71. }
  72. public int getState() {
  73. return state;
  74. }
  75. public void setState(int state) {
  76. this.state = state;
  77. }
  78. public String getCode() {
  79. return code;
  80. }
  81. public void setCode(String code) {
  82. this.code = code;
  83. }
  84. }

发表评论

表情:
评论列表 (有 0 条评论,200人围观)

还没有评论,来说两句吧...

相关阅读