在easyui里写一个连接数据库的二级联动

二级联动的效果图

在easyui里写一个连接数据库的二级联动_第1张图片 在easyui里写一个连接数据库的二级联动_第2张图片


第一步: 引入easyui库 ,如果没有easyui可以去www.jeasyui.net官网去下载

   

 
 
第二步:写入下拉框

省:
市:

第三步:写js样式

第四步:写一个dao方法

package com.zking.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zking.entity.City;
import com.zking.util.DBHelper;

public class CityDao {
	/**
	 * 查询省、distinct,去重复值
	 * @return 省
	 */
	 
	public List selectSheng() {
		List cList=new ArrayList<>();
		Connection conn=DBHelper.getConn();
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			ps=conn.prepareStatement("select distinct sheng from city");
			rs=ps.executeQuery();
			while(rs.next()){
				String sheng=rs.getString("sheng");
				City c=new City(sheng);
				cList.add(c);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBHelper.myCloas(conn, ps, rs);
		}
		return cList;
	}

	/**
	 *  根据省查询ID、市
	 * @param sheng 省
	 * @return ID、市
	 */
	 
	public List selectShi(String sheng) {
		List cList=new ArrayList<>();
		Connection conn=DBHelper.getConn();
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			ps=conn.prepareStatement("select c_id,shi from city where sheng=?");
			ps.setString(1, sheng);
			rs=ps.executeQuery();
			while(rs.next()){
				int c_id=rs.getInt("c_id");
				String shi=rs.getString("shi");
				City c=new City(c_id,shi);
				cList.add(c);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBHelper.myCloas(conn, ps, rs);
		}
		return cList;
	}
}
第五部:我用了Struts2,所以你要导入9个Struts需要的jar包,写一个Struts.xml




	
	

	
		
		
	
第六步:写一个Cityaction.java

package com.zking.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.zking.dao.CityDao;
import com.zking.entity.City;

public class CityAction extends ActionSupport{

	public String selectSheng() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8"); 
		HttpServletRequest req=ServletActionContext.getRequest();		
		req.setCharacterEncoding("UTF-8");
		CityDao dao=new CityDao();
		List cList=dao.selectSheng();
		List cLists=new ArrayList<>();
		for (int i = 0; i < cList.size(); i++) {
			City citys=new City(i, cList.get(i).getSheng());
			cLists.add(citys);
		}
		PrintWriter out=ServletActionContext.getResponse().getWriter();
		String json=JSON.toJSONString(cLists);
		out.write(json.toString());
		out.close();
		return null;
	}
	
	public String selectShi() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8"); 
		HttpServletRequest req=ServletActionContext.getRequest();		
		req.setCharacterEncoding("UTF-8");
		CityDao dao=new CityDao();
		String sheng=req.getParameter("sheng");
		List cLists=dao.selectShi(sheng);
		PrintWriter out=ServletActionContext.getResponse().getWriter();
		String json=JSON.toJSONString(cLists);
		out.write(json.toString());
		out.close();
		return null;
	}
}






你可能感兴趣的:(框架)