【Java】 批处理

来源:互联网 发布:windows邮件服务器搭建 编辑:程序博客网 时间:2024/06/05 09:50

//用的是和风天气的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 ;
0 0
原创粉丝点击