1.新建customer表生日都选为当天

所需jar包

2.使用c3p0连接到数据的xml配置文件

3.连接数据库的工具类

  1. package com.cc.birthday;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. import javax.sql.DataSource;
  9.  
  10. import com.mchange.v2.c3p0.ComboPooledDataSource;
  11.  
  12. public class DataSourceUtils {
  13. private static DataSource dataSource=new ComboPooledDataSource();
  14.  
  15. private static ThreadLocal<Connection> t1=new ThreadLocal<Connection>();
  16.  
  17. //直接可以获取一个连接池
  18. public static DataSource getDataSource(){
  19. return dataSource;
  20. }
  21.  
  22. //获取连接对象
  23. public static Connection getConnection() throws SQLException{
  24. Connection con=t1.get();
  25. if(con==null){
  26. con=dataSource.getConnection();
  27. t1.set(con);
  28. }
  29. return con;
  30. }
  31.  
  32. //开启事务
  33. public static void startTrasaction() throws SQLException {
  34. Connection con=getConnection();
  35. if(con!=null){
  36. con.setAutoCommit(false);
  37. }
  38. }
  39.  
  40. //事务回滚
  41. public static void rollback() throws SQLException{
  42. Connection con =getConnection();
  43. if(con!=null){
  44. con.rollback();
  45. }
  46. }
  47.  
  48. //提交并且 关闭资源及从ThreadLocal中释放
  49. public static void commitAndRelease() throws SQLException{
  50. Connection con=getConnection();
  51. if(con!=null){
  52. con.commit();
  53. con.close();
  54. t1.remove();
  55. }
  56. }
  57.  
  58. //关闭资源方法
  59. public static void closeConnection() throws SQLException{
  60. Connection con=getConnection();
  61. if(con!=null){
  62. con.close();
  63. }
  64. }
  65.  
  66. public static void closeStatement(Statement st) throws SQLException {
  67. if(st!=null){
  68. st.close();
  69. }
  70. }
  71.  
  72. public static void closeResultSet(ResultSet rs) throws SQLException{
  73. if(rs!=null){
  74. rs.close();
  75. }
  76. }
  77.  
  78. }

4.发送邮件的工具类

  1. package com.cc.mail;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.mail.Authenticator;
  6. import javax.mail.Message;
  7. import javax.mail.MessagingException;
  8. import javax.mail.PasswordAuthentication;
  9. import javax.mail.Session;
  10. import javax.mail.Transport;
  11. import javax.mail.internet.AddressException;
  12. import javax.mail.internet.InternetAddress;
  13. import javax.mail.internet.MimeMessage;
  14. import javax.mail.internet.MimeMessage.RecipientType;
  15.  
  16. public class MailUtils {
  17.  
  18. //email:邮件发给谁 subject:主题 emailMsg:邮件的内容
  19. public static void sendMail(String email, String subject, String emailMsg)
  20. throws AddressException, MessagingException {
  21.  
  22. // 1.创建一个程序与邮件服务器会话对象 Session
  23. Properties props = new Properties();
  24. props.setProperty("mail.transport.protocol", "SMTP");//发邮件的协议
  25. props.setProperty("mail.host", "smtp.163.com");//发送邮件的服务器地址
  26. props.setProperty("mail.smtp.auth", "true");// 指定验证为true
  27.  
  28. // 创建验证器
  29. Authenticator auth = new Authenticator() {
  30. public PasswordAuthentication getPasswordAuthentication() {
  31. return new PasswordAuthentication("emailusername", "password");//发邮件的账号的验证
  32. }
  33. };
  34.  
  35. Session session = Session.getInstance(props, auth);
  36.  
  37. // 2.创建一个Message,它相当于是邮件内容
  38. Message message = new MimeMessage(session);
  39.  
  40. message.setFrom(new InternetAddress("xxxxxx@163.com")); // 设置发送者
  41.  
  42. message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者
  43.  
  44. message.setSubject(subject);//邮件的主题
  45.  
  46. message.setContent(emailMsg, "text/html;charset=utf-8");
  47.  
  48. // 3.创建 Transport用于将邮件发送
  49. Transport.send(message);
  50. }
  51. }

5.customer实体类

  1. package com.cc.birthday;
  2.  
  3. public class Customer {
  4. private int id;
  5. private String username;
  6. private String password;
  7. private String realname;
  8. private String birthday;
  9. private String email;
  10. public int getId() {
  11. return id;
  12. }
  13. public void setId(int id) {
  14. this.id = id;
  15. }
  16. public String getUsername() {
  17. return username;
  18. }
  19. public void setUsername(String username) {
  20. this.username = username;
  21. }
  22. public String getPassword() {
  23. return password;
  24. }
  25. public void setPassword(String password) {
  26. this.password = password;
  27. }
  28. public String getRealname() {
  29. return realname;
  30. }
  31. public void setRealname(String realname) {
  32. this.realname = realname;
  33. }
  34. public String getBirthday() {
  35. return birthday;
  36. }
  37. public void setBirthday(String birthday) {
  38. this.birthday = birthday;
  39. }
  40. public String getEmail() {
  41. return email;
  42. }
  43. public void setEmail(String email) {
  44. this.email = email;
  45. }
  46.  
  47. }

6.根据数据库查询结果使用调度器定时发送祝福邮件

  1. package com.cc.birthday;
  2.  
  3. import java.sql.SQLException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.List;
  7. import java.util.Timer;
  8. import java.util.TimerTask;
  9.  
  10. import javax.mail.MessagingException;
  11. import javax.servlet.ServletContextEvent;
  12. import javax.servlet.ServletContextListener;
  13.  
  14. import org.apache.commons.dbutils.QueryRunner;
  15. import org.apache.commons.dbutils.handlers.BeanListHandler;
  16.  
  17. import com.cc.birthday.Customer;
  18. import com.cc.mail.MailUtils;
  19.  
  20. public class BirthdayListener implements ServletContextListener{
  21.  
  22. @Override
  23. public void contextInitialized(ServletContextEvent sce) {
  24. // 当web应用启动开启任务调动---功能在用户的生日当天发送邮件
  25. //开启一个定时器
  26. Timer timer=new Timer();
  27. timer.scheduleAtFixedRate(new TimerTask() {
  28.  
  29. @Override
  30. public void run() {
  31. // 为当前的生日的用户发邮件
  32. //1.获得今天过生日的人
  33. //获得今天的日期
  34. SimpleDateFormat format=new SimpleDateFormat("MM-dd");
  35. String currentDate=format.format(new Date());
  36. //根据当前时间从数据库查询今天过生日的人
  37. QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
  38. String sql="select * from customer where birthday like ?";
  39. List<Customer> customerList=null;
  40. try {
  41. customerList = qr.query(sql, new BeanListHandler<Customer>(Customer.class),"%"+currentDate);
  42. } catch (SQLException e) {
  43. // TODO Auto-generated catch block
  44. e.printStackTrace();
  45. }
  46. //2.发邮件
  47. if(customerList!=null&&customerList.size()>0){
  48. for(Customer c:customerList){
  49. String emailMsg="亲爱的:"+c.getRealname()+",生日快乐!";
  50. try {
  51. MailUtils.sendMail(c.getEmail(), "happy..birthday", emailMsg);
  52. System.out.println(c.getRealname()+"邮件发送完毕");
  53. } catch (MessagingException e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59. }
  60. },new Date(),10*1000);
  61. //实际开发中起始时间是一个固定的时间
  62. //实际开发中间隔时间是1天
  63. }
  64.  
  65. @Override
  66. public void contextDestroyed(ServletContextEvent sce) {
  67. // TODO Auto-generated method stub
  68.  
  69. }
  70.  
  71. }

java模拟生日发祝福的更多相关文章

  1. Java模拟登录系统抓取内容【转载】

    没有看考勤的习惯,导致我的一天班白上了,都是钱啊,系统也不发个邮件通知下....     为了避免以后还有类似状况特别写了个java模拟登录抓取考勤内容的方法(部分代码来自网络),希望有人修改后也可以 ...

  2. websocket通信 实现java模拟一个client与webclient通信

    发文原由: 熟悉socket通信的同学,对于socket模拟server与client,实现相互通信, 或者使用websocket与java模拟的websocket服务器通信(比如一个聊天室),对于这 ...

  3. 浏览器与服务器交互原理以及用java模拟浏览器操作v

    浏览器应用服务器JavaPHPApache * 1,在HTTP的WEB应用中, 应用客户端和服务器之间的状态是通过Session来维持的, 而Session的本质就是Cookie, * 简单的讲,当浏 ...

  4. java模拟post请求发送json

    java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main ...

  5. java 模拟qq源码

    java 模拟qq源码: http://files.cnblogs.com/files/hujunzheng/QQ--hjzgg.zip

  6. java模拟开锁

    java模拟开锁 service qq:928900200 Introduction to Computer Science II: CSCI142Fall 2014Lab #1Instructor: ...

  7. Jsoup实现java模拟登陆

    Jsoup实现java模拟登陆 2013-10-29 14:52:05|  分类: web开发|举报|字号 订阅     下载LOFTER我的照片书  |     1:如何获取cookies. 1.1 ...

  8. [Java] 模拟HTTP的Get和Post请求

    在之前,写了篇Java模拟HTTP的Get和Post请求的文章,这篇文章起源与和一个朋友砍飞信诈骗网站的问题,于是动用了Apache的comments-net包,也实现了get和post的http请求 ...

  9. Java模拟登陆02【转载】

    在使用java访问URL时,如果该URL需要身份验证,那么就不能够直接访问,因为没有登陆.那么,如何解决这个问题呢?     方法是使用java模拟登陆,登陆后记录下cookie信息,在下次发起请求时 ...

随机推荐

  1. bootstrap设计进度条和圆点

    1.设计进度条.文字前面的圆点和图片 2.思路: (1)设计进度条 (a) 进度条有滚动效果,要加上类.active (b)进度条的颜色通过类.progress-bar-success来写,可以写成. ...

  2. gettid 和pthread_self的区别

    转: Linux中,每个进程有一个pid,类型pid_t,由getpid()取得.Linux下的POSIX线程也有一个id,类型 pthread_t,由pthread_self()取得,该id由线程库 ...

  3. [转]ANDROID JNI之JAVA域与c域的互操作

    本文讲述AndroidJava域与C域互操作:Java域调用c域的函数:c域访问Java域的属性和方法:c域生成的对象的保存与使用.重点讲解c域如何访问Java域. 虽然AndroidJNI实现中,c ...

  4. 一个python的文件对比脚本

    脚本主要用来给游戏客户端做热更的. 处理方式就是针对每个文件求其MD5值,再根据文件的目录和名字对比两个版本的MD5值,如果不一样,则这次热更就需要更新这个文件. 用法很简单. 1,生成MD5码列表 ...

  5. 《Cracking the Coding Interview》——第16章:线程与锁——题目1

    2014-04-27 19:09 题目:线程和进程有什么区别? 解法:理论题,操作系统教材上应该有很详细的解释.我回忆了一下,写了如下几点. 代码: // 16.1 What is the diffe ...

  6. CSS系列(7)CSS类选择器Class详解

    这一篇文章,以笔记形式写. 1,  CSS 类选择器详解 http://www.w3school.com.cn/css/css_selector_class.asp 知识点: (1)    使用类选择 ...

  7. CSS系列(5)-如何使用Firebug查看网页的html和css

    Firebug是火狐浏览器Firefox的一个插件,专门为开发人员开发的.使用Firebug需要先在Firefox中安装这个插件,网上有很多教程,可以对照着安装一下. 不同的火狐浏览器版本中的Fire ...

  8. 去除文件夹中的.svn

    一.在Dos窗口中运行如下命令 for/r <你项目的路径> %i in (.svn) do rd /s /q %i 二.将“Delete SVN Folders”操作添加到右击菜单中 建 ...

  9. eclipse里导入maven项目有红叉的解决办法

    导入maven的项目上有红叉,说明eclipse里maven的插件该更新了 1.help里选择install new software 2.点击add,输入name:MavenArchiver, lo ...

  10. 孤荷凌寒自学python第六十四天学习mongoDB的基本操作并进行简单封装3

    孤荷凌寒自学python第六十四天学习mongoDB的基本操作并进行简单封装3 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第十天. 今天继续学习mongoDB的简单操作, ...