JavaBean + dao +Servlet + JSP 实现省市区的三级下拉列表
首先分别创建JavaBean的省、市、区对象的SetGet构造方法,
接着定义dao接口的实现类实现数据库的连接,
再通过servlet对数据库进行查询,
最后在JSP上布局网页实现交互功能。
bean. Province.java
public class Province {
private int id;
private String code;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Province() {
// TODO Auto-generated constructor stub
}
public Province(int id, String code, String name) {
super();
this.id = id;
this.code = code;
this.name = name;
}
}
bean.City.java
public class City {
private int id;
private String code;
private String name;
private String provinceCode;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProvinceCode() {
return provinceCode;
}
public void setProvinceCode(String provinceCode) {
this.provinceCode = provinceCode;
}
public City() {
// TODO Auto-generated constructor stub
}
public City(int id, String code, String name, String provinceCode) {
super();
this.id = id;
this.code = code;
this.name = name;
this.provinceCode = provinceCode;
}
}
bean.Town.java
public class Town {
private int id;
private String code;
private String name;
private String cityCode;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public Town() {
// TODO Auto-generated constructor stub
}
public Town(int id, String code, String name, String cityCode) {
super();
this.id = id;
this.code = code;
this.name = name;
this.cityCode = cityCode;
}
}
dao.AddressDao.java(interface)
public List
getAllProvinceList(); ※定义接口(规范)让其实现类实现规定的方法
dao.impl.AddressDaoImpl.java(implements AddressDao)
@Override
public List
getAllProvinceList() { // 查询数据库
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List
pList = new ArrayList<>(); try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/java0520";
String user = "root";
String password = "1234";
conn = DriverManager.getConnection(url, user, password);
ps = conn.prepareStatement("select * from t_address_province");
rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String code = rs.getString("code");
String name = rs.getString("name");
Province province = new Province(id, code, name);
pList.add(province);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
return pList;
}
}
servlet.GetProvince.java
@WebServlet("/getprovince")
public class GetProvince extends HttpServlet {
private static final long serialVersionUID = 1L;
private AddressDao addressDao = new AddressDaoImpl();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8"); //设置字符集
List
pList = addressDao.getAllProvinceList(); String jsonString = JSON.toJSONString(pList); //将pList转换成json格式的字符串
PrintWriter out = response.getWriter(); //将字符串放入流中
out.write(jsonString);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
servlet.GetCity.java
@WebServlet("/getcity")
public class GetCity extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String pcode = request.getParameter("pcode");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List
cList = new ArrayList<>(); try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/java0520", "root", "1234");
ps = conn.prepareStatement("select * from t_address_city where provinceCode = ?");
ps.setString(1, pcode);
rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String code = rs.getString("code");
String name = rs.getString("name");
String provinceCode = rs.getString("provinceCode");
City city = new City(id,code,name,provinceCode);
cList.add(city);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
}
String jsonString = JSON.toJSONString(cList);
PrintWriter out = response.getWriter();
out.write(jsonString);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
servlet.GetTown.java
@WebServlet("/gettown")
public class GetTown extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String ccode = request.getParameter("ccode");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List
tList = new ArrayList<>(); try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/java0520", "root", "1234");
ps = conn.prepareStatement("select * from t_address_town where cityCode = ?");
ps.setString(1, ccode);
rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String code = rs.getString("code");
String name = rs.getString("name");
String cityCode = rs.getString("cityCode");
Town town = new Town(id, code, name, cityCode);
tList.add(town);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
}
String jsonString = JSON.toJSONString(tList);
PrintWriter out = response.getWriter();
out.write(jsonString);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
view/3level.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
$(function(){
getProvince();
getCity();
getTown();
})
function getProvince(){
$.ajax({
"url":"getprovince",
"type":"get",
"success":function(data){
var ProvinceArray = JSON.parse(data);
for(var i = 0 ; i < ProvinceArray.length ; i++){
$("#pid").append("");
}
}
})
}
function getCity(provinceCode){
$.ajax({
"url":"getcity",
"type":"get",
"data":{ "pcode" : provinceCode },
"success":function(data){
var CityArray = JSON.parse(data);
$("#cid").html("");
$("#tid").html("");
for(var i = 0 ; i < CityArray.length ; i++){
$("#cid").append("");
}
}
})
}
function getTown(cityCode){
$.ajax({
"url":"gettown",
"type":"get",
"data":{ "ccode" : cityCode },
"success":function(data){
var TownArray = JSON.parse(data);
$("#tid").html("");
for(var i = 0 ; i < TownArray.length ; i++){
$("#tid").append("");
}
}
})
}