【Java】 批处理
2016-02-03 18:18
387 查看
//用的是和风天气的API,先把JSON的数据存放到results数组
String dituUrl =" https://api.heweather.com/x3/condition?search=allcond&key=df5dbde555e54a788d1edd1c0ec09c1f";
String currentline = "";
String totalstring = "";
URL url = new URL(dituUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.connect();
InputStream urlStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
urlStream, "UTF-8"));
while ((currentline = reader.readLine()) != null) {
totalstring += currentline + "\r\n";
}
JSONObject json = JSON.parseObject(totalstring);
String code = json.getString("status");
long startTime = new Date().getTime();
if ("ok".equals(code)) {
JSONArray results = json.getJSONArray("cond_info");
//开始进入批处理的操作,将数据批处理存到t_b_weather_condition中
System.out.print(results.size());
PreparedStatement ps = null;
String sql = "insert into t_b_weather_condition(weather_code,weather_desc,weather_desc_en,weather_icon) values (?,?,?,?)";
Connection con = ConnectionFactory.getConnection();
ps = con.prepareStatement(sql);
for (int i=0;i<results.size();i++) {
JSONObject obj = results.getJSONObject(i);
ps.setInt(1, obj.getInteger("code"));
ps.setString(2, obj.getString("txt"));
ps.setString(3, obj.getString("txt_en"));
ps.setString(4, obj.getString("icon"));
ps.addBatch();
// System.out.println(results.getJSONObject(i));
}
ps.executeBatch();
ps.close();
con.close();
long endTime = new Date().getTime();
System.out.println(endTime - startTime + "秒");
}
return ;
String dituUrl =" https://api.heweather.com/x3/condition?search=allcond&key=df5dbde555e54a788d1edd1c0ec09c1f";
String currentline = "";
String totalstring = "";
URL url = new URL(dituUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.connect();
InputStream urlStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
urlStream, "UTF-8"));
while ((currentline = reader.readLine()) != null) {
totalstring += currentline + "\r\n";
}
JSONObject json = JSON.parseObject(totalstring);
String code = json.getString("status");
long startTime = new Date().getTime();
if ("ok".equals(code)) {
JSONArray results = json.getJSONArray("cond_info");
//开始进入批处理的操作,将数据批处理存到t_b_weather_condition中
System.out.print(results.size());
PreparedStatement ps = null;
String sql = "insert into t_b_weather_condition(weather_code,weather_desc,weather_desc_en,weather_icon) values (?,?,?,?)";
Connection con = ConnectionFactory.getConnection();
ps = con.prepareStatement(sql);
for (int i=0;i<results.size();i++) {
JSONObject obj = results.getJSONObject(i);
ps.setInt(1, obj.getInteger("code"));
ps.setString(2, obj.getString("txt"));
ps.setString(3, obj.getString("txt_en"));
ps.setString(4, obj.getString("icon"));
ps.addBatch();
// System.out.println(results.getJSONObject(i));
}
ps.executeBatch();
ps.close();
con.close();
long endTime = new Date().getTime();
System.out.println(endTime - startTime + "秒");
}
return ;
相关文章推荐
- Java图片压缩处理
- java32:I/O
- java线程池
- SpringMVC验证码生成,点击刷新及验证--基于kaptcha
- Struts2之ModelDriven用法
- 基于注解的mybatis和spring整合
- Java项目开发环境构建工具 Gradle 使用笔记(简单、基本)
- 使用LS2J技术在LotusScript中使用Java
- Java数据类型转换
- JVM源码分析之javaagent原理完全解读
- Java基础:转义字符
- JDK Timer & TimerTask
- jar包导致的错误之java.lang.NoClassDefFoundError: org.achartengine.renderer.XYMultipleSeriesRe
- Spring框架学习(二)
- springMVC js等文件找不到解决方法
- 深入解析Java的设计模式编程中的模板方法模式
- Spring-web源码解析之Filter-CharacterEncodingFilter
- maven使用命令行新建项目
- kafka集群搭建和使用Java写kafka生产者消费者
- eclipse svn2.0.0插件 手动安装方法