Java implementation of Baidu map address resolution and read stored procedures in the database

Source: Internet
Author: User

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";

/**
* Returns the latitude and longitude coordinates of the input address key LNG (longitude), lat (latitude)
*/
public static map<string, string> getlatitude (String address) {
try {
Convert address to Utf-8 16 binary
Address = Urlencoder.encode (address, "UTF-8");
If there is an agent, to set the proxy, no proxy can be commented
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= Guangzhou &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 ("1th renmin Middle Road, Yuexiu District");
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. Load Database Driver
Class.forName ("Oracle.jdbc.OracleDriver");
2. Create a database connection
String url = "Jdbc:oracle:thin: @localhost: 1520:ORCL";
Connection = drivermanager.getconnection (URL, "Scot", "Tiger");

3. Perform database operations
statement = Connection.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable);// Oracle database needs to be set for Updata
ResultSet rs = statement.executequery ("Select Djxh,dz,gps from T_location"), extra punctuation in//sql statement, error Invalid character
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 ();/Get latitude and longitude
System.out.println (dstring);
Rs.updatestring ("GPs", dstring);//Deposit Database
Rs.updaterow ();
System.out.println (rs.getnstring ("dz") + ":" +rs.getnstring ("GPs"));



}
Rs.close ();

} catch (Exception e) {
E.printstacktrace ();
} finally {
4. Close the connection
try {
if (statement! = NULL) {
Statement.close ();
}
if (connection! = null) {
Connection.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
}
}
}

}

Java implementation of Baidu map address resolution and read stored procedures in the database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.