1. java IP查詢方法
Java編程查詢IP地址歸屬地,可以調用淘寶提供的service查詢,並且解析http請求返回的json串,代碼如下:
packagegetAddressByIp;
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.HttpURLConnection;
importjava.net.MalformedURLException;
importjava.net.URL;
importnet.sf.json.JSONObject;
publicclassGetAddressByIp
{
/**
*
*@paramIP
*@return
*/
(StringIP){
Stringresout="";
try{
Stringstr=getJsonContent("http://ip.taobao.com/service/getIpInfo.php?ip="+IP);
System.out.println(str);
JSONObjectobj=JSONObject.fromObject(str);
JSONObjectobj2=(JSONObject)obj.get("data");
Stringcode=(String)obj.get("code");
if(code.equals("0")){
resout=obj2.get("country")+"--"+obj2.get("area")+"--"+obj2.get("city")+"--"+obj2.get("isp");
}else{
resout="IP地址有誤";
}
}catch(Exceptione){
e.printStackTrace();
resout="獲取IP地址異常:"+e.getMessage();
}
returnresout;
}
(StringurlStr)
{
try
{//獲取HttpURLConnection連接對象
URLurl=newURL(urlStr);
HttpURLConnectionhttpConn=(HttpURLConnection)url.openConnection();
//設置連接屬性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
//獲取相應碼
intrespCode=httpConn.getResponseCode();
if(respCode==200)
{
returnConvertStream2Json(httpConn.getInputStream());
}
}
catch(MalformedURLExceptione)
{
e.printStackTrace();
}
catch(IOExceptione)
{
e.printStackTrace();
}
return"";
}
(InputStreaminputStream)
{
StringjsonStr="";
//ByteArrayOutputStream相當於內存輸出流
ByteArrayOutputStreamout=newByteArrayOutputStream();
byte[]buffer=newbyte[1024];
intlen=0;
//將輸入流轉移到內存輸出流中
try
{
while((len=inputStream.read(buffer,0,buffer.length))!=-1)
{
out.write(buffer,0,len);
}
//將內存流轉換為字元串
jsonStr=newString(out.toByteArray());
}
catch(IOExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnjsonStr;
}
}
2. Java中查詢最大id的方法如何定義
方法如下:
@Override
@Transactional(propagation=Propagation.REQUIRED)
public long findBusinessIdMAX() throws WuliuguanliException {
Session session=null;
session=factory.getCurrentSession();
String hql="select max(b.id) from Business b";
Query query=session.createQuery(hql);
Long count=(Long) query.uniqueResult();
return count;
}
3. JAVA中如何快速查詢你想要找的方法
你可以通過eclipse中的查詢功能呀,
操作步驟 菜單欄上的
search - > File - > java search - > 下邊有個radio button選中method
然後搜索就可以了
我經常是這樣搜的,感覺比用api文檔還要方便