使用jquery的ajax方式从服务器加载json数据至客户端即可以实现省市级联的效果。
具体如下:
1)、页面初始化时,首先加载所有的省;
2)、选择省,ajax传送编码至服务器,服务器根据选中的省编码,返回省下所有的市。
这样就能动态实现了省市级联的效果了。
--省级 provincial
create table provincial
(
provincialID int,
provincialName varchar(50),
primary key (provincialID)
)engine=INNODB default charset=gbk;
insert into provincial values(1,'北京市');
insert into provincial values(2,'天津市');
insert into provincial values(3,'上海市');
insert into provincial values(4,'重庆市');
insert into provincial values(5,'河北省');
insert into provincial values(6,'山西省');
insert into provincial values(7,'台湾省');
insert into provincial values(8,'辽宁省');
insert into provincial values(9,'吉林省');
insert into provincial values(10,'黑龙江省');
insert into provincial values(11,'江苏省');
insert into provincial values(12,'浙江省');
insert into provincial values(13,'安徽省');
insert into provincial values(14,'福建省');
insert into provincial values(15,'江西省');
insert into provincial values(16,'山东省');
insert into provincial values(17,'河南省');
insert into provincial values(18,'湖北省');
insert into provincial values(19,'湖南省');
insert into provincial values(20,'广东省');
insert into provincial values(21,'甘肃省');
insert into provincial values(22,'四川省');
insert into provincial values(23,'贵州省');
insert into provincial values(24,'海南省');
insert into provincial values(25,'云南省');
insert into provincial values(26,'青海省');
insert into provincial values(27,'陕西省');
insert into provincial values(28,'广西壮族自治区');
insert into provincial values(29,'西藏自治区');
insert into provincial values(30,'宁夏回族自治区');
insert into provincial values(31,'新疆维吾尔自治区');
insert into provincial values(32,'内蒙古自治区');
insert into provincial values(33,'澳门特别行政区');
insert into provincial values(34,'香港特别行政区');
select provincialID,provincialName from provincial;
--城市 city
create table city
(
cityID int not null,
cityName varchar(50) not null,
provincialID int not null,
primary key (cityID)
)engine=INNODB default charset=gbk; alter table city add constraint FK_pro_city_provincialID foreign key (provincialID)
references provincial (provincialID);
----------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------
--插入各个省的城市数据
--4个直辖市
insert into city values(1,'北京市',1);
insert into city values(2,'天津市',2);
insert into city values(3,'上海市',3);
insert into city values(4,'重庆市',4);
--select from city where provincialID=4
----------------------------------------------------------------
--5河北省 11个地级市
insert into city values(5,'石家庄市',5);
insert into city values(6,'唐山市',5);
insert into city values(7,'秦皇岛市',5);
insert into city values(8,'邯郸市',5);
insert into city values(9,'邢台市',5);
insert into city values(10,'保定市',5);
insert into city values(11,'张家口市',5);
insert into city values(12,'承德市',5);
insert into city values(13,'沧州市',5);
insert into city values(14,'廊坊市',5);
insert into city values(15,'衡水市',5);
--select from city where provincialID=5 order by cityID
----------------------------------------------------------------
--6山西省 11个城市
insert into city values(16,'太原市',6);
insert into city values(17,'大同市',6);
insert into city values(18,'阳泉市',6);
insert into city values(19,'长治市',6);
insert into city values(20,'晋城市',6);
insert into city values(21,'朔州市',6);
insert into city values(22,'晋中市',6);
insert into city values(23,'运城市',6);
insert into city values(24,'忻州市',6);
insert into city values(25,'临汾市',6);
insert into city values(26,'吕梁市',6);
--select from city where provincialID=6 order by cityID
----------------------------------------------------------------
--7台湾省(台湾本岛和澎湖共设7市、16县,其中台北市和高雄市为“院辖市”,直属“行政院”,其余属台湾省;市下设区,县下设市(县辖市)、镇、乡,合称区市镇乡。);
insert into city values(27,'台北市',7);
insert into city values(28,'高雄市',7);
insert into city values(29,'基隆市',7);
insert into city values(30,'台中市',7);
insert into city values(31,'台南市',7);
insert into city values(32,'新竹市',7);
insert into city values(33,'嘉义市',7);
insert into city values(34,'台北县',7);
insert into city values(35,'宜兰县',7);
insert into city values(36,'桃园县',7);
insert into city values(37,'新竹县',7);
insert into city values(38,'苗栗县',7);
insert into city values(39,'台中县',7);
insert into city values(40,'彰化县',7);
insert into city values(41,'南投县',7);
insert into city values(42,'云林县',7);
insert into city values(43,'嘉义县',7);
insert into city values(44,'台南县',7);
insert into city values(45,'高雄县',7);
insert into city values(46,'屏东县',7);
insert into city values(47,'澎湖县',7);
insert into city values(48,'台东县',7);
insert into city values(49,'花莲县',7);
--select from city where provincialID=7 order by cityID
----------------------------------------------------------------
--8辽宁省 14个地级市
insert into city values(50,'沈阳市',8);
insert into city values(51,'大连市',8);
insert into city values(52,'鞍山市',8);
insert into city values(53,'抚顺市',8);
insert into city values(54,'本溪市',8);
insert into city values(55,'丹东市',8);
insert into city values(56,'锦州市',8);
insert into city values(57,'营口市',8);
insert into city values(58,'阜新市',8);
insert into city values(59,'辽阳市',8);
insert into city values(60,'盘锦市',8);
insert into city values(61,'铁岭市',8);
insert into city values(62,'朝阳市',8);
insert into city values(63,'葫芦岛市',8);
--select from city where provincialID=8 order by cityID
----------------------------------------------------------------
--9吉林省(2006年,辖:8个地级市、1个自治州;20个市辖区、20个县级市、17个县、3个自治县。);
insert into city values(64,'长春市',9);
insert into city values(65,'吉林市',9);
insert into city values(66,'四平市',9);
insert into city values(67,'辽源市',9);
insert into city values(68,'通化市',9);
insert into city values(69,'白山市',9);
insert into city values(70,'松原市',9);
insert into city values(71,'白城市',9);
insert into city values(72,'延边朝鲜族自治州',9);
--select from city where provincialID=9 order by cityID
----------------------------------------------------------------
--10黑龙江省(2006年,辖:12地级市、1地区;64市辖区、18县级市、45县、1自治县);
insert into city values(73,'哈尔滨市',10);
insert into city values(74,'齐齐哈尔市',10);
insert into city values(75,'鹤 岗 市',10);
insert into city values(76,'双鸭山市',10);
insert into city values(77,'鸡 西 市',10);
insert into city values(78,'大 庆 市',10);
insert into city values(79,'伊 春 市',10);
insert into city values(80,'牡丹江市',10);
insert into city values(81,'佳木斯市',10);
insert into city values(82,'七台河市',10);
insert into city values(83,'黑 河 市',10);
insert into city values(84,'绥 化 市',10);
insert into city values(85,'大兴安岭地区',10);
--select from city where provincialID=10 order by cityID
----------------------------------------------------------------
--11江苏省(2005年辖:13个地级市;54个市辖区、27个县级市、25个县);
insert into city values(86,'南京市',11);
insert into city values(87,'无锡市',11);
insert into city values(88,'徐州市',11);
insert into city values(89,'常州市',11);
insert into city values(90,'苏州市',11);
insert into city values(91,'南通市',11);
insert into city values(92,'连云港市',11);
insert into city values(93,'淮安市',11);
insert into city values(94,'盐城市',11);
insert into city values(95,'扬州市',11);
insert into city values(96,'镇江市',11);
insert into city values(97,'泰州市',11);
insert into city values(98,'宿迁市',11);
--select from city where provincialID=11 order by cityID
----------------------------------------------------------------
--12浙江省(2006年,辖:11个地级市;32个市辖区、22个县级市、35个县、1个自治县。);
insert into city values(99,'杭州市',12);
insert into city values(100,'宁波市',12);
insert into city values(101,'温州市',12);
insert into city values(102,'嘉兴市',12);
insert into city values(103,'湖州市',12);
insert into city values(104,'绍兴市',12);
insert into city values(105,'金华市',12);
insert into city values(106,'衢州市',12);
insert into city values(107,'舟山市',12);
insert into city values(108,'台州市',12);
insert into city values(109,'丽水市',12);
--select from city where provincialID=12 order by cityID
---------------------------------------------------------------- 没有写完, 写不下了,你加我! 775790318
中国有41636个乡镇行政单位。截至2016年底,中国大陆全境有街道8105个。
全国乡镇级以下行政区划共有:2个区公所,19531个镇,14677个乡,181个苏木,1092 个民族乡,1个民族苏木,6152个街道。
希望能够帮助到你,望采纳~
1、在做这个小项目前的准备工作:
11、分析:
由于省、市、县城(区)这些数据是存储到了MySQL数据库中的,我们要通过后台servlet获取数据库中的数据,然后再通过转发或者重定向的方式将数据呈现到前台页面中。
12、需要导入的jar包有:
mysql驱动包:mysql-connector-Java-517-binjar
c3p0数据库连接池:c3p0-0921jar、mysql-connector-java-517-binjar(c3p0依赖包)
前台c标签(需要通过遍历的方式呈现——c:forEach):jstl-102jar、standard-101jar(jstl依赖包)
将集合或者数组转换成json数据格式(Jackson包):jackson-annotations-221jar、jackson-core-221jar、jackson-databind-221jar
前台页面需要用到jQuery,故还需要导入jquery-172js库文件
13、该小项目用到的技术:
jdbc的知识、servlet的知识、jquery的知识、Ajax的知识(局部刷新)、标签的知识、EL表达式的知识、JSP的知识
2、开发过程:
21、准备数据源
创建一个数据库,命名为thereaction并创建三个表——province(省)、city(市)、county(县/区)
22后台开发
创建三个Javabean分别是Province、City、County。(由于太过简单,这里就不粘代码了)
创建Java类和c3p0连接池实现与数据库的连接:DAOjava(实现获取数据库数据的功能)、jdbctoolsjava(实现获取数据库连接、释放连接的功能)、c3p0-configxml
jdbctoolsjava代码如下:
[java] view plain copy
package comxiaojiedao;
import javaioIOException;
import javasqlConnection;
import javasqlPreparedStatement;
import javasqlResultSet;
import javasqlSQLException;
import javaxsqlDataSource;
import commchangev2c3p0ComboPooledDataSource;
public class Jdbctools {
private static DataSource ds=null;
//数据库连接池应只被初始化一次
static{
ds=new ComboPooledDataSource("helloc3p0");
}
//获取数据库连接
public static Connection getConnection() throws ClassNotFoundException, SQLException, IOException{
return dsgetConnection();
}
public static void shifanglianjie(Connection ct, PreparedStatement ps,ResultSet rs) {
if(rs!=null){
try {
rsclose();
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
if(ps!=null){
try {
psclose();
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
if(ct!=null){
try {
ctclose();
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
}
DAOjava的代码如下
[java] view plain copy
package comxiaojiedao;
import javaioIOException;
import javasqlConnection;
import javasqlPreparedStatement;
import javasqlResultSet;
import javasqlSQLException;
import javautilArrayList;
import javautilList;
import comxiaojiebeansCity;
import comxiaojiebeansCounty;
import comxiaojiebeansProvince;
public class DAO {
public List<Province> getprovince(String sql ,Objectargs) throws ClassNotFoundException, SQLException, IOException{
List<Province> provinces=new ArrayList<Province>();
//准备去查数据库
Connection ct=null;
ct=JdbctoolsgetConnection();
Systemoutprintln("获取到数据库的连接了");
PreparedStatement ps=null;
ResultSet rs=null;
ps=ctprepareStatement(sql);
for(int i=0;i<argslength;i++){
pssetObject(i+1, args[i]);
}
rs=psexecuteQuery();
while(rsnext()){
provincesadd(new Province(rsgetInt("province_id"),rsgetString("province_name")));
}
Jdbctoolsshifanglianjie(ct, ps, rs);
return provinces;
}
public List<City> getcity(String sql ,Objectargs) throws SQLException, ClassNotFoundException, IOException{
List<City> cities=new ArrayList<City>();
//准备去查数据库
Jdbctools jt=new Jdbctools();
Connection ct=null;
ct=jtgetConnection();
PreparedStatement ps=null;
ResultSet rs=null;
ps=ctprepareStatement(sql);
for(int i=0;i<argslength;i++){
pssetObject(i+1, args[i]);
}
rs=psexecuteQuery();
while(rsnext()){
citiesadd(new City(rsgetInt("city_id"),rsgetString("city_name")));
}
jtshifanglianjie(ct, ps, rs);
return cities;
}
public List<County> getcounty(String sql,Objectargs ) throws SQLException, ClassNotFoundException, IOException{
List<County> counties=new ArrayList<County>();
//准备去查数据库
Jdbctools jt=new Jdbctools();
Connection ct=null;
ct=jtgetConnection();
PreparedStatement ps=null;
ResultSet rs=null;
ps=ctprepareStatement(sql);
for(int i=0;i<argslength;i++){
pssetObject(i+1, args[i]);
}
rs=psexecuteQuery();
while(rsnext()){
countiesadd(new County(rsgetInt("county_id"),rsgetString("county_name")));
}
jtshifanglianjie(ct, ps, rs);
return counties;
}
}
c3p0-configxml的代码如下:
[html] view plain copy
<xml version="10" encoding="UTF-8">
<c3p0-config>
<named-config name="helloc3p0">
<!-- 连接数据源的基本属性 -->
<property name="user">root</property>
<property name="password"></property>
<property name="driverClass">commysqljdbcDriver</property>
<property name="jdbcUrl">jdbc:mysql:///thereaction</property>
<!-- 若数据库中连接数不足时,一次向数据库服务器申请多少个连接 -->
<property name="acquireIncrement">5</property>
<!-- 初始化数据库连接池时连接的数量 -->
<property name="initialPoolSize">5</property>
<!-- 数据库连接池中的最小的数据库连接数 -->
<property name="minPoolSize">5</property>
<!-- 数据库连接池中的最大的数据库连接数 -->
<property name="maxPoolSize">10</property>
<!-- c3p0数据库连接可以维护的statement的个数 -->
<property name="maxStatements">20</property>
<!-- 每个连接同时可以使用的statement对象的个数 -->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
创建servletjava 文件
[java] view plain copy
package comxiaojieservlet;
import javaioIOException;
import javalangreflectMethod;
import javasqlSQLException;
import javautilList;
import javaxservletServletException;
import javaxservletannotationWebServlet;
import javaxservlet>
以上就是关于请问用什么方式可以实现省市级联的效果呀 省市的数据是从数据库中查出来的全部的内容,包括:请问用什么方式可以实现省市级联的效果呀 省市的数据是从数据库中查出来的、求一完整的全国省市级联的oracle数据库建表脚本、请问全中国一共有几个街道呢等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)