ajax,jsp,java技术实现行政区划代码三级关联下拉框

package untitled1;

 

import java.io.*;

 

import java.io.*;

import java.util.*;

import java.sql.*;

 

public class DatabaseOp {

    //声明数据库jdbc驱动变量

    static String jdbcClass = "oracle.jdbc.driver.OracleDriver";

 

    //声明数据库url变量

    static String jdbcURL = "jdbc:oracle:thin:@127.0.0.1:1521:databasename";//注意数据库名

    static String userName = ""; //修改为你自己的用户密码

    static String password = "";

 

    //创建执行sql语句的对象变量

    public Connection conn;

    Statement stmt;

 

    public DatabaseOp() {

 

    }

 

    public String getcanton() {

        String RS = null;

 

        int i = 0;

        getCon(userName, password);

 

        ResultSet ds = null;

        try {

            if (stmt != null) {

                String SqlText = "select c.cantoncode,c.cantonname  from CHs_Canton c where c.pcantoncode is null order by c.cantonname";

                ds = stmt.executeQuery(SqlText);

 

                RS = "";

 

                while (ds.next()) {

 

                    RS = RS + "<option value=/"" + ds.getString(1) + "/""; //1开始的

                    if (i == 0) {

                        RS = RS + " selected ";

                    }

 

                    RS = RS + ">" + ds.getString(2) + "</option>";

                    i++;

                }

 

                RS = RS + "/r";

            }

        }

 

        catch (Exception e)

 

        {

            RS = e.toString() + "错误";

        }

        finally {

            try {

                stmt.close();

            }

            catch (Exception e)

 

            {

 

            }

 

            try {

                conn.close();

            }

            catch (Exception e)

 

            {

 

            }

 

        }

 

        return RS;

 

    }

 

    public String getcanton(String pCantonCode) {

        String RS = null;

 

        int i = 0;

        ResultSet ds = null;

        getCon(userName, password);

        try {

            if (stmt != null) {

                String SqlText =

                "select c.cantoncode,c.cantonname  from 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 (Exception e)

 

        {

            RS = e.toString() + "错误";

        }

        finally {

            try {

                stmt.close();

            }

            catch (Exception e)

 

            {

 

            }

 

            try {

                conn.close();

            }

            catch (Exception e)

 

            {

 

            }

        }

        return RS;

 

    }

 

    public boolean getCon(String UserName, String PassWord) {

        try {

            //加载数据库驱动程序

            Class.forName(jdbcClass);

            //建立数据库连接

            conn = DriverManager.getConnection(jdbcURL, UserName, PassWord);

            stmt = conn.createStatement();

            return true;

        }

 

        catch (Exception e) {

            e.printStackTrace();

            stmt = null;

            return false;

        }

    }

 

}

//by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.12.18

-------------------------jsp1.jsp代码-------------------------

<%@ page contentType="text/html; charset=GB18030" %>

<html>

<head>

<title>

jsp1

</title>

<script language="javascript">

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] = new Option('====所有地区====','');

    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 = new XMLHttpRequest();

        if (http_request.overrideMimeType) {//设置MiME类别

            http_request.overrideMimeType('text/xml');

        }

    }

    else if (window.ActiveXObject)

    { // IE浏览器

        try

        {

            http_request = new ActiveXObject("Msxml2.XMLHTTP");

        } catch (e)

        {

            try

            {

                http_request = new ActiveXObject("Microsoft.XMLHTTP");

            }

            catch (e) {}

        }

    }

    if (!http_request)

    { // 异常,创建对象实例失败

        window.alert("不能创建XMLHttpRequest对象实例.");

        return false;

    }

    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<len; 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]

                            = new Option(name,code);

                        }

                    }

                }

                document.getElementById("datapro").innerHTML = "取数据完成";

            }

            else

            {

                document.getElementById("datapro").innerHTML = "错误";

 

            }

        } else

        { //页面不正常

            alert("您所请求的页面有异常。");

        }

    }

}

 

</script>

 

</head>

<body bgcolor="#ffffff">

 

<form action="jsp1.jsp" method="post" enctype="application/x-www-form-urlencoded" name="myform" target="_self">

省市

<select name="shengcode" onChange="changecode(shengcode,shicode)">

<%

untitled1.DatabaseOp op=new untitled1.DatabaseOp();

out.println(op.getcanton());

%>

</select>

市区

<select name="shicode" onChange="changecode(shicode,xiancode)">

<option selected value="">==所有地区==</option>

</select>

<select name="xiancode">

<option selected value="">==所有地区==</option>

</select>

<label id="datapro">&nbsp;</label>

</form>

</body>

</html>

 

-------------------------jsp2.jsp代码,响应ajax,该界面不能放任何其他的如html,body等-------------------------

<%@ page contentType="text/html; charset=GB18030" %>

<%

   untitled1.DatabaseOp op=new untitled1.DatabaseOp();

   String  pcode=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

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值