mysql数据库抓取验收_天气预报之抓取、解析、存入MYSQL数据库模块实现,不规范(代码)...

package com.zzk.cn;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.ProtocolException;

import java.net.URL;

import java.sql.*;

import java.util.Enumeration;

import java.util.Properties;

import net.sf.json.JSONObject;

/**

*

* @author zhuzhengke

* @version 1.0.0

*

*/

public class Json {

static String info = "";

/*主函数*/

public static void main(String[] args) throws UnsupportedEncodingException {

//抓取网页

getInfo(101010100);

//解析字段

readJson();

//insertMysql();

}

public class GetInfo {

public void getInfo(int id) {

String path = "http://m.weather.com.cn/data/"+id+".html";

URL url;

String inputline = "";

InputStream input = null;

InputStreamReader reader = null;

BufferedReader buffer = null;

try {

url = new URL(path);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setReadTimeout(10 * 1000);

conn.setRequestMethod("GET");

input = conn.getInputStream();

reader = new InputStreamReader(input,"utf8");

buffer = new BufferedReader(reader);

while ((inputline = buffer.readLine()) != null) {

info += inputline;

}

} catch (ProtocolException e) {

e.printStackTrace();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally {

//关闭资源

try {

if (null != buffer) {

buffer.close();

}

} catch (IOException e) {

e.printStackTrace();

}

buffer = null;

try {

if (null != reader) {

reader.close();

}

} catch (IOException e) {

e.printStackTrace();

}

reader = null;

try {

if (null != input) {

input.close();

}

} catch (IOException e) {

e.printStackTrace();

}

input = null;

}

}

}

/**

* 获取网页信息

*/

public static void getInfo(int id) {

String path = "http://m.weather.com.cn/data/"+id+".html";

URL url;

String inputline = "";

InputStream input = null;

InputStreamReader reader = null;

BufferedReader buffer = null;

try {

url = new URL(path);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setReadTimeout(10 * 1000);

conn.setRequestMethod("GET");

input = conn.getInputStream();

reader = new InputStreamReader(input,"utf8");

buffer = new BufferedReader(reader);

while ((inputline = buffer.readLine()) != null) {

info += inputline;

}

} catch (ProtocolException e) {

e.printStackTrace();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally {

//关闭资源

try {

if (null != buffer) {

buffer.close();

}

} catch (IOException e) {

e.printStackTrace();

}

buffer = null;

try {

if (null != reader) {

reader.close();

}

} catch (IOException e) {

e.printStackTrace();

}

reader = null;

try {

if (null != input) {

input.close();

}

} catch (IOException e) {

e.printStackTrace();

}

input = null;

}

}

/**

* Json解析

*/

public static void readJson() {

JSONObject jsonob = JSONObject.fromObject((JSONObject.fromObject(info)

.getString("weatherinfo")));

String city = jsonob.getString("city");// 城市

System.out.println(city);

String date_y = jsonob.getString("date_y");// 时间

System.out.println(date_y);

String date = jsonob.getString("date");// 农历年

System.out.println(date);

String week = jsonob.getString("week");// 星期

System.out.println(week);

String cityid = jsonob.getString("cityid");// 城市号

System.out.println(cityid);

String temp1 = jsonob.getString("temp1");// 以下是六天内摄氏温度

System.out.println(temp1);

String temp2 = jsonob.getString("temp2");

System.out.println(temp2);

String temp3 = jsonob.getString("temp3");

System.out.println(temp3);

String temp4 = jsonob.getString("temp4");

System.out.println(temp4);

String temp5 = jsonob.getString("temp5");

System.out.println(temp5);

String temp6 = jsonob.getString("temp6");

System.out.println(temp6);

String tempF1 = jsonob.getString("tempF1");// 以下是六天内华氏温度

System.out.println(tempF1);

String tempF2 = jsonob.getString("tempF2");

System.out.println(tempF2);

String tempF3 = jsonob.getString("tempF3");

System.out.println(tempF3);

String tempF4 = jsonob.getString("tempF4");

System.out.println(tempF4);

String tempF5 = jsonob.getString("tempF5");

System.out.println(tempF5);

String tempF6 = jsonob.getString("tempF6");

System.out.println(tempF6);

String weather1 = jsonob.getString("weather1");// 以下是六天天气

System.out.println(weather1);

String weather2 = jsonob.getString("weather2");

System.out.println(weather2);

String weather3 = jsonob.getString("weather3");

System.out.println(weather3);

String weather4 = jsonob.getString("weather4");

System.out.println(weather4);

String weather5 = jsonob.getString("weather5");

System.out.println(weather5);

String weather6 = jsonob.getString("weather6");

System.out.println(weather6);

String wind1 = jsonob.getString("wind1");// 以下六天为风力

System.out.println(wind1);

String wind2 = jsonob.getString("wind2");

System.out.println(wind2);

String wind3 = jsonob.getString("wind3");

System.out.println(wind3);

String wind4 = jsonob.getString("wind4");

System.out.println(wind4);

String wind5 = jsonob.getString("wind5");

System.out.println(wind5);

String wind6 = jsonob.getString("wind6");

System.out.println(wind6);

String fl1 = jsonob.getString("fl1");// 以下为六天风级

System.out.println(fl1);

String fl2 = jsonob.getString("fl2");

System.out.println(fl2);

String fl3 = jsonob.getString("fl3");

System.out.println(fl3);

String fl4 = jsonob.getString("fl4");

System.out.println(fl4);

String fl5 = jsonob.getString("fl5");

System.out.println(fl5);

String fl6 = jsonob.getString("fl6");

System.out.println(fl6);

String index_d = jsonob.getString("index_d");// 当日穿衣指数

System.out.println(index_d);

insertMysql(cityid,date_y,date,week,temp1,wind1, fl1, index_d);

}

public static void insertMysql(String cityid,String date_y,String date,String week,String temp1,String wind1,String fl1,String index_d) {

Connection conn = null;

PreparedStatement pstmt=null;

try {

System.out.println("HelloWorld!");

Class.forName("com.mysql.jdbc.Driver");

// new oracle.jdbc.driver.OracleDriver;

conn=DriverManager.getConnection("jdbc:mysql://10.1.101.223:3306/weather", "appuser", "opzoon123!");

pstmt=conn.prepareStatement("insert into weather_info(city_id,weather_date,weather_year,weather_week,weather_temp,weather_winddirection,weather_windpower,weather_description) values (?,?,?,?,?,?,?,?)");

pstmt.setInt(1, Integer.parseInt(cityid));

pstmt.setString(2, date_y);

pstmt.setString(3, date);

pstmt.setString(4, week);

pstmt.setString(5, temp1);

pstmt.setString(6, wind1);

pstmt.setString(7,fl1);

pstmt.setString(8,index_d);

pstmt.executeUpdate();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

if (pstmt !=null) {

pstmt.close();

pstmt=null;

}

}catch (SQLException e) {

e.printStackTrace();

}

try {

if (conn != null) {

conn.close();

conn = null;

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值