昨天下午和今天早上重新对程序进行了一下修改,增加了如下功能:
一、首次运行时会先暂停五秒,然后每24小时运行一次
二、每次运行程序的时候会先清空一次数据库,然后再执行抓取、解析、插入的工作
三、增加了程序30%的注释
四、部分类和主函数之间面向对象的实现得到了改善
五、发布了JSP的WEB页面,WEB页面有两个:Index和detail。Index主要显示数据库city_info中的数据项信息,detail主要通过页面传参的形式通过city_info及weather_info两张表的联合查询,显示出Index页面鼠标点击城市的对应天气信息。每个城市都有自己的id值,这个id值就是对外发布出去的HTTP接口,页面运行结果如以下几个页面的地址所示:
http://localhost:8080/WeatherWeb/ShowDetail.jsp?id=101050301
{"weatherinfo":{"city":"牡丹江","date":2011年12月1日","year":辛卯年","week":星期四","description":天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。","temp1":"-7℃~-18℃","winddricetion1":"西风3-4级转微风","windpower1":"3-4级转小于3级 ","temp2":"-6℃~-15℃","winddricetion2":"西北风3-4级转微风","windpower2":"3-4级转小于3级
","temp3":"-6℃~-15℃","winddricetion3":"西南风3-4级转微风","windpower3":"3-4级转小于3级 ","temp4":"-6℃~-16℃","winddricetion4":"西南风3-4级转微风","windpower4":"3-4级转小于3级 ","temp5":"-6℃~-17℃","winddricetion5":"西南风3-4级转微风","windpower5":"3-4级转小于3级"}}
http://localhost:8080/WeatherWeb/ShowDetail.jsp?id=101060301
{"weatherinfo":{"city":"延吉","date":2011年12月1日","year":辛卯年","week":星期四","description":天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。","temp1":"-6℃~-17℃","winddricetion1":"西风转东风小于3级","windpower1":"小于3级 ","temp2":"-4℃~-12℃","winddricetion2":"东风转西风小于3级","windpower2":"小于3级
","temp3":"-4℃~-12℃","winddricetion3":"西风3-4级转小于3级","windpower3":"3-4级转小于3级 ","temp4":"-2℃~-13℃","winddricetion4":"西风小于3级","windpower4":"小于3级 ","temp5":"-3℃~-11℃","winddricetion5":"西风小于3级","windpower5":"小于3级"}}
http://localhost:8080/WeatherWeb/ShowDetail.jsp?id=101081000
{"weatherinfo":{"city":"呼伦贝尔","date":2011年12月1日","year":辛卯年","week":星期四","description":天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。","temp1":"-17℃~-26℃","winddricetion1":"微风","windpower1":"小于3级 ","temp2":"-11℃~-20℃","winddricetion2":"微风","windpower2":"小于3级
","temp3":"-12℃~-25℃","winddricetion3":"微风","windpower3":"小于3级 ","temp4":"-7℃~-29℃","winddricetion4":"微风","windpower4":"小于3级 ","temp5":"-9℃~-28℃","winddricetion5":"微风","windpower5":"小于3级"}}
六、将数据库中解析出的字段通过拼字符串的方法重新转化为JSON格式对外提供接口
需要改善增加的地方有:
一、程序面向对象的体现不是很好
二、增加历史表,每天自动运行程序时不是使用truncate清空表,而是将历史记录转移至历史表中
三、定时器,改造为每天,比如零点或者六点自动运行
四、放弃JSP,改用SERVLET的方法重新制作WEB端,需要学习WEB开发!
最近的事情也要做个规划:
一、今天周四了,打算利用这周四、五、六、日的业余时间学习制作一下马士兵的BBS项目,然后自己动手改造做一个自己的项目。旨在熟悉WEB开发,前台后台管理员、登陆等等。毕竟以前从未做过WEB开发。
二、 下周利用时间学习制作下马士兵的网上商城项目,然后自己动手也跟着学习制作一个自己的网上商城。
三、SERVLET还是前提啊。。。。需要抓紧学习。
增加的部分代码如下:
主函数定时器实现:
public class TestWeather { /** * @param args */ public static void main(String[] args) { Timer tt=new Timer();//设置定时器 TimerTaskMine tk=new TimerTaskMine(); tt.schedule(tk, 1000*5, 1000*3600*24);//程序不关,每次执行先暂停五秒,以后每隔24小时运行一次 } } class TimerTaskMine extends TimerTask{ private static GetInfo get;//调用静态方法从GetInfo类中获取网页信息 @Override public void run() { //private static ReadJson read;//调用静态方法从ReadJson类中读取文件信息 //private static InputStream inputStream; /* 主函数 */ //int i = 0; //Properties可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串 Properties prop = new Properties(); InputStream inputStream = new TestWeather().getClass().getResourceAsStream("/weather_code.properties"); try { prop.load(inputStream);//从输入流中读取属性列表 // System.out.println(prop.getProperty("101020100")); Enumeration<?> en = prop.propertyNames();//返回属性列表中所有键的枚举 String obj = ""; // get=new GetInfo(); // get.getInfo("101020100"); TruncateMysql.truncateMysql();//清空数据库数据表内容 System.out.println("truncate over!"); while (en.hasMoreElements()) {//测试此枚举是否包含更多的元素。 obj = (String) en.nextElement();//obj即为weather_code.properties中获得的城市代码 // System.out.println(obj); // get.getInfo(Integer.parseInt(obj)); get = new GetInfo(); // System.out.println(obj.); get.getInfo(obj); // 抓取网页 // get=new GetInfo(); // get.getInfo(obj); // get=new GetInfo(); // get.getInfo(Integer.parseInt(obj)); } } catch (IOException e) { e.printStackTrace(); } finally { // 关闭IO资源 try { if (null != inputStream) { inputStream.close(); inputStream = null; } } catch (IOException e) { e.printStackTrace(); } } System.out.println("helloworld"); } }
清空表的实现:
public class TruncateMysql { public static void truncateMysql() { //将连接和预编译的 SQL语句的对象初始化为0 Statement stmt=null; Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver");//Load the Driver conn=DriverManager.getConnection("jdbc:mysql://10.1.101.223:3306/weather", "appuser", "opzoon123!");//Connect to the DataBase stmt=conn.createStatement(); String sql="truncate table weather_info"; stmt.executeUpdate(sql);//Execute the SQL }catch (ClassNotFoundException e) { e.printStackTrace(); }catch (SQLException e) { e.printStackTrace(); }finally {//close try {//按照顺序先关闭stmt再关闭conn if(stmt!=null) { stmt.close(); stmt=null; } }catch (SQLException e) { e.printStackTrace(); } try { if(conn!=null) { conn.close(); conn=null; } }catch (SQLException e) { e.printStackTrace(); } } }
JSP主页面实现:
<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <%@ page import="java.sql.*" %> <% Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://10.1.101.223:3306/weather", "appuser", "opzoon123!"); Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery("select * from city_info"); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk"> <title>天气预报发布页主页</title> </head> <body> <table border="1"> <% while(rs.next()) { %> <tr> <td><%= rs.getInt("id") %></td> <td><%= rs.getInt("city_id") %></td> <td><a href="ShowDetail.jsp?id=<%= rs.getInt("city_id") %>"><%= rs.getString("city_name") %></a></td> <%-- <td><%= rs.getString("weather_date") %></td> <td><%= rs.getString("weather_year") %></td> <td><%= rs.getString("weather_week") %></td> <td><%= rs.getString("weather_temp") %></td> <td><%= rs.getString("weather_winddriection") %></td> <td><%= rs.getString("weather_windpower") %></td> <td><%= rs.getString("weather_desprition") %></td> --%> </tr> <% } %> </table> </body> <% rs.close(); stmt.close(); conn.close(); %> </html>
JSP detail页面的实现
将内容转化为JSON格式
<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <%@ page import="java.sql.*" %> <% String city_id=request.getParameter("id"); Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://10.1.101.223:3306/weather", "appuser", "opzoon123!"); Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery("select city_name,weather_date,weather_year,weather_week,weather_temp,weather_winddirection,weather_windpower,weather_description from city_info,weather_info where city_info.city_id=weather_info.city_id and city_info.city_id='"+city_id+"'"); %> <% boolean flag = true; int i=0; int j=0; int k=0; int m=0; while(rs.next()) { %> <%if(flag){ %>{"weatherinfo":{"city":"<%= rs.getString("city_name") %>","date":<%= rs.getString("weather_date") %>","year":<%= rs.getString("weather_year") %>","week":<%= rs.getString("weather_week") %>","description":<%= rs.getString("weather_description") %><% flag = false; } %>","temp<%++i;out.print(i);%>":"<%= rs.getString("weather_temp")%>","winddricetion<%++j;out.print(j);%>":"<%= rs.getString("weather_winddirection")%>","windpower<%++k;out.print(k);%>":"<%= rs.getString("weather_windpower")%><%}%>"}} <% rs.close(); stmt.close(); conn.close(); %>