日期:2014-05-16  浏览次数:20702 次

ajax实现三级联动菜单

插入数据库语句


create table COUNTRY
(
? NAME VARCHAR2(100) not null,
? TYPE NUMBER not null
)
;
alter table COUNTRY
? add constraint PRIMARY_KEY primary key (TYPE);


insert into COUNTRY (NAME, TYPE)
values ('中国', 1);
insert into COUNTRY (NAME, TYPE)
values ('美国', 2);
insert into COUNTRY (NAME, TYPE)
values ('日本', 3);

?

?

?


create table CITY
(
? NAME??????? VARCHAR2(100) not null,
? TYPE??????? NUMBER not null,
? COUNTRYTYPE NUMBER not null
)
;


insert into CITY (NAME, TYPE, COUNTRYTYPE)
values ('沈阳', 1, 1);
insert into CITY (NAME, TYPE, COUNTRYTYPE)
values ('大连', 2, 1);
insert into CITY (NAME, TYPE, COUNTRYTYPE)
values ('american_city1', 3, 2);
insert into CITY (NAME, TYPE, COUNTRYTYPE)
values ('american_city2', 4, 2);
insert into CITY (NAME, TYPE, COUNTRYTYPE)
values ('american_city3', 5, 2);
insert into CITY (NAME, TYPE, COUNTRYTYPE)
values ('japan_city1', 6, 3);
insert into CITY (NAME, TYPE, COUNTRYTYPE)
values ('japan_city2', 7, 3);


?

?


create table STREET
(
? NAME???? VARCHAR2(100) not null,
? TYPE???? NUMBER not null,
? CITYTYPE NUMBER not null
)
;


insert into STREET (NAME, TYPE, CITYTYPE)
values ('中街', 1, 1);
insert into STREET (NAME, TYPE, CITYTYPE)
values ('太原街', 2, 1);
insert into STREET (NAME, TYPE, CITYTYPE)
values ('大连street1', 3, 2);
insert into STREET (NAME, TYPE, CITYTYPE)
values ('大连street2', 4, 2);
insert into STREET (NAME, TYPE, CITYTYPE)
values ('大连street3', 5, 2);

?

新建servlet 类两个

?

package test.servlet;

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SelectServlet extends HttpServlet {

?public void doGet(HttpServletRequest request, HttpServletResponse response)
???throws ServletException, IOException {

??response.setContentType("text/xml;charset=gb2312");
??response.setHeader("Cache-Control", "no-cache");
??String targetId = request.getParameter("id").toString();

??String xml_start = "<selects>";
??String xml_end = "</selects>";
??String xml = "";
??List list1 = new ArrayList();
??List list2 = new ArrayList();

???String sql = "select * from city where countrytype='" + targetId + "'";
???ConnDB53 db=new ConnDB53();
???ResultSet rs=db.executeQuery(sql);
???try{
????while (rs.next()) {
?????list1.add(rs.getString("name"));
?????list2.add(rs.getString("type"));
????}
???}catch(Exception e){
????e.printStackTrace();
???}
???
??

??if (targetId.equalsIgnoreCase("0")) {
???xml = "<select><value>0</value><text>--请选择--</text></select>";
??} else {
???xml = "<select><value>0</value><text>--请选择--</text></select>";
???for (int i = 0; i < list1.size(); i++) {
????xml += "<select><value>" + list2.get(i) + "</value><text>"
??????+ list1.get(i) + "</text></select>";
???}

??}
??String last_xml = "<?xml version='1.0' encoding='GB2312'?>" + xml_start
????+ xml + xml_end;
??response.getWriter().write(last_xml);

?}

?public void doPost(HttpServletRequest request, HttpServletResponse response)
???throws ServletException, IOException {

??doGet(request, response);
?}

}

?

package test.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.