已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
微信开发纪实之历史上的今天服务说明:此篇文章是看了柳峰老师的文章(/lyq8479/article/details/12785115)后自己动手写的。相比于柳峰老师的方法,我觉得我的方法有两项优点:1. 主服务代码量更少(得益于HttpClient和Jsoup)2. 实现了数据的平滑过渡语言:java代码:TodayInHistoryService.javapackage com.wdyx.weixin.service;import java.io.IOException;import org.apache.http.client.ClientProtocolException;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements; /* * 历史上的今天 服务 * author 帮杰 * */public class TodayInHistoryService /数据源public static final String URL = ;private String todayInHistoryInfo = ; public TodayInHistoryService() throws ClientProtocolException, IOExceptionStringBuffer buffer = new StringBuffer();/得到网页源码String html = HttpUtil.getHtml(URL);/用Jsoup解析Document doc = Jsoup.parse(html); Elements elements = doc.select(div.listren).select(a); for(Element element : elements) buffer.append(element.text().append(nn); todayInHistoryInfo = buffer.substring(0, buffer.lastIndexOf(nn); public String getTodayInHistoryInfo()return todayInHistoryInfo; /* * 测试 * * param args * throws IOException * throws ClientProtocolException */ public static void main(String args) throws ClientProtocolException, IOException System.out.println(new TodayInHistoryService().getTodayInHistoryInfo(); 测试结果:1564年2月15日 欧洲近代自然科学的创始人伽利略诞辰1682年2月15日 顾炎武逝世1823年2月15日 洋务运动倡导者李鸿章诞辰1857年2月15日 俄国音乐家格林卡逝世1904年2月15日 反清组织华兴会成立1912年2月15日 袁世凯任临时大总统1935年2月15日 东北抗日联军发表统一建制宣言1937年2月15日 国民党内外政策开始转变1942年2月15日 新加坡落入日军手中1946年2月15日 世界第一台电子计算机问世1952年2月15日 乔治六世下葬1953年2月15日 中央颁布关于农业生产互助合作的决议1957年2月15日 葛罗米柯就任外交部长1961年2月15日 美国18名滑冰运动员在空中丧生1972年2月15日 中国人民的朋友埃德加斯诺逝世1974年2月15日 苏联驱逐作家索尔仁尼琴1982年2月15日 我国公布首批历史文化名城1989年2月15日 苏联军队全部撤出阿富汗1990年2月15日 美国等四国就联合反毒签署卡塔赫纳声明为了不频繁调用服务通过网络抓取信息,我又写了一个用于数据的平滑过渡的类。代码:TodayInHistoryServiceUtil.javapackage com.wdyx.weixin.service;import java.io.IOException;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.sql.Timestamp;import org.apache.http.client.ClientProtocolException;import com.wdyx.weixin.util.MySQLUtil;/* * 这个是“历史上的今天”的工具类,主要用于数据的平滑过渡: * 1、将当日抓取数据写入数据库 * 2、将当日数据从数据库中取出 * author 帮杰 * */public class TodayInHistoryServiceUtil /* * 初始化数据库 * return * throws SQLException */public static int initDB() throws SQLExceptionint count = -1;String sql = create table if not exists TodayInHistory(+ today timestamp not null default current_timestamp,+ history varchar(1024) not null default ,+ primary key(today) + comment=todayInHistory default character set utf8 collate utf8_bin;Connection con = MySQLUtil.getConnection();Statement sm = con.createStatement();count = sm.executeUpdate(sql);return count;/* * 得到数据写入时间(以判断数据是否过期) * return 数据写入时间 */public static Timestamp getTodayFromDB()Timestamp today = null;String Query = SELECT today FROM TodayInHistory;Connection con = MySQLUtil.getConnection();Statement sm = null;ResultSet rs = null;try sm = con.createStatement();rs = sm.executeQuery(Query);if (rs.next() today = rs.getTimestamp(1); catch (SQLException e) e.printStackTrace(); finally MySQLUtil.closeConnection(sm, rs, con);return today;/* * 判断数据是否过期 * return */SuppressWarnings(deprecation)public static boolean isOutOfDate()int date_now = new java.util.Date().getDate();int date_DB = getTodayFromDB().getDate();return date_now=date_DB?false:true;/* * 从数据库取出“历史上的今天”数据 * return “历史上的今天”数据 */public static String getHistoryFromDB()String history = null;String Query = SELECT history FROM TodayInHistory;Connection con = MySQLUtil.getConnection();Statement sm = null;ResultSet rs = null;try sm = con.createStatement();rs = sm.executeQuery(Query);if (rs.next() history = rs.getString(1); catch (SQLException e) e.printStackTrace(); finally MySQLUtil.closeConnection(sm, rs, con);return history;/* * 更新数据表 * return 最新数据 * throws IOException * throws ClientProtocolException */public static String updateDB() throws ClientProtocolException, IOExceptionString history = new TodayInHistoryService().getTodayInHistoryInfo();Connection con = MySQLUtil.getConnection();Statement sm = null;try sm = con.createStatement();sm.addBatch(DELETE FROM TodayInHistory);sm.addBatch(INSERT INTO TodayInHistory (today,history) VALUES (CURRENT_TIMESTAMP,+history+);sm.executeBatch(); catch (SQLException e) e.printStackTrace(); finally MySQLUtil.closeConnection(sm,null,null);return history;/* * 删除数据表 * return */public static boolean deleteDB()boolean flag = true;String sql = DROP TABLE TodayInHistory;Connection con = MySQLUtil.getConnection();Statement sm = null;try sm = con.createStatement();flag = sm.execute(sql); catch (SQLException e) flag = false;e.printStackTrace(); finally MySQLUtil.closeConnection(sm,null,null);return flag;/* * 流程控制 * throws IOException * throws ClientProtocolException * throws SQLException */public static String getTodayInHistoryInfoFluently() throws ClientProtocolException, IOException String history = null;/有两种情况需要更新表:1、表中无记录;2、表中有记录,但记录已过期if(getHistoryFromDB()=null|isOutOfDate()history = updateDB();elsehistory = getHistoryFromDB();return history;/测试
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年北京市石景山医院皮肤性病治疗中心医护人员招聘笔试试题及答案详解
- 2026年河北北方学院附属第三医院张家口市建国医院医护人员招聘笔试备考题库及答案解析
- 2026四川九洲软件有限公司(合并)招聘销售经理 (铁路能源)等岗位9人笔试参考题库及答案详解
- 2026年梁平县中医医院医护人员招聘笔试备考题库及答案解析
- 2025年大连市甘井子区辛寨子地区医院医护人员招聘笔试试题及答案详解
- 中医健康实施方案
- 双壁波纹管工程作业指导方案
- 云原生应用架构现代化改造方案
- 宁安市2026学年四下数学期末质量检测试题(含答案解析)
- 浦口区沿江街道招聘社区网格员真题附答案详解
- 2026年全国高考语文(全国Ⅰ卷)真题及答案
- 2026年7月自考13996旅游接待业押题及答案
- 2026春西师大版小学数学四年级下册期末综合测试卷含答案
- IATF16949 五大核心工具综合培训(APQP-FMEA-SPC-MSA-PPAP)
- 2026年(春新版)道德与法治二年级下册1-4单元全套试卷
- 浙江省绍兴市柯桥区2024-2025学年七年级下学期期末数学试卷(含答案)
- 初中七年级道德与法治下册《让和声更美-集体生活中的个人与规则》教学设计
- (2026)学校园欺凌现状调查报告(3篇)
- (2026版)《电力重大事故隐患判定标准及治理监督管理规定》培训
- DB11T 2409-2025建筑屋顶光伏应用条件评估技术规范
- 2025年托育保健医考题库及答案
评论
0/150
提交评论