package untitled1;
import java.io.*;
import java.io.*;
import java.util.*;
import java.sql.*;
publicclassDatabaseOp {
//声明数据库jdbc驱动变量
staticStringjdbcClass ="oracle.jdbc.driver.OracleDriver";
//声明数据库url变量
staticStringjdbcURL ="jdbc:oracle:thin:@127.0.0.1:1521:databasename";//注意数据库名
staticStringuserName ="";//修改为你自己的用户密码
staticStringpassword ="";
//创建执行sql语句的对象变量
publicConnection conn;
Statement stmt;
publicDatabaseOp() {
}
publicStringgetcanton() {
StringRS =null;
inti = 0;
getCon(userName, password);
ResultSet ds =null;
try{
if(stmt !=null) {
StringSqlText ="select c.cantoncode,c.cantonnamefrom CHs_Canton c where c.pcantoncode is null order by c.cantonname";
ds = stmt.executeQuery(SqlText);
RS ="";
while (ds.next()) {
RS = RS +"
if(i == 0) {
RS = RS +" selected ";
}
RS = RS +">"+ ds.getString(2) +"
";i++;
}
RS = RS +"/r";
}
}
catch(Exceptione)
{
RS = e.toString() +"错误";
}
finally{
try{
stmt.close();
}
catch(Exceptione)
{
}
try{
conn.close();
}
catch(Exceptione)
{
}
}
returnRS;
}
publicStringgetcanton(StringpCantonCode) {
StringRS =null;
inti = 0;
ResultSet ds =null;
getCon(userName, password);
try{
if(stmt !=null) {
StringSqlText =
"select c.cantoncode,c.cantonnamefrom CHs_Canton c where c.pcantoncode='" +
pCantonCode + "' order by c.cantonname";
ds = stmt.executeQuery(SqlText);
RS ="";
while (ds.next()) {
RS = RS + ds.getString(1) +","+ ds.getString(2) +"$";//从1开始的
}
}
}
catch(Exceptione)
{
RS = e.toString() +"错误";
}
finally{
try{
stmt.close();
}
catch(Exceptione)
{
}
try{
conn.close();
}
catch(Exceptione)
{
}
}
returnRS;
}
publicboolean getCon(StringUserName,StringPassWord) {
try{
//加载数据库驱动程序
Class.forName(jdbcClass);
//建立数据库连接
conn = DriverManager.getConnection(jdbcURL, UserName, PassWord);
stmt = conn.createStatement();
returntrue;
}
catch(Exceptione) {
e.printStackTrace();
stmt =null;
returnfalse;
}
}
}
//by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.12.18
-------------------------jsp1.jsp代码-------------------------
jsp1
var Currobj=null;
//parentobj上级对象
function changecode(parentobj,obj)
{
if(obj==null)return;
obj.length = 0;
Currobj=obj;
var selectcode=parentobj.options[parentobj.selectedIndex].value;
//alert(selectcode);
obj.options[0] =newOption('====所有地区====','');
document.getElementById("datapro").innerHTML ="正在取数据,请稍后......";
send_request("jsp2.jsp?code="+selectcode);
}
var http_request =false;
//向服务器发起XMLHTTP请求。
function send_request(url)
{//初始化、指定处理函数、发送请求的函数
http_request =false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest)
{//Mozilla浏览器
http_request =newXMLHttpRequest();
if(http_request.overrideMimeType) {//设置MiME类别
http_request.overrideMimeType('text/xml');
}
}
elseif(window.ActiveXObject)
{// IE浏览器
try
{
http_request =newActiveXObject("Msxml2.XMLHTTP");
}catch(e)
{
try
{
http_request =newActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {}
}
}
if(!http_request)
{// 异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
returnfalse;
}
http_request.onreadystatechange = processRequest;
// 确定发送请求的方式和URL以及是否同步执行下段代码
http_request.open("GET", url,true);
http_request.send(null);
}
// 处理返回信息的函数
function processRequest()
{
if(http_request.readyState == 4)
{// 判断对象状态
if(http_request.status == 200)
{// 信息已经成功返回,开始处理信息
var returnStr = http_request.responseText;
if(returnStr.indexOf("Error")==-1)//正确
{
var list = returnStr.split('$');
var len =list.length;
var code;
var name;
var p;
var line;
var strlen;
for(var i=0; i
{
line=list[i];
line=line.replace(/(^[//s]*)|([//s]*$)/g,"");//删除空格
if(line!="")
{
if(line.length>0)
{
p=line.indexOf(",");
//alert(line+":line");
code=line.substr(0,p);
//alert(p+":p");
strlen=line.length;
//alert(strlen+":strlen");
name=line.substr(p+1,strlen-p);
//alert(name);
Currobj.options[Currobj.length]
=newOption(name,code);
}
}
}
document.getElementById("datapro").innerHTML ="取数据完成";
}
else
{
document.getElementById("datapro").innerHTML ="错误";
}
}else
{//页面不正常
alert("您所请求的页面有异常。");
}
}
}
省市
untitled1.DatabaseOp op=newuntitled1.DatabaseOp();
out.println(op.getcanton());
%>
市区
==所有地区==
县
==所有地区==
-------------------------jsp2.jsp代码,响应ajax,该界面不能放任何其他的如html,body等-------------------------
untitled1.DatabaseOp op=newuntitled1.DatabaseOp();
Stringpcode=request.getParameter("code");
out.println(op.getcanton(pcode));
%>
数据库结构:
create table CHs_Canton(Cantoncode varchar2(32),CantonName varchar2(64),pCantonCode varchar2(32),leveled integer);
数据内容类似:
110000,北京市,
110101,东城区,110000
110102,西城区,110000
110103,崇文区,110000
110104,宣武区,110000
110105,朝阳区,110000
110106,丰台区,110000
110107,石景山区,110000
110108,海淀区,110000
110109,门头沟区,110000
110111,房山区,110000
110112,通州区,110000
110113,顺义区,110000
110114,昌平区,110000
110115,大兴区,110000
110116,怀柔区,110000
110117,平谷区,110000
110200,县,110000
110228,密云县,110200
110229,延庆县,110200
120000,天津市,
120101,和平区,120000
120102,河东区,120000
120103,河西区,120000
120104,南开区,120000
120105,河北区,120000
120106,红桥区,120000
120107,塘沽区,120000
120108,汉沽区,120000
120109,大港区,120000
120110,东丽区,120000
120111,西青区,120000
120112,津南区,120000
120113,北辰区,120000
120114,武清区,120000
120115,宝坻区,120000