IP地址查询代码的方法
IP地址查询代码的方法
IP地址查询的实现方法可能和大家自己的想法不是一样的。大家可以看看一般的实现方法。
ipQueary.jsp
<%@page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %>
<jsp:useBean id="user" scope="page" class="user.db" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>IP查询</title>
</head>
<LINK href="css/style.css" _fcksavedurl=""css/style.css"" _fcksavedurl=""css/style.css"" rel=stylesheet type="text/css">
<body>
<div align="center"><font color="#0000FF" size="2"><strong>IP查询结果</strong></font></div>
<br>
<%
Connection con = user.getConn() ;
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql;
ResultSet rs;
if( request.getParameter("ip1") != null){
String number1 = request.getParameter("ip1").trim();
String number2 = request.getParameter("ip2").trim();
String number3 = request.getParameter("ip3").trim();
String number4 = request.getParameter("ip4").trim();
String number = number1+"."+number2+"."+number3+"."+number4;
sql="select * from ipaddr where beginaddr<='"+number+"' and endaddr>='"+number+"' and left(beginaddr,3)='"+number1+"' and left(endaddr,3)='"+number1+"' and substring(beginaddr,5,3)='"+number2+"' and '"+number2+"'=substring(endaddr,5,3) and substring(beginaddr,9,3)='"+number3+"' and '"+number3+"'=substring(endaddr,9,3)";
rs=stmt.executeQuery(sql);
if(!rs.next()){
sql="select * from ipaddr where beginaddr<='"+number+"' and endaddr>='"+number+"' and left(beginaddr,3)='"+number1+"' and left(endaddr,3)='"+number1+"' and substring(beginaddr,5,3)='"+number2+"' and '"+number2+"'=substring(endaddr,5,3)";
rs=stmt.executeQuery(sql);
if(!rs.next()){
sql="select * from ipaddr where beginaddr<='"+number+"' and endaddr>='"+number+"' and left(beginaddr,3)='"+number1+"' and left(endaddr,3)='"+number1+"'";
rs=stmt.executeQuery(sql);
if(!rs.next()){
sql="select * from ipaddr where beginaddr<='"+number+"' and endaddr>='"+number+"'";
rs=stmt.executeQuery(sql);
}
}
}
if(rs.next()){
%>
</div>
<table border=1 width=500 align="center" >
<tr>
<td width=100% colspan=2 align=middle height=22 class=btn>搜索结果:</td>
</tr>
<tr>
<td width=28% align=middle height=22 class=btn>你要搜索的IP地址:</td>
<td width=72% align=middle height=22 class=btn><%=number%></td>
</tr>
<tr>
<td width=28% align=middle height=22 class=btn>查询结果:</td>
<td width=72% align=middle height=22 class=btn><%=rs.getString("country")%><font color="#FF0000">:</font><%=rs.getString("city")%><font color="#FF0000">:</font><%=rs.getString("detail")%></td>
</tr>
</table>
<%
}else
{
%>
<table border=1 cellpadding=4 width=500 align=center>
<tr>
<td width='100%' colspan=2 align=middle height=22 class=btn>搜索结果:</td>
</tr>
<tr>
<td width=28% align=middle height=22 bgcolor=#eff1f3 class=btn>你要搜索的IP地址:</td>
<td width=72% align=middle height=22 bgcolor=#eff1f3 class=btn><%=number%></td>
</tr>
<tr>
<td width=28% align=middle height=22 bgcolor=#eff1f3 class=btn>查询结果:</td>
<td width=72% align=middle height=22 bgcolor=#eff1f3 class=btn>没有找到!!</td>
</tr>
</table>
<%
}
}%>
</html>