標籤:ext try hash acl 地址 需要 start 廣州市 ble
package action;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class LocationUtil {
private static final String ak = "XG8Ed4Vi6BN67wrmFLc7x63oYOayXIR1";
/**
* 返回輸入地址的經緯度座標 key lng(經度),lat(緯度)
*/
public static Map<String, String> getLatitude(String address) {
try {
// 將地址轉換成utf-8的16進位
address = URLEncoder.encode(address, "UTF-8");
// 如果有代理,要設定代理,沒代理可注釋
// System.setProperty("http.proxyHost","192.168.172.23");
// System.setProperty("http.proxyPort","3209");
URL resjson = new URL("http://api.map.baidu.com/geocoder/v2/?callback=renderOption&output=json&address="
+ address + "&city=廣州市&output=json&ak=" + ak);
// System.out.println(resjson);
BufferedReader in = new BufferedReader(new InputStreamReader(resjson.openStream()));
String res;
StringBuilder sb = new StringBuilder("");
while ((res = in.readLine()) != null) {
sb.append(res.trim());
}
in.close();
String str = sb.toString();
//System.out.println("return json:" + str);
if(str!=null&&!str.equals("")){
Map<String, String> map = null;
int lngStart = str.indexOf("\"lng\"");
int lngEnd = str.indexOf(",\"lat\"");
int latEnd = str.indexOf("},\"precise");
if (lngStart > 0 && lngEnd > 0 && latEnd > 0) {
String lng = str.substring(lngStart + 6, lngEnd);
String lat = str.substring(lngEnd + 7, latEnd);
map = new HashMap<String, String>();
map.put("lng", lng);
map.put("lat", lat);
return map;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String args[]) {
Map<String, String> map = LocationUtil.getLatitude("越秀區人民中路1號");
if (null != map) {
System.out.println(map.get("lng"));
System.out.println(map.get("lat"));
}
}
}
package action;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
public class JDBC {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
try {
//1、載入資料庫驅動
Class.forName("oracle.jdbc.OracleDriver");
//2、建立資料庫連接
String url = "jdbc:oracle:thin:@localhost:1520:orcl";
connection = DriverManager.getConnection(url, "scot", "tiger");
//3、進行資料庫操作
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);//Oracle資料庫需要設定才能進行updata
ResultSet rs = statement.executeQuery("select djxh,dz,gps from t_location");//sql語句中出現多餘的標點符號 ;, 會報錯無效字元
LocationUtil locationUtil=new LocationUtil();
while(rs.next()){
// System.out.println(rs.toString());
String a=rs.getNString("djxh");
String bString=rs.getNString("dz");
Map cString=locationUtil.getLatitude(bString);
String dString =cString .get("lng").toString()+","+cString.get("lat").toString();/得到經緯度
//System.out.println(dString);
rs.updateString("gps",dString);//存入資料庫
rs.updateRow();
System.out.println(rs.getNString("dz")+" : "+rs.getNString("gps"));
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
//4、關閉串連
try {
if(statement != null){
statement.close();
}
if(connection != null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
java 實現百度地圖的位址解析及在資料庫的讀取預存程序