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;
		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无关。请勿用于商业用途!

你可能感兴趣的:(数据库,public,计数器,daima,sheng)