81,094
社区成员
发帖
与我相关
我的任务
分享
create database test
use test
create table mytest(
id int not null,
city varchar(50),
cityNo varchar(50)
)
insert into mytest(id,city,cityNo) values(1,'北京','010')
insert into mytest(id,city,cityNo) values(1,'上海','020')
insert into mytest(id,city,cityNo) values(1,'长沙','0731')
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import="test.connest,java.sql.*" %>
<html>
<head>
<title>
联级菜单(动态)
</title>
<script type="">
var xmlhttp;
//创建浏览器对象
function createHttp()
{
if(window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(widow.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
}
//参数页面
function staupdateroomtype()
{
var city=document.getElementById("city").value;
var url="";
url="Ajax.jsp?city="+city;
createHttp();
xmlhttp.onreadystatechange=StateDO;
xmlhttp.open("get",url,true);
xmlhttp.send(null);
}
//处理返回的结果
function StateDO()
{
if(xmlhttp.readystate==4)
{
if(xmlhttp.status==200)
{
document.getElementById("mycity").innerHTML=xmlhttp.responsetext;
}
}
}
</script>
</head>
<body bgcolor="#ffffff">
城市:<select id="city" name="city" onchange="staupdateroomtype();">
<option value="">请先选择城市</option>
<%
PreparedStatement ps = null;
ResultSet rs = null;
String city="";
Connection conn = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};database=test;server=(local)", "sa", "");
ps=conn.prepareStatement("select city from mytest");
rs=ps.executeQuery();
while(rs.next())
{
city=rs.getString("city");
%>
<option value="<%=city%>"><%=city%></option>
<%
}
}catch(Exception ex)
{
ex.printStackTrace();
}
%>
</select>
区号:
<span id="mycity">
<select name="cityno" id="cityno" style="width:80px;height:100px;">
<option value="">请先选择城市</option>
</select>
</span>
</body>
</html>
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="test.connest,java.sql.*" %>
<html>
<head>
<title>
Ajax
</title>
</head>
<body bgcolor="#ffffff">
<select name="cityno" id="cityno" style="width:80px;height:100px;">
<%
String city=new String(request.getParameter("city").getBytes("ISO8859_1"),"GB2312");
PreparedStatement ps = null;
ResultSet rs = null;
String cityNo="";
Connection conn = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};database=test;server=(local)", "sa", "");
ps=conn.prepareStatement("select cityNo from mytest where city='"+city+"'");
System.out.println("select cityNo from mytest where city='"+city+"'");
rs=ps.executeQuery();
while(rs.next())
{
cityNo=rs.getString("cityNo");
%>
<option value="<%=cityNo%>"><%=cityNo%></option>
<%
}
}catch(Exception ex)
{
ex.printStackTrace();
}
%>
</select>
</body>
</html>