java 数据库数据写接口_Java读取接口数据并保存到数据库
public?class?Test05GetData?{
//guo?sheng?shi?xian?zhen?cun的表結構完全一樣。數據全部讀取完畢之后再將數據整合到同一張表中
protected?static?String?parentTable?=?"sheng";//?父表
protected?static?String?insertTable?=?"shi";//?字表
protected?static?String?daima?=?"";//?代碼
protected?static?int?dataCount?=?0;//?計數器
protected?static?int?totalData?=?0;//?父表總記錄數
protected?static?Connection?connection?=?null;
protected?static?Statement?statement?=?null;
protected?static?ResultSet?resultSet?=?null;
public?static?void?main(String[]?args)?{
connection?=?DBConnection.getOracleConnection();
statement?=?null;
resultSet?=?null;
Listlist?=?new?ArrayList();
try?{
statement?=?connection.createStatement();
resultSet?=?statement.executeQuery("select?*?from?"?+?parentTable);
while?(resultSet.next())?{
totalData?++;
daima?=?resultSet.getString("daima");
list.add(daima);
}
System.out.println("tatalData:"?+?totalData);
}?catch?(Exception?e)?{
e.printStackTrace();
}
for?(Iterator?iterator?=?list.iterator();?iterator.hasNext();)?{
String?string?=?(String)?iterator.next();
dataCount?++;
double?x1?=?((double)dataCount/(double)totalData)*100.0?;
System.out.print("第"?+?dataCount?+?"個父項,");
System.out.println(String.format("%.2f",?x1)?+?"%");//顯示完成的百分比
query(string);
}
try?{
if?(resultSet?!=?null)?{
resultSet.close();
}
if?(statement?!=?null)?{
statement.close();
}
if(connection?!=?null){
connection.close();
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
public?static?void?query(String?daima)?{
String?url?=?"http://api.dangqian.com/apidiqu2/api.asp?id="?+?daima;
StringBuilder?stringBuilder?=?new?StringBuilder();
URL?oracle;
try?{
oracle?=?new?URL(url);
URLConnection?yc?=?oracle.openConnection();
BufferedReader?br?=?new?BufferedReader(new?InputStreamReader(yc.getInputStream()));
String?inputLine?=?null;
while?((inputLine?=?br.readLine())?!=?null)?{
stringBuilder.append(inputLine);
}
br.close();
String?str?=?stringBuilder.toString();
StringBuffer?stringBuffer?=?new?StringBuffer(str);
stringBuffer.delete(0,?9);
stringBuffer.deleteCharAt(stringBuffer.length()?-?1);
System.out.println(stringBuffer);
JSONObject?jsonobject?=?JSONObject.fromObject(stringBuffer.toString());
JSONObject?jsonobject1?=?(JSONObject)?jsonobject.get("list");
//?System.out.println(jsonobject1);
List?list?=?new?ArrayList();
for?(Iterator?iter?=?jsonobject1.keys();?iter.hasNext();)?{
String?key?=?(String)?iter.next();
Shi?shi?=?(Shi)?JSONObject.toBean(
(JSONObject)?jsonobject1.get(key),?Shi.class);
list.add(shi);
}
saveData(list);
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
protected?static?void?saveData(Listlist){//將讀取的數據存儲到數據庫
String?sql?=?"insert?into?"+?insertTable+"?values(?,?,?,?)";
Shi?shi?=?null;
int?size?=?list.size();
try?{
for?(int?i?=?0;?i?
shi?=?list.get(i);
sql?=?"insert?into?"+?insertTable+"?(zidi,diming,daima,dengji)values("+shi.getZidi()+",‘"+shi.getDiming()+"‘,‘"+shi.getDaima()+"‘,"+shi.getDengji()+")";
statement.execute(sql);
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
}
返回的數據如下callback({?"is_login":1,?"site":"中國行政區劃api",?"url":"http://api.dangqian.com/apidiqu2/",?"jieshao":"省市縣鄉村五級行政區劃地址信息庫",?"idengji":"2",?"idiming":"湖北",?"idaima":"420000000000",?"diming1":"中國",?"daima1":"000000000000",?"diming2":"湖北省",?"daima2":"420000000000",?"diming3":"",?"daima3":"",?"diming4":"",?"daima4":"",?"diming5":"",?"daima5":"",?"diming6":"",?"daima6":"",?"zidi":14,?"banben":"2012",?"zhizao":"王建然",?"qq":"250247045",?"riqi":"2013-4-21?16:08:25",?"shike":"2014-8-25?9:29:46",?"id":"17",?"list":{?"wjr1":{"dengji":"3","diming":"武漢市","daima":"420100000000","zidi":13},?"wjr2":{"dengji":"3","diming":"黃石市","daima":"420200000000","zidi":6},?"wjr3":{"dengji":"3","diming":"十堰市","daima":"420300000000","zidi":8},?"wjr4":{"dengji":"3","diming":"宜昌市","daima":"420500000000","zidi":13},?"wjr5":{"dengji":"3","diming":"襄陽市","daima":"420600000000","zidi":9},?"wjr6":{"dengji":"3","diming":"鄂州市","daima":"420700000000","zidi":3},?"wjr7":{"dengji":"3","diming":"荊門市","daima":"420800000000","zidi":5},?"wjr8":{"dengji":"3","diming":"孝感市","daima":"420900000000","zidi":7},?"wjr9":{"dengji":"3","diming":"荊州市","daima":"421000000000","zidi":8},?"wjr10":{"dengji":"3","diming":"黃岡市","daima":"421100000000","zidi":10},?"wjr11":{"dengji":"3","diming":"咸寧市","daima":"421200000000","zidi":6},?"wjr12":{"dengji":"3","diming":"隨州市","daima":"421300000000","zidi":3},?"wjr13":{"dengji":"3","diming":"恩施土家族苗族自治州","daima":"422800000000","zidi":8},?"wjr14":{"dengji":"3","diming":"省直轄縣級行政區劃","daima":"429000000000","zidi":4}?}?})
只需要解析出其中的list部分存儲到數據庫即可。
以上內容僅供技術分享,涉及到的任何法律問題與本人及51cto無關。請勿用于商業用途!
原文:http://shamrock.blog.51cto.com/2079212/1544270
總結
以上是生活随笔為你收集整理的java 数据库数据写接口_Java读取接口数据并保存到数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java实现分发_关于JAVA中事件分发
- 下一篇: java有向图邻接表入度_图的实现--邻