The web inserts a record into the database every 5 seconds-Learn notes

Source: Internet
Author: User
Tags uuid

Systemlistener
Import Java.util.timer;import java.util.timertask;import java.util.uuid;import javax.servlet.ServletContextEvent; Import Javax.servlet.servletcontextlistener;import Cn.itcast.web.dao.systemdao;public class SystemListener    Implements Servletcontextlistener {Private Timer timer = new timer ();            public void contextinitialized (Servletcontextevent sce) {try {Systemdao Systemdao = new Systemdao ();            Systemdao.createtable ("Systeminit");        Timer.schedule (New Systemtask (), 0,5*1000); } catch (Exception e) {}} public void contextdestroyed (Servletcontextevent sce) {try {S            Ystemdao Systemdao = new Systemdao ();            Systemdao.droptable ("Systeminit");        Abort timer timer.cancel ();            } catch (Exception e) {}}}//Task Class class Systemtask extends timertask{public void run () {try {            Systemdao Systemdao = new Systemdao (); Systemdao.init ("Systeminit", Uuid.randoMuuid (). toString ()); } catch (Exception e) {}}}
Table
*/import java.sql.SQLException;import org.apache.commons.dbutils.QueryRunner;import cn.itcast.web.util.JdbcUtil;public class SystemDao {    //删除表    public void dropTable(String tableName) throws SQLException{        QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());        String sql = "drop table if exists " + tableName;        runner.update(sql);    }    //创建表    public void createTable(String tableName) throws SQLException{        QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());        String sql = "create table if not exists "+tableName+"(id varchar(40) primary key,curr_time timestamp not null)";        runner.update(sql);    }    //初始化数据    public void init(String tableName,String id) throws SQLException{        QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());        String sql = "insert into "+tableName+"(id) values(?)";        runner.update(sql,id);    }}
Web. XML configuration

The web inserts a record into the database every 5 seconds-Learn notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.