JAVA——三级联动 省 市 区
Posted 拿红罗卜钓鱼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA——三级联动 省 市 区相关的知识,希望对你有一定的参考价值。
login.jsp首页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script type="text/javascript" src="js/jquery-3.6.0.js"></script>
</head>
<body>
<script>
$(function () {
$.ajax({
type:"get",//类型
url:"findprovince",//地址
dataType:"json",//数据类型
success:function (data) {//回调函数 data的值就是从后端发送过来的json格式的字符串
//拿到当前省份的元素对象
var obj=$("#province");
for ( var i = 0; i < data.length;i++){
var ob="<option value='"+data[i].pid+"'>"+data[i].pname+"</option>";
obj.append(ob);
}
}
});
});
</script>
<select name="province" id="province">
<option value="0">请选择</option>
</select>省
<select name="city" id="city">
<option value="0">请选择</option>
</select>市
<select name="street" id="street">
<option value="0">请选择</option>
</select>区
<script>
$("#province").change(function () {
$("#city option").remove();
$.ajax({
type: "get",
url: "findcitybypid?id="+$("#province").val(),
dataType: "json",
success:function (data) {
var obj= $("#city");
for (var i=0;i<data.length;i++){
var ob= "<option value='"+data[i].cid+"'>"+data[i].cname+"</option>"
obj.append(ob)
}
}
});
});
</script>
<script>
$("#city").change(function () {
$("#street option").remove();
$.ajax({
type:"get",
url:"findstreetbysid?id="+$("#city").val(),
dataType: "json",
success:function (data) {
var obj= $("#street");
for (var i=0;i<data.length;i++){
var ob= "<option value='"+data[i].sid+"'>"+data[i].sname+"</option>"
obj.append(ob)
}
}
});
});
</script>
</body>
</html>
dao层 的接口
省份 的dao的接口ProvinceInfoDao
package cn.hp.dao;
import cn.hp.model.Province;
import java.util.List;
public interface ProvinceInfoDao {
public List<Province> findAll();
}
市的dao的接口CityInfoDao
package cn.hp.dao;
import cn.hp.model.City;
import cn.hp.model.Province;
import java.util.List;
public interface CityInfoDao {
public List<City> findAllCity(int pid);
}
区的dao的接口StreetDao
package cn.hp.dao;
import cn.hp.model.Street;
import java.util.List;
public interface StreetDao {
public List<Street> findAllStreet(int cid);
}
impl层 dao的实现类
省份dao的实现类ProvinceInfoDaoImpl
package cn.hp.dao;
import cn.hp.model.Province;
import cn.hp.util.DBHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
//接口实现类
public class ProvinceInfoDaoImpl implements ProvinceInfoDao {
@Override
public List<Province> findAll() {
Connection conn = DBHelper.getConn();
List<Province> list=new ArrayList<Province>();
String sql="select * from province";
try {
PreparedStatement ps=conn.prepareStatement(sql);//预编译当前的sql语句
ResultSet rs = ps.executeQuery();
while (rs.next()){
Province p=new Province();//实例化当前省份
p.setPid(rs.getInt(1));
p.setPname(rs.getString(2));
list.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
市dao的实现类CityInfoDaoImpl
package cn.hp.dao;
import cn.hp.model.City;
import cn.hp.util.DBHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class CityInfoDaoImpl implements CityInfoDao {
@Override
public List<City> findAllCity(int pid) {
Connection conn = DBHelper.getConn();
List<City> list=new ArrayList<City>();
String sql="select * from city where pid=?";
try {
PreparedStatement ps=conn.prepareStatement(sql);//预编译当前的sql语句
ps.setInt(1,pid);
ResultSet rs = ps.executeQuery();
while (rs.next()){
City c=new City();//实例化当前省份
c.setCid(rs.getInt(1));
c.setCname(rs.getString(2));
c.setPid(rs.getInt(3));
list.add(c);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
区县dao的实现类StreetDaoImpl
package cn.hp.dao;
import cn.hp.model.Street;
import cn.hp.util.DBHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StreetDaoImpl implements StreetDao {
@Override
public List<Street> findAllStreet(int cid) {
Connection conn= DBHelper.getConn();
List<Street> list= new ArrayList<Street>();
String sql="select * from street where cid =?";
try {
PreparedStatement ps= conn.prepareStatement(sql);//预编译当前的sql语句
ps.setInt(1,cid);
ResultSet rs= ps.executeQuery();
while (rs.next()){
Street s= new Street();//实例化当前区
s.setSid(rs.getInt(1));
s.setSname(rs.getString(2));
s.setCid(rs.getInt(3));
list.add(s);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
model层 实体类
省 Province
package cn.hp.model;
public class Province {
private int pid;
private String pname;
public Province(int pid, String pname) {
this.pid = pid;
this.pname = pname;
}
public Province() {
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
}
市City
package cn.hp.model;
public class City {
private int cid;
private String cname;
private int pid;
public City(int cid, String cname, int pid) {
this.cid = cid;
this.cname = cname;
this.pid = pid;
}
public City() {
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
}
区Street
package cn.hp.model;
public class Street {
private int sid;
private String sname;
private int cid;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public Street(int sid, String sname, int cid) {
this.sid = sid;
this.sname = sname;
this.cid = cid;
}
public Street() {
}
}
servlet层
省 ProvincServlet
package cn.hp.servlet;
import cn.hp.dao.ProvinceInfoDao;
import cn.hp.dao.ProvinceInfoDaoImpl;
import cn.hp.model.Province;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/findprovince")
public class FindProvinceServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//编码格式
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
ProvinceInfoDao pid=new ProvinceInfoDaoImpl();
List<Province> plist =pid.findAll();
//把这个省份的集合转换成json格式的数据发送到前端页面
response.getWriter().write(JSONObject.toJSONString(plist));
}
}
市CityPidServlet
package cn.hp.servlet;
import cn.hp.dao.CityInfoDao;
import cn.hp.dao.CityInfoDaoImpl;
import cn.hp.model.City;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/findcitybypid")
public class FindCityByPidServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//编码格式
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String id = request.getParameter("id");
CityInfoDao cid=new CityInfoDaoImpl();
List<City> list = cid.findAllCity(Integer.parseInt(id));
//把城市的集合转换成json格式的字符串发送到前端页面
response.getWriter().write(JSONObject.toJSONString(list));
}
}
区CitySidServlet
package cn.hp.servlet;
import cn.hp.dao.StreetDao;
import cn.hp.dao.StreetDaoImpl;
import cn.hp.model.Street;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/findstreetbysid")
public class FindCityBySidServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//编码格式
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String id = request.getParameter("id");
StreetDao sid=new StreetDaoImpl();
List<Street> slist = sid.findAllStreet(Integer.parseInt(id));
response.getWriter().write(JSONObject.toJSONString(slist));
}
}
util层工具类
package cn.hp.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBHelper {
private static String Driver = "com.mysql.jdbc.Driver";
private static String Url = "jdbc:mysql://localhost:3306/provinces?characterEncoding=utf8";
private static String user = "root";
private static String pwd = "123456";
public static Connection conn;
public static Connection getConn() {
try {
Class.forName(Driver);
conn = DriverManager.getConnection(Url, user, pwd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void getClose() {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(getConn());
if (getConn()!=null) {
System.out.println("���ӳɹ�");
}
}
}
数据库信息
CREATE database provinces CHARACTER set utf8;
use provinces;
CREATE table province (
pid INT PRIMARY KEY auto_increment,
pname varchar(20)
);
INSERT into province VALUES (null,"河南省");
INSERT into province VALUES (null,"山东省");
INSERT into province VALUES (null,"河北省");
CREATE table city (
cid INT PRIMARY KEY auto_increment,
cname varchar(20),
pid int
);
-- 河南省
INSERT into city VALUES (null,"郑州市",1);
INSERT into city VALUES (null,"开封市",1);
INSERT into city VALUES (null,"洛阳市",1);
-- 山东
INSERT into city VALUES (null,"济南市",2);
INSERT into city VALUES (null,"青岛市",2);
INSERT into city VALUES (null,"淄博市",2);
-- 河北
INSERT into city VALUES (null,"石家庄市",3);
INSERT into city VALUES (null,"唐山市",3);
INSERT into city VALUES (null,"秦皇岛市",3);
CREATE table street (
sid INT PRIMARY KEY auto_increment,
sname varchar(20),
cid int
);
-- 郑州市
INSERT into street VALUES (null,"中原区",1);
INSERT into street VALUES (null,"二七区",1);
INSERT into street VALUES (null,"管城回族区",1);
-- 开封市
INSERT into street VALUES (null,"龙亭区",2);
INSERT into street VALUES (null,"顺河回族区",2);
INSERT into street VALUES (null,"鼓楼区",2);
-- 洛阳市
INSERT into street VALUES (null,"汝阳",3);
INSERT into street VALUES (null,"宜阳",3);
INSERT into street VALUES (null,"洛宁",3);
-- 济南市
INSERT into street VALUES (null,"商河县",4);
INSERT into street VALUES (null,"济阳县",4);
INSERT into street VALUES (null,"平阴县",4);
-- 青岛市
INSERT into street VALUES (null,"七区五市",5);
INSERT into street VALUES (null,"市南区",5);
INSERT into street VALUES (null,"市北区",5);
-- 淄博市
INSERT into street VALUES (null,"博山",6);
INSERT into street VALUES (null,"周村",6);
INSERT into street VALUES (null,"临淄",6);
-- 石家庄市
INSERT into street VALUES (null,"正定县",7);
INSERT into street VALUES (null,"行唐县",7);
INSERT into street VALUES (null,"灵寿县",7);
-- 唐山市
INSERT into street VALUES (null,"乐亭县",8);
INSERT into street VALUES (null,"迁西县",8);
INSERT into street VALUES (null,"玉田县",8);
-- 秦皇岛市
INSERT into street VALUES (null,"青龙满族自治县",9);
INSERT into street VALUES (null,"昌黎县",9);
INSERT into street VALUES (null,"卢龙县",9);
实现效果
以上是关于JAVA——三级联动 省 市 区的主要内容,如果未能解决你的问题,请参考以下文章