任务:有1000多条门店信息(放在excel中,包括地址,店名,电话等,但是没有经纬度坐标),老大让我用地址通过百度地图拾取坐标系统找到相应的坐标,然后加上坐标后更新到公司的数据库。
失败的方案:1、使用按键精灵,按键精灵是一个模仿键盘鼠标操作的软件,用来写动作脚本的,由于时间紧,没怎么研究,因为整套动作太复杂了按键精灵尝试了下不行就放弃了。
2、表单填充工具(就是把exel表格批量提交到网页),什么风越、乌溜漆(特别是这乌溜漆,还要钱,坑货)都尝试了下,结果都不满意。因为我要把excel中的内容提交到网页还要从网页获得相应的内容,所以这些用于批量注册的软件用不上。
解决方案:最后还是干起了我本行---写代码,把问题解决了。思路是:通过传入地址作为参数拼接url调用百度地图,然后解析返回的页面,提取经纬度坐标。
以下为具体步骤
1、修改excel表中的属性名(方便后面用查询读取)然后倒入到数据库。
2、代码实现
实体类
- public class ShopInfo {
- private String name;
- private String scope;
- private String address;
- private String mobile;//手机
- private String phone;//座机
- private String description;
- private String lat;//经度
- private String lng;//纬度
- public ShopInfo() {
- }
- //....get和set方法
关键代码 模拟http和解析返回的数据:
- /*
- * 管理数据库连接的类
- */
- public class DbManager{
- private Connection con = null ;
- private Statement sta = null ;
- private ResultSet rs = null ;
- private PreparedStatement ps = null ;
- private Connection cons = null ;
- private Statement stas = null ;
- private ResultSet rss = null ;
- private PreparedStatement pss = null ;
- //连接本地mysql参数 ?后面的参数是解决中文乱码的
- private String MYSQLDRIVER="com.mysql.jdbc.Driver" ;
- private String CONTENT="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8";
- private String UN="***";
- private String UP="****";
- //连接服务器mysql参数
- private String MYSQLDRIVER1="com.mysql.jdbc.Driver" ;
- private String CONTENT1="jdbc:mysql://***********:3306/test?useUnicode=true&characterEncoding=utf8";
- private String UN1="*******";
- private String UP1="****";
- public DbManager()
- {
- try {
- Class.forName(MYSQLDRIVER);
- System.out.println("加载MySQL驱动...");
- con = DriverManager.getConnection(CONTENT,UN,UP);
- sta = con.createStatement();
- System.out.println("连接本地数据库成功!!");
- Class.forName(MYSQLDRIVER1);
- System.out.println("加载MySQL驱动...");
- cons = DriverManager.getConnection(CONTENT1,UN1,UP1);
- stas = cons.createStatement();
- System.out.println("连接服务器成功!!");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public ArrayList<ShopInfo> getAll(String tablename) throws SQLException{
- ArrayList<ShopInfo> allShops=new ArrayList();
- ShopInfo si;
- String sql="select * from "+tablename;
- System.out.println(sql);
- rs=sta.executeQuery(sql);
- while(rs.next()){
- si=new ShopInfo();
- si.setAddress(rs.getString("address"));
- si.setDescription(rs.getString("names")+"欢迎您的光临");
- si.setMobile(rs.getString("keeperphone"));
- si.setScope(tablename);
- si.setPhone(rs.getString("shoptel"));
- getPoint(si);
- allShops.add(si);
- System.out.println("经度:"+si.getLat()+" 纬度:"+si.getLng());
- }
- return allShops;
- }
- //-------------------------》关键代码根据地址获得坐标《--------------------------------
- public void getPoint(ShopInfo shop){
- try {
- String sCurrentLine;
- String sTotalString;
- sCurrentLine = "";
- sTotalString = "";
- java.io.InputStream l_urlStream;
- java.net.URL l_url = new java.net.URL("http://api.map.baidu.com/geocoder/v2/?address="+shop.getAddress().replaceAll(" ", "")+"&output=json&ak=702632E1add3d4953d0f105f27c294b9&callback=showLocation");
- java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
- l_connection.connect();
- l_urlStream = l_connection.getInputStream();
- java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_urlStream));
- String str=l_reader.readLine();
- //用经度分割返回的网页代码
- String s=","+"\""+"lat"+"\""+":";
- String strs[]=str.split(s, 2);
- String s1="\""+"lng"+"\""+":";
- String a[]=strs[0].split(s1, 2);
- shop.setLng(a[1]);
- s1="}"+","+"\"";
- String a1[]=strs[1].split(s1, 2);
- shop.setLat(a1[0]);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //存入数据库
- public void inputAll(ArrayList<ShopInfo> allShops){
- System.out.println("开始向服务器中写入");
- String sql2="insert into test.dc_shop (name,scope,address,phone,description,image,createtime,lat,lng) values (?,?,?,?,?,?,?,?,?)";
- try {
- pss=cons.prepareStatement(sql2);
- System.out.println("-------------------------等待写入数据条数: "+allShops.size());
- for(int i=0;i<allShops.size();i++){
- pss.setString(1,allShops.get(i).getName());
- pss.setString(2, allShops.get(i).getScope());
- pss.setString(3, allShops.get(i).getAddress());
- pss.setString(4, allShops.get(i).getPhone());
- pss.setString(5, allShops.get(i).getDescription());
- pss.setString(6, null);//图片路径
- pss.setString(7, allShops.get(i).getMobile());
- pss.setString(8, allShops.get(i).getLat());
- pss.setString(9, allShops.get(i).getLng());
- pss.executeUpdate();
- }
- pss.close();
- cons.close();
- System.out.println("--->OK");
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- System.out.println("向mysql中更新数据时发生异常!");
- e.printStackTrace();
- }
- }