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

所需jar包

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

3.连接数据库的工具类

package com.cc.birthday;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DataSourceUtils {
private static DataSource dataSource=new ComboPooledDataSource(); private static ThreadLocal<Connection> t1=new ThreadLocal<Connection>(); //直接可以获取一个连接池
public static DataSource getDataSource(){
return dataSource;
} //获取连接对象
public static Connection getConnection() throws SQLException{
Connection con=t1.get();
if(con==null){
con=dataSource.getConnection();
t1.set(con);
}
return con;
} //开启事务
public static void startTrasaction() throws SQLException {
Connection con=getConnection();
if(con!=null){
con.setAutoCommit(false);
}
} //事务回滚
public static void rollback() throws SQLException{
Connection con =getConnection();
if(con!=null){
con.rollback();
}
} //提交并且 关闭资源及从ThreadLocal中释放
public static void commitAndRelease() throws SQLException{
Connection con=getConnection();
if(con!=null){
con.commit();
con.close();
t1.remove();
}
} //关闭资源方法
public static void closeConnection() throws SQLException{
Connection con=getConnection();
if(con!=null){
con.close();
}
} public static void closeStatement(Statement st) throws SQLException {
if(st!=null){
st.close();
}
} public static void closeResultSet(ResultSet rs) throws SQLException{
if(rs!=null){
rs.close();
}
} }

4.发送邮件的工具类

package com.cc.mail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType; public class MailUtils { //email:邮件发给谁 subject:主题 emailMsg:邮件的内容
public static void sendMail(String email, String subject, String emailMsg)
throws AddressException, MessagingException { // 1.创建一个程序与邮件服务器会话对象 Session
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTP");//发邮件的协议
props.setProperty("mail.host", "smtp.163.com");//发送邮件的服务器地址
props.setProperty("mail.smtp.auth", "true");// 指定验证为true // 创建验证器
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("emailusername", "password");//发邮件的账号的验证
}
}; Session session = Session.getInstance(props, auth); // 2.创建一个Message,它相当于是邮件内容
Message message = new MimeMessage(session); message.setFrom(new InternetAddress("xxxxxx@163.com")); // 设置发送者 message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者 message.setSubject(subject);//邮件的主题 message.setContent(emailMsg, "text/html;charset=utf-8"); // 3.创建 Transport用于将邮件发送
Transport.send(message);
}
}

5.customer实体类

package com.cc.birthday;

public class Customer {
private int id;
private String username;
private String password;
private String realname;
private String birthday;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
} }

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

package com.cc.birthday;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask; import javax.mail.MessagingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler; import com.cc.birthday.Customer;
import com.cc.mail.MailUtils; public class BirthdayListener implements ServletContextListener{ @Override
public void contextInitialized(ServletContextEvent sce) {
// 当web应用启动开启任务调动---功能在用户的生日当天发送邮件
//开启一个定时器
Timer timer=new Timer();
timer.scheduleAtFixedRate(new TimerTask() { @Override
public void run() {
// 为当前的生日的用户发邮件
//1.获得今天过生日的人
//获得今天的日期
SimpleDateFormat format=new SimpleDateFormat("MM-dd");
String currentDate=format.format(new Date());
//根据当前时间从数据库查询今天过生日的人
QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
String sql="select * from customer where birthday like ?";
List<Customer> customerList=null;
try {
customerList = qr.query(sql, new BeanListHandler<Customer>(Customer.class),"%"+currentDate);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.发邮件
if(customerList!=null&&customerList.size()>0){
for(Customer c:customerList){
String emailMsg="亲爱的:"+c.getRealname()+",生日快乐!";
try {
MailUtils.sendMail(c.getEmail(), "happy..birthday", emailMsg);
System.out.println(c.getRealname()+"邮件发送完毕");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
},new Date(),10*1000);
//实际开发中起始时间是一个固定的时间
//实际开发中间隔时间是1天
} @Override
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub } }

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. 9.3centos7安装python3 以及tab补全功能

    1.安装python3 1.1下载python源码包 网址:https://www.python.org/downloads/release/python-362/ 下载地址:https://www. ...

  2. windows 定时任务 - 定时关机

    添加定时关机,刚好可以利用windows定时任务 [开始]->[控制面板]->[任务计划]->[添加任务计划] 1.找到 shutdown.exe 设置每天执行 2.设置晚上10点 ...

  3. postman与charles的结合使用

    1.准备charles环境 Charles端口一般配置的为8888,不知道怎么配置详见charles文档 打开charles,发现访问浏览器任意页面都是失败. 在浏览器的高级设置中设置代理服务器,以火 ...

  4. python学习笔记-基础

    1.大小写敏感 2. print (n,f,s1,s2,s3,s4,sep='\n')  -- 换行输出  seq='\n' print ('n=%d'%n,'f=%f'%f,'s1=%s'%s1,' ...

  5. Python全栈工程师(文件操作、编码)

    ParisGabriel                每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰     Python人工智能从入门到精通 最近简直要死了 发烧感冒 喉咙痛..... ...

  6. crmsh语法

    .查看配置信息 crm(live)# configure crm(live)configure# show node node1 node node2 property cib-bootstrap-o ...

  7. 网络编程--广播&组播

    广播 1.广播地址 如果用{netid, subnetid, hostid}( {网络ID,子网ID,主机ID})表示IPv4地址.那么有四种类型的广播地址,我们用-1表示所有比特位均为1的字段: 1 ...

  8. HDU 4760 Good Firewall ( Trie树 )

    一开始看的时候就想歪了,比赛的时候一直在YY线段树区间覆盖,然后纠结节点数太多开不下怎么办啊啊啊啊…… 然后昨天吃饭的时候也在纠结这到底是个啥题,后来发现公共前缀->前缀??!!!!->这 ...

  9. 软工实践 - 第十七次作业 Alpha 冲刺 (8/10)

    队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/articles/10023469.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长 ...

  10. 我与0xc000007b奋斗的日子

    自从新换了一台笔记本,就开始重装各种软件,就在将要开始软工课设的重要的日子里,我默默地在运行客户端时出现了一个这样的错误: 鉴于本人很废柴,自然不可能去查内存,所以开始各种度娘必应和谷歌,哦!原来应该 ...