package util;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.Date;
import db.DBHelper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class DBTest {
static String sql = null;
static DBHelper db1 = null;
public static void addWeather() {
PreparedStatement pst = null;
sql = "insert into weathers(city,update_time,data,humidity,pm25,pm10,quality,temperature,generalization,high,low,aqi,wind_direction,wind_power,type,"
+ "t_data,t_high,t_low,t_aqi,t_wind_direction,t_wind_power,t_type,"
+ "n_data,n_high,n_low,n_aqi,n_wind_direction,n_wind_power,n_type) "
+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";//SQL语句
db1 = new DBHelper();//创建DBHelper对象
try {
System.out.println("======获取天气中======");
String str = TestWeather.Test();
ObjectMapper obj = new ObjectMapper();
JsonNode node = obj.readValue(str, JsonNode.class);
//city
JsonNode cityNode = node.get("city");
String city = cityNode.asText();
//update_time
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//data
JsonNode dataNode = node.get("data");
JsonNode forecastNode = dataNode.get("forecast");
JsonNode forecastNode0_date = forecastNode.get(0);
JsonNode dateNode = forecastNode0_date.get("date");
String forecast_date = dateNode.asText();
//humidity
JsonNode shiduNode = dataNode.get("shidu");
String shidu = shiduNode.asText();
//pm25
JsonNode pm25Node = dataNode.get("pm25");
String pm25 = pm25Node.asText();
//pm10
JsonNode pm10Node = dataNode.get("pm10");
String pm10 = pm10Node.asText();
//quality
JsonNode qualityNode = dataNode.get("quality");
String quality = qualityNode.asText();
//temperature
JsonNode temperatureNode = dataNode.get("wendu");
String temperature = temperatureNode.asText();
//generalization
JsonNode forecastNode_generalization = forecastNode0_date.get("notice");
String forecast_generalization = forecastNode_generalization.asText();
//high
JsonNode forecastNode_high = forecastNode0_date.get("high");
String forecast_high = forecastNode_high.asText();
//low
JsonNode forecastNode_low = forecastNode0_date.get("low");
String forecast_low = forecastNode_low.asText();
//aqi
JsonNode forecastNode_aqi = forecastNode0_date.get("aqi");
String forecast_aqi = forecastNode_aqi.asText();
//fx
JsonNode forecastNode_fx = forecastNode0_date.get("fx");
String forecast_fx = forecastNode_fx.asText();
//fl
JsonNode forecastNode_fl = forecastNode0_date.get("fl");
String forecast_fl = forecastNode_fl.asText();
//type
JsonNode forecastNode_type = forecastNode0_date.get("type");
String forecast_type = forecastNode_type.asText();
//t_date
JsonNode forecastNode1_date = forecastNode.get(1);
JsonNode dateNode1 = forecastNode1_date.get("date");
String t_date = dateNode1.asText();
//t_high
JsonNode highNode1 = forecastNode1_date.get("high");
String t_high = highNode1.asText();
//t_low
JsonNode lowNode1 = forecastNode1_date.get("low");
String t_low = lowNode1.asText();
//t_aqi
JsonNode aqiNode1 = forecastNode1_date.get("aqi");
String t_aqi = aqiNode1.asText();
//t_fx
JsonNode fxNode1 = forecastNode1_date.get("fx");
String t_fx = fxNode1.asText();
//t_fl
JsonNode flNode1 = forecastNode1_date.get("fl");
String t_fl = flNode1.asText();
//t_type
JsonNode typeNode1 = forecastNode1_date.get("type");
String t_type = typeNode1.asText();
//n_data
JsonNode forecastNode2_date = forecastNode.get(2);
JsonNode dateNode2 = forecastNode2_date.get("date");
String n_date = dateNode2.asText();
//n_high
JsonNode highNode2 = forecastNode2_date.get("high");
String n_high = highNode2.asText();
//n_low
JsonNode lowNode2 = forecastNode2_date.get("low");
String n_low = lowNode2.asText();
//n_aqi
JsonNode aqiNode2 = forecastNode2_date.get("aqi");
String n_aqi = aqiNode2.asText();
//n_fx
JsonNode fxNode2 = forecastNode2_date.get("fx");
String n_fx = fxNode2.asText();
//n_fl
JsonNode flNode2 = forecastNode2_date.get("fl");
String n_fl = flNode2.asText();
//n_type
JsonNode typeNode2 = forecastNode2_date.get("type");
String n_type = typeNode2.asText();
try {
pst = db1.conn.prepareStatement(sql);
pst.setString(1, city);
pst.setString(2, df.format(new Date()));
pst.setString(3, forecast_date);
pst.setString(4, shidu);
pst.setString(5, pm25);
pst.setString(6, pm10);
pst.setString(7, quality);
pst.setString(8, temperature);
pst.setString(9, forecast_generalization);
pst.setString(10, forecast_high);
pst.setString(11, forecast_low);
pst.setString(12, forecast_aqi);
pst.setString(13, forecast_fx);
pst.setString(14, forecast_fl);
pst.setString(15, forecast_type);
pst.setString(16, t_date);
pst.setString(17, t_high);
pst.setString(18, t_low);
pst.setString(19, t_aqi);
pst.setString(20, t_fx);
pst.setString(21, t_fl);
pst.setString(22, t_type);
pst.setString(23, n_date);
pst.setString(24, n_high);
pst.setString(25, n_low);
pst.setString(26, n_aqi);
pst.setString(27, n_fx);
pst.setString(28, n_fl);
pst.setString(29, n_type);
pst.executeUpdate();
System.out.println("======天气入库成功======");
} catch (Exception e) {
e.printStackTrace();
System.out.println("======天气入库失败======");
}
} catch (Exception e) {
e.printStackTrace();
} finally{
if(db1 != null) {
try {
db1.close();//关闭连接
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
调用天气接口存入数据库---mysql
共20个文件
java:6个
class:6个
jar:4个
需积分: 5 29 下载量 164 浏览量
2018-01-10
17:11:03
上传
评论 4
收藏 1.83MB ZIP 举报
温馨提示
经过多方研究,总结出的简单明了的接口,通过调用接口获取天气预报,然后存入mysql数据库。研究有遗漏之后可以提出来,相互学习。
资源推荐
资源详情
资源评论
收起资源包目录
天气入库.zip (20个子文件)
天气入库
weathers
src
start
WeatherStart.java 165B
db
DBHelper.java 894B
util
SandTime.java 609B
DBTest.java 7KB
TestWeather.java 230B
WeatherUtil.java 1KB
bin
start
WeatherStart.class 445B
db
DBHelper.class 1KB
util
TestWeather.class 573B
WeatherUtil.class 2KB
SandTime.class 1KB
DBTest.class 6KB
.classpath 639B
.settings
org.eclipse.jdt.core.prefs 598B
.project 384B
jar包
jackson-annotations-2.2.3.jar 33KB
jackson-databind-2.2.3.jar 846KB
jackson-core-2.2.3.jar 188KB
mysql-connector-java-5.1.30.jar 932KB
项目结构及所需jar包.png 18KB
共 20 条
- 1
资源评论
六月大雪纷飞
- 粉丝: 7
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- C语言基础-C语言编程基础之Leetcode编程题解之第30题串联所有单词的子串.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第29题两数相除.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第28题找出字符串中第一个匹配项的下标.zip
- 实验报告模板(1).docx
- C语言基础-C语言编程基础之Leetcode编程题解之第26题删除有序数组中的重复项.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第25题K个一组翻转链表.zip
- hnu计算机系统作业-计算机系统基础课程大作业.zip
- 树莓派app.apk
- C++的基于同态加密技术的匿名电子投票系统源码.zip
- SW建模格式图.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功