第一张 数据库结构:
第二张 省级数据:
这个数据是在 http://www.weather.com.cn/data/city3jdata/china.html 这里获取到的。
我是手动添加的当然你也可以用后面我给的小程序来添加,现在可以用这段sql脚本来添加
insert into city values (10101, '北京',0);
insert into city values (10102, '上海',0);
insert into city values (10103, '天津',0);
insert into city values (10104, '重庆',0);
insert into city values (10105, '黑龙江',0);
insert into city values (10106, '吉林',0);
insert into city values (10107, '辽宁',0);
insert into city values (10108, '内蒙古',0);
insert into city values (10109, '河北',0);
insert into city values (10110, '山西',0);
insert into city values (10111, '陕西',0);
insert into city values (10112, '山东',0);
insert into city values (10113, '新疆',0);
insert into city values (10114,'西藏',0);
insert into city values (10115,'青海',0);
insert into city values (10116,'甘肃',0);
insert into city values (10117,'宁夏',0);
insert into city values (10118,'河南',0);
insert into city values (10119,'江苏',0);
insert into city values (10120,'湖北',0);
insert into city values (10121,'浙江',0);
insert into city values (10122,'安徽',0);
insert into city values (10123,'福建',0);
insert into city values (10124,'江西',0);
insert into city values (10125,'湖南',0);
insert into city values (10126,'贵州',0);
insert into city values (10127,'四川',0);
insert into city values (10128,'广东',0);
insert into city values (10129,'云南',0);
insert into city values (10130,'广西',0);
insert into city values (10131,'海南',0);
insert into city values (10132,'香港',0);
insert into city values (10133,'澳门',0);
insert into city values (10134,'台湾',0);
第三张 读取城市 然后添加:
读取的方法 是根据这篇文章小改了一下
http://blog.csdn.net/windlee09/article/details/22742625
public static String readContentFromGet(String getURL) throws IOException {
// 拼凑get请求的URL字串,使用URLEncoder.encode对特殊和不可见字符进行编码
//String getURL = GET_URL;
URL getUrl = new URL(getURL);
// 根据拼凑的URL,打开连接,URL.openConnection()函数会根据 URL的类型,返回不同的URLConnection子类的对象,在这里我们的URL是一个http,因此它实际上返回的是HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
//connection.setRequestProperty("Charset", "utf-8");
// 建立与服务器的连接,并未发送数据
connection.connect();
// 发送数据到服务器并使用Reader读取返回的数据
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
System.out.println(" 读取文档!开始 ");
String str=reader.readLine();
reader.close();
// 断开连接
connection.disconnect();
System.out.println(" 读取文档!结束");
return str;
}
这个方法出去的就是String 类型的 json格式数据,得到这个数据然后进行解析
第五张 获取到省份:
这里就不介绍如何连接SQLliet
public static List<city> getCityList() throws SQLException, ClassNotFoundException{
Connection cn = null;
Statement stmt = null;
try {
cn=SqliteTest.getStmt();
stmt=cn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id FROM city where belong='0'");
while (rs.next()) {
list.add(new city(rs.getString("id")));
}
}finally{
SqliteTest.close(stmt, cn);
System.out.println("结束!~~~");
}
return list;
}
第六张 根据身份查找城市 一次性添加:
public static boolean add(List<city> list) throws SQLException{
Connection cn = null;
Statement stmt = null;
boolean bo=false;
JSONObject jo=null;
for (city city : list) {
try{
jo=new JSONObject(getDate.readContentFromGet(Get_s+city.getId()+".html"));
cn=SqliteTest.getStmt();
stmt=cn.createStatement();
String key=jo.isNull("00")?"01":"00";
int i=1;
while (!jo.isNull(key)) {
//System.out.println(jo.get(key));
String sql="INSERT INTO city(id,name,belong) VALUES('"+city.getId()+key+"','"+jo.get(key)+"','"+city.getId()+"')";
stmt.execute(sql);
System.out.println("增加:"+jo.get(key));
i++;
if(i>=10){
key=""+i;
}else{
key="0"+i;
}
bo=true;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} catch (JSONException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
SqliteTest.close(stmt, cn);
System.out.println("结束!~~~");
}
}
return bo;
}
这里解释一下几个东西,看得懂的就当废话。。
1.这里要city.getid +key的原因是 最后查询天气 格式是 省份id+城市id 如果有县级就再加县级id
String sql="INSERT INTO city(id,name,belong) VALUES('"+city.getId()+key+"','"+jo.get(key)+"','"+city.getId()+"')";
2.如果是空就返回TRUE 一直以为是非空返回TRUE
jo.isNull(key)
3.这段是返回的数据格式 如果是10一下他就是:{00,北京} 10以上他是{11} 所以加了这么个判断
if(i>=10){
key=""+i;
}else{
key="0"+i;
}
4.这段呢是因为国家气象局返回的城市 是直辖市例如:北京开头是{00,北京} 但是非直辖市 是{01,宁波} ;所以加了这么个判断
String key=jo.isNull("00")?"01":"00";