您的位置:首页 > 编程语言 > Java开发

【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 ;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: