JAVA——三级联动 省 市 区

Posted z1427094386

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA——三级联动 省 市 区相关的知识,希望对你有一定的参考价值。

JAVA 分页效果实现
整体架构为
在这里插入图片描述

后端代码
dao层 的接口
省份 的dao的接口

package src.cn.hp.dao;
import src.cn.hp.model.Provice;
import java.util.List;
public interface ProviceInfoDao {
    /**
     * 查询所有的省份信息
     * @return  省份数据
     */
    public List<Provice> findAll();
}

市 的dao的接口

package src.cn.hp.dao;
import src.cn.hp.model.City;
import java.util.List;
public interface CityInfoDao {

    /**
     * 根据省份查询所属的市
     * @param pid 省份的id
     * @return  省份所属的市
     */
    public List<City> findAllCity(int pid);
}

区 的dao的接口

package src.cn.hp.dao;
import src.cn.hp.model.Street;
import java.util.List;
public interface StreetInfoDao {
    /**
     * 查询 所有区 信息
     * @param cid  根据市 的id 查
     * @return 区 信息
     */
    public List<Street> findAll(int cid);
}

impl层 dao的实现类
省份 dao的实现类 imol

package src.cn.hp.dao.impl;
import src.cn.hp.dao.ProviceInfoDao;
import src.cn.hp.model.Provice;
import src.cn.hp.util.ConnDemo;
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 ProviceInfoDaoImol implements ProviceInfoDao {
    @Override
    public List<Provice> findAll() {
        Connection conn = ConnDemo.getConn();
        List<Provice> list = new ArrayList<Provice>();
        String sql = "select * from provice";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Provice p = new Provice();
                p.setPid(rs.getInt(1));
                p.setPname(rs.getString(2));
                list.add(p);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}

市 dao的实现类 imol

package src.cn.hp.dao.impl;
import src.cn.hp.dao.CityInfoDao;
import src.cn.hp.model.City;
import src.cn.hp.model.Provice;
import src.cn.hp.util.ConnDemo;
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 = ConnDemo.getConn();
        List<City> list = new ArrayList<City>();
        String sql = "select * from city where pid=?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,pid);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                City p = new City();
                p.setCid(rs.getInt(1));
                p.setCname(rs.getString(2));
                p.setPid(rs.getInt(3));
                list.add(p);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}


区 县 dao的实现类 imol

package src.cn.hp.dao.impl;
import src.cn.hp.dao.StreetInfoDao;
import src.cn.hp.model.City;
import src.cn.hp.model.Street;
import src.cn.hp.util.ConnDemo;
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 StreetInfoDaoImpl implements StreetInfoDao {
    @Override
    public List<Street> findAll(int cid) {
        Connection conn = ConnDemo.getConn();
        List<Street> list = new ArrayList<Street>();
        String sql = "select * from street where cid=?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, cid);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Street p = new Street();
                p.setCid(rs.getInt(1));
                p.setSname(rs.getString(2));
                p.setCid(rs.getInt(3));
                list.add(p);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}


model层 实体类
省 model

package src.cn.hp.model;
public class Provice {
    private  int pid;
    private String pname;

    public Provice(int pid, String pname) {
        this.pid = pid;
        this.pname = pname;
    }

    public Provice() {
    }

    public int getPid() {
        return pid;
    }

    public Provice(String pname) {
        this.pname = pname;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }
}

市 model

package src.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 City(String cname, int pid) {
        this.cname = cname;
        this.pid = pid;
    }

    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;
    }
}

区 model

package src.cn.hp.model;

public class Street {

    private int sid;
    private  String sname;
    private int cid;

    public Street(int sid, String sname, int cid) {
        this.sid = sid;
        this.sname = sname;
        this.cid = cid;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public Street(int cid) {
        this.cid = cid;
    }

    public Street(String sname, int cid) {
        this.sname = sname;
        this.cid = cid;
    }

    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() {
    }
}

servlet层
省 servlet

package src.cn.hp.servlet;
import com.alibaba.fastjson.JSONObject;
import src.cn.hp.dao.ProviceInfoDao;
import src.cn.hp.dao.impl.ProviceInfoDaoImol;
import src.cn.hp.model.Provice;
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("/findprovice")
public class FindProviceServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        ProviceInfoDao pid = new ProviceInfoDaoImol();
        List<Provice> plist = pid.findAll();
//        把这个省份的集合转换成json 格式的数据发送到前端页面
        resp.getWriter().write(JSONObject.toJSONString(plist));
    }
}

市 servlet

package src.cn.hp.servlet;
import com.alibaba.fastjson.JSONObject;
import src.cn.hp.dao.CityInfoDao;
import src.cn.hp.dao.ProviceInfoDao;
import src.cn.hp.dao.impl.CityInfoDaoImpl;
import src.cn.hp.dao.impl.ProviceInfoDaoImol;
import src.cn.hp.model.City;
import src.cn.hp.model.Provice;
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 {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String id=req.getParameter("id");
        CityInfoDao pid = new CityInfoDaoImpl();
        List<City> list = pid.findAllCity(Integer.parseInt(id));
//        把这个城市的集合转换成json 格式的数据发送到前端页面
        resp.getWriter().write(JSONObject.toJSONString(list));
    }
}

 

区 servlet

package src.cn.hp.servlet;
import com.alibaba.fastjson.JSONObject;
import src.cn.hp.dao.CityInfoDao;
import src.cn.hp.dao.StreetInfoDao;
import src.cn.hp.dao.impl.CityInfoDaoImpl;
import src.cn.hp.dao.impl.StreetInfoDaoImpl;
import src.cn.hp.model.City;
import src.cn.hp.model.Street;
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("/findstreetbypid")
public class FindStreetByPidServlet extends HttpServlet {


    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String id=req.getParameter("id");
        StreetInfoDao pid = new StreetInfoDaoImpl();
        List<Street> list = pid.findAll(Integer.parseInt(id));
//        把这个城市的集合转换成json 格式的数据发送到前端页面
        resp.getWriter().write(JSONObject.toJSONString(list));
        
    }
}

util 层 工具类

package src.cn.hp.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnDemo {
	private static String Driver = "com.mysql.jdbc.Driver";
	private static String Url = "jdbc:mysql://localhost:3306/provices?characterEncoding=utf8";
	private 以上是关于JAVA——三级联动 省 市 区的主要内容,如果未能解决你的问题,请参考以下文章

JAVA——三级联动 省 市 区

html实现 省——市——区三级联动

20180825 ajax PHP html js 实现 三级联动(省 市 区)

WinForm Timer控件,三级联动[省,市,区]

2017-5-7 time控件 三级联动(省,市,区)

2017-5-7 time控件 三级联动(省,市,区)