您好,登錄后才能下訂單哦!
本篇內容主要講解“ajax實現三級聯動省市的代碼”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“ajax實現三級聯動省市的代碼”吧!
創建數據庫
首先創建 City 和 Province 類 給getter setter tostring 以及 構造方法 Province類
City類
連接數據庫
創建 接口 ProvinceInfoDao
實例化對象ProvinceInfoDaoImpl
以及CityInfoDao接口
CityInfoDaoImpl實例化對象
FindProvinceServlet
FindCityPidServlet
最后是jsp頁面
總結
我只寫到了市剩下的區可以復制粘貼的很簡單
所需要的jar包有
代碼如下
CREATE database provinces CHARACTER set utf8; use provices; 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,"臺灣省"); 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,"菏澤曹縣",4); INSERT into city VALUES(null,"高雄市",3); INSERT into city VALUES(null,"保定",5); INSERT into city VALUES(null,"三亞市",2);
package cn.hp.model; public class Province { private int pid; private String pname; 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; } @Override public String toString() { return "Province{" + "pid=" + pid + ", pname='" + pname + '\'' + '}'; } public Province(int pid, String pname) { this.pid = pid; this.pname = pname; } public Province() { } }
package cn.hp.model; public class City { private int cid; private String cname; private int pid; public City() { } public int getCid() { return cid; } @Override public String toString() { return "City{" + "cid=" + cid + ", cname='" + cname + '\'' + ", pid=" + pid + '}'; } public void setCid(int cid) { this.cid = cid; } public String getCname() { return cname; } public City(String cname, int pid) { this.cname = cname; this.pid = pid; } public void setCname(String cname) { this.cname = cname; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public City(int cid, String cname, int pid) { this.cid = cid; this.cname = cname; this.pid = pid; } }
package 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/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("??????"); } } }
package cn.hp.dao; import cn.hp.model.Province; import java.util.List; public interface ProvinceInfoDao { public List<Province> findAll(); }
package cn.hp.dao; import cn.hp.model.Province; import 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 ProvinceInfoDaoImpl implements ProvinceInfoDao { @Override public List<Province> findAll() { Connection conn= ConnDemo.getConn(); List<Province> list= new ArrayList<Province>(); String sql="select * from provice"; try { PreparedStatement ps= conn.prepareStatement(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; } }
package cn.hp.dao; import cn.hp.model.City; import java.util.List; public interface CityInfoDao { public List<City>findAllCity(int pid); }
package cn.hp.dao; import cn.hp.model.City; import cn.hp.model.Province; import 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 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; } }
寫servlet FindProvinceServlet 以及FindCityPidServlet
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 doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } 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(); response.getWriter().write(JSONObject.toJSONString(plist)); } }
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 { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } 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> clist = cid.findAllCity(Integer.parseInt(id)); response.getWriter().write(JSONObject.toJSONString(clist)); } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <script src="js/jquery-3.6.0.js"></script> <title>$Title$</title> <script> $(function () { $.ajax({ type:"get", url:"findProvince", dataType:"json", success:function (data) { var obj= $("#province"); for (var i=0;i<data.length;i++){ // var ob= "<option value='+data[i].pid+"'>"+data[i].pname+"</option>" var ob= "<option value='"+data[i].pid+"'>"+data[i].pname+"</option>" obj.append(ob) } } }) }) </script> </head> <body> <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>區 </body> <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> </html>
到此,相信大家對“ajax實現三級聯動省市的代碼”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。