標籤:public 資料庫 計數器 daima sheng
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;List<String>list = new ArrayList<String>();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<Shi> list = new ArrayList<Shi>();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(List<Shi>list){//將讀取的資料存放區到資料庫String sql = "insert into "+ insertTable+" values(?,?,?,?)";Shi shi = null;int size = list.size();try {for (int i = 0; i < size; 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();}}}
http://api.dangqian.com/apidiqu2/api.asp?id=420000000000
返回的資料如下
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讀取介面資料並儲存到資料庫