多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
![](https://box.kancloud.cn/3475dbacb3b5e671dccc3570fad3666c_719x378.png) PromotionListener.java ~~~ package zyw.promotion; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import zyw.bean.Product; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import java.math.BigDecimal; import java.sql.SQLException; import java.util.Date; import java.util.List; import java.util.Timer; import java.util.TimerTask; @WebListener() public class PromotionListener implements ServletContextListener{ public PromotionListener() { } public void contextInitialized(ServletContextEvent sce) { Timer timer=new Timer();//创建定时器 //1.要执行的任务2.第一次执行的时间3.间隔的时长 timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { try {//快捷键,选中代码块,Ctrl+Art+t ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource(); QueryRunner queryRunner=new QueryRunner(comboPooledDataSource); String sql="select * from product"; List<Product> productList = queryRunner.query(sql, new BeanListHandler<Product>(Product.class)); // 获取商品信息后遍历 商品 根据不同的种类,修改不同的价格 if (productList!=null&&productList.size()>0){ for (Product product : productList) { if (product.getC_id()==1){ //数码产品 BigDecimal p_price = product.getP_price(); //减100元 BigDecimal temp=new BigDecimal(100); p_price=p_price.subtract(temp); //将修改好的价格设置给对象 product.setP_price(p_price); System.out.println(product.getP_name()+"现在打折促销啦,只要"+ product.getP_price()+"就可以拿回家啦!"); }else { //数码产品 BigDecimal p_price = product.getP_price(); //减100元 BigDecimal temp=new BigDecimal(10); p_price=p_price.subtract(temp); //将修改好的价格设置给对象 product.setP_price(p_price); System.out.println(product.getP_name()+"现在打折促销啦,只要"+ product.getP_price()+"就可以拿回家啦!"); } } } } catch (SQLException e) { e.printStackTrace(); } } }, new Date(), 1000*20); } public void contextDestroyed(ServletContextEvent sce) { } } ~~~ Product.java ~~~ package zyw.bean; import java.math.BigDecimal; public class Product { private int p_id; private String p_name; private BigDecimal p_price; private String p_image; private int c_id; @Override public String toString() { return "Product{" + "p_id=" + p_id + ", p_name='" + p_name + '\'' + ", p_price=" + p_price + ", p_image='" + p_image + '\'' + ", c_id=" + c_id + '}'; } public Product() { } public int getP_id() { return p_id; } public void setP_id(int p_id) { this.p_id = p_id; } public String getP_name() { return p_name; } public void setP_name(String p_name) { this.p_name = p_name; } public BigDecimal getP_price() { return p_price; } public void setP_price(BigDecimal p_price) { this.p_price = p_price; } public String getP_image() { return p_image; } public void setP_image(String p_image) { this.p_image = p_image; } public int getC_id() { return c_id; } public void setC_id(int c_id) { this.c_id = c_id; } } ~~~ c3p0-config.xml放在src目录下 ~~~ <?xml version="1.0" encoding="utf-8" ?> <c3p0-config> <!-- 默认配置,如果没有指定则使用这个配置 --> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://122.14.200.136:3306/javadb</property> <property name="user">root</property> <property name="password">6a133f0024</property> <!-- 初始化池大小 --> <property name="initialPoolSize">5</property> <!-- 最大空闲时间 --> <property name="maxIdleTime">30</property> <!-- 最多有多少个连接 --> <property name="maxPoolSize">10</property> <!-- 最少几个连接 --> <property name="minPoolSize">5</property> <!-- 每次最多可以执行多少个批处理语句 --> <property name="maxStatements">50</property> </default-config> <!-- 命名的配置 --> <named-config name="51zixue"><!--这里是设置配置文件的名字--> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://122.14.200.136:3306/javadb</property> <property name="user">root</property><!--mysql的登陆名--> <property name="password">6a133f0024</property><!--如果没密码就可以设置成<property name="password"></property>--> <property name="acquireIncrement">5</property><!-- 如果池中数据连接不够时一次增长多少个 --> <property name="initialPoolSize">10</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">15</property> <property name="maxStatements">0</property> <property name="maxStatementsPerConnection">5</property> <!-- he's important, but there's only one of him --> </named-config> </c3p0-config> ~~~ jar包 ![](https://box.kancloud.cn/54ac10fd5b77b2861cca4412bb2c6fe9_302x152.png) 数据库 ![](https://box.kancloud.cn/5db711f313d01b4ed2b5eafac3b36ede_543x264.png) ![](https://box.kancloud.cn/0fb163375716c9b37e0bc44705e24434_841x323.png) 结果,每隔20秒刷新一次 ![](https://box.kancloud.cn/c8959ab3c35b45f7f0b8eaa98d74e559_620x191.png)