Home > Back-end >  I dynamically from the API access to data in the database, Java applications can and servlet is not,
I dynamically from the API access to data in the database, Java applications can and servlet is not,

Time:09-23

Package APIInfoSave;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

The import javax.mail. Servlet. ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

The import org.apache.com mons. Dbutils. QueryRunner;
The import org.apache.com mons. Dbutils. Handlers. BeanHandler;
The import org.apache.com mons. Dbutils. Handlers. BeanListHandler;

The import com. Alibaba. Fastjson. JSON;
The import com. Alibaba. Fastjson. JSONObject;

The import APIInfo. APIInfo;
The import DataConnection. Databaseconnection;
The import Table. CityTable;



Public class APINow extends the HttpServlet {

Public void doGet (it request, HttpServletResponse response) throws ServletException, IOException {

//connect
QueryRunner runner=new QueryRunner (Databaseconnection getDataSource ());
//enter SQL statements
String SQL="select * from city";
//collection
List CityTableList=null;
Try {
//to accept data
CityTableList=runner. Query (SQL, new BeanListHandler (CityTable. Class));
} the catch (SQLException e) {
//TODO Auto - generated the catch block,
e.printStackTrace();
}

//
If (CityTableList!=null) {

Int Registertable=0;
int num=0;

For (CityTable CityTable: CityTableList) {
num++;
//for APIinfo class information string
String info=APIinfo. GetAPIinfo (citytable getCityID ());
//json,
JSONObject object=JSON. ParseObject (info);

System. The out. Println (object. The get (" now "));

//get in now hum to humidity (relative humidity) and translated into String
String humidity=(String) ((JSONObject) object. The get (" now ")), get (" hum ");

//get now in vis (visibility) and translated into a String to the visibility
String visibility=(String) ((JSONObject) object. The get (" now ")), get (" vis ");

//get in now pres (atmospheric pressure) and translated into a String to the airpressure
String airpressure=(String) ((JSONObject) object. The get (" now ")), get (" pres ");

//get in now PCPN (rainfall) and translated into a String to the precipitation
String precipitation=(String) ((JSONObject) object. The get (" now ")), get (" PCPN ");

//for now in fl (body feeling temperature, the default units: degrees Celsius) and translated into a String to bodytemperature
String bodytemperature=(String) ((JSONObject) object. The get (" now ")), get (" fl ");

//get in now wind_sc (wind) and translated into a String to the windpower
String windpower=(String) ((JSONObject) object. The get (" now ")), get (" wind_sc ");

//get in now wind_dir (wind) and translated into a String to winddirection
String winddirection=(String) ((JSONObject) object. The get (" now ")), get (" wind_dir ");

//get in now wind_spd (wind speed) and translated into a String to the windspeed
String windspeed=(String) ((JSONObject) object. The get (" now ")), get (" wind_spd ");

//for now in the cloud (cloud cover) and translated into a String to cloudcover
//String cloudcover=(String) ((JSONObject) object. The get (" now ")), get (" cloud ");

//get in now wind_deg (wind 360 Angle) and translated into a String to windangle
String windangle=(String) ((JSONObject) object. The get (" now ")), get (" wind_deg ");

//for now in the TMP (temperature, the default units: degrees Celsius) and translated into a String to temperature
String temperature=(String) ((JSONObject) object. The get (" now ")), get (" TMP ");

//get in now cond_txt (a description of the actual weather conditions) and translated into a String to weatherlive
String weatherlive=(String) ((JSONObject) object. The get (" now ")), get (" cond_txt ");

//get in now cond_code live weather conditions (code) and translated into a String to weathercode
String weathercode=(String) ((JSONObject) object. The get (" now ")), get (" cond_code ");

//System. Out. Println (citytable getCityID ());

System. The out. Println (object. The get (" now "));
//String nowinfo=(String) object. The get (" now ");
//System. Out. Println (nowinfo);
//System. Out. Println (info).
String weathersql="insert into now1 (cid, cond_code cond_txt, fl, hum, PCPN, pres, TMP, vis, wind_deg, wind_dir, wind_sc, wind_spd) values (?,?,?,?,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,?) ";
Try {
Registertable=runner. Update (weathersql, citytable getCityID (), weathercode, weatherlive, bodytemperature, humidity, precipitation, and airpressure, temperature, visibility, windangle, winddirection, windpower, windspeed);
} the catch (SQLException e) {
e.printStackTrace();
}

//System. Out. Println (citytable getCityID ());
}
System. The out. Println (" success ");
}
The else {
System. The out. Println (" kong ");
}

}

Public void doPost (it request, HttpServletResponse response) throws ServletException, IOException {
DoGet (request, response);
}
}





.
.
.
{" hum ", "18", "vis" : "16" and "pres" : "854", "PCPN" : "0.0", "fl" : "17", "wind_sc" : "3", "wind_dir" : "west wind", "wind_spd" : "13", "cloud" : "0" and "wind_deg" : "260", "TMP" : "20", "cond_txt" : "clear", "cond_code" : "100"}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related