/**
* IP处理类
*/
import java.sql.*; public class IPDeal {
/**
* 通过ip地址查询地区名称
* @param strip
* @return ip所在地区名称
*/
public static String getArea(String strip){
Connection conn = null;
PreparedStatement pstmt = null;
String sql;
String strRtn = null;
try{
MyJdbc myjdbc = new MyJdbc();
conn = myjdbc.getConn();
sql = "select * from fullip where startip<='" + strip + "' and endip>='" + strip + "'";
pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
strRtn = rs.getString("country");
}else{
strRtn = "未确定";
}
rs.close();
rs = null;
}catch(Exception e){
e.printStackTrace();
}finally{
if (pstmt != null)
try{
pstmt.close();
pstmt = null;
}catch(Exception e){}
if (conn != null)
try{
conn.close();
conn = null;
}catch(Exception e){}
}
return strRtn;
}
/**
* 把ip地址格式化为:000.000.000.000
* @param ip
* @return 返回规格ip
*/
public static String strfullip(String ip){
StringBuffer buff = new StringBuffer();
buff.append("");
String strzero = "000";
int ilen = 0;
if(ip != null){
String[] arrip = ip.split("\\.");
if(arrip.length == 4){
for(int i = 0; i < 4; i++){
if (i==0){
ilen = arrip[i].length();
if(ilen < 3){
buff.append(strzero.substring(0,3-ilen)).append(arrip[i]);
}else{
buff.append(arrip[i]);
}
}else{
ilen = arrip[i].length();
if(ilen < 3){
buff.append(".").append(strzero.substring(0,3-ilen)).append(arrip[i]);
}else{
buff.append(".").append(arrip[i]);
}
}
}
}
}
return buff.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
String strip = "202.108.33.32";
System.out.println(IPDeal.strfullip(strip));
System.out.println(System.currentTimeMillis());
System.out.println("ip" + strip + "所在地区:" + IPDeal.getArea(IPDeal.strfullip(strip)));
System.out.println(System.currentTimeMillis());
} }

最新文章

  1. Akka初步介绍
  2. jquery触屏幻灯片
  3. 针对APP的测试过程和重点关注内容,做以下梳理和总结
  4. ROS 新手教程 命令汇总
  5. 使用git建立远程仓库,让别人git clone下来
  6. NOIP2009普及组细胞分裂(数论)——yhx
  7. 解决SaveChanges会Hold住之前的错误的问题
  8. 【Anagrams】 cpp
  9. java Servlet接口及应用
  10. ASP.net MVC基础
  11. OpenCV2学习笔记01:Linux下OpenCV开发环境的搭建
  12. Oracle DB 备份和恢复的概念
  13. Vector/Arraylist与Linklist的区别
  14. JS笔记2 --定义对象
  15. jQuery停止事件冒泡
  16. R语言学习笔记之外部文件读取
  17. GeoServer初识与安装
  18. Java 多线程之线程池的使用
  19. 微信小程序,加载更多
  20. R语言-箱型图&amp;热力图

热门文章

  1. Socket与系统调用深层分析
  2. 10.hive安装
  3. #import &quot;msado15.dll&quot; no_namespace rename(&quot;EOF&quot;,&quot;adoEOF&quot;)
  4. 剑指offer3:从尾到头打印链表每个节点的值
  5. INPUT和CONSTRUCT指令——范例报表查询,作用让用户输入数据,自动生成SQL的WHERE条件,带开窗查询
  6. maraidb忘记数据密码
  7. List与Set区别
  8. ZROIDay3-比赛解题报告
  9. HTML5之动画优化(requestAnimationFrame)
  10. Golang新开发者要注意的陷阱和常见错误