• 三级联动及读取配置文件


    CascadeController.java

    package com.huawei.controller;

    import java.io.IOException;
    import java.util.List;

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

    import com.cdsxt.po.Area;
    import com.cdsxt.po.City;
    import com.cdsxt.po.Province;
    import com.cdsxt.service.CascadeService;
    import com.google.gson.Gson;

    /**
    * Servlet implementation class CascadeController
    */
    public class CascadeController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private CascadeService cascadeService = new CascadeService();
    /**
    * @see HttpServlet#HttpServlet()
    */
    public CascadeController() {
    super();
    // TODO Auto-generated constructor stub
    }

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    this.doPost(request, response);
    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");

    String _method = request.getParameter("_method");

    if("findAllProvince".equals(_method)){
    this.findAllProvince(request, response);
    }else if("findCityByProvinceID".equals(_method)){
    this.findCityByProvinceID(request, response);
    }else if("findAreaByCityID".equals(_method)){
    this.findAreaByCityID(request, response);
    }

    }

    protected void findAllProvince(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<Province> provinces = this.cascadeService.findAllProvince();
    response.getWriter().write(new Gson().toJson(provinces));
    }
    protected void findCityByProvinceID(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String privinceID = request.getParameter("id");

    List<City> cities = this.cascadeService.findCityByProvinceID(Integer.parseInt(privinceID));
    response.getWriter().write(new Gson().toJson(cities));
    }
    protected void findAreaByCityID(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String cityID = request.getParameter("id");
    List<Area> areas = this.cascadeService.findAreaByCityID(Integer.parseInt(cityID));
    response.getWriter().write(new Gson().toJson(areas));
    }

    }

    CascadeService.java

    public class CascadeService {

    private CascadeDAO cascadeDAO = new CascadeDAO();

    public List<Province> findAllProvince(){
    return this.cascadeDAO.findAllProvince();
    }
    public List<City> findCityByProvinceID(Integer provinceID){
    return this.cascadeDAO.findCityByProvinceID(provinceID);
    }
    public List<Area> findAreaByCityID(Integer cityID){
    return this.cascadeDAO.findAreaByCityID(cityID);
    }
    }

    CascadeDAO.java

    package com.cdsxt.dao;

    public class CascadeDAO {

    public List<Province> findAllProvince(){

    List<Province> result = new ArrayList<Province>();

    Connection connection = DBUtil.getConnection();

    String sql = "SELECT * FROM province";

    PreparedStatement ps = null;

    ResultSet rs = null;

    try {
    ps = connection.prepareStatement(sql);
    rs = ps.executeQuery();
    while(rs.next()){
    result.add(new Province(rs.getInt("provinceID"), rs.getString("province")));
    }


    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    DBUtil.close(rs,ps,connection);
    }

    return result;

    }

    public List<City> findCityByProvinceID(Integer provinceID){
    List<City> result = new ArrayList<City>();

    Connection connection = DBUtil.getConnection();

    String sql = "SELECT * FROM city WHERE provinceID=?";

    PreparedStatement ps = null;

    ResultSet rs = null;

    try {
    ps = connection.prepareStatement(sql);
    ps.setInt(1, provinceID);
    rs = ps.executeQuery();
    while(rs.next()){
    result.add(new City(rs.getInt("cityID"), rs.getString("city")));
    }


    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    DBUtil.close(rs,ps,connection);
    }

    return result;
    }
    public List<Area> findAreaByCityID(Integer cityID){
    List<Area> result = new ArrayList<Area>();

    Connection connection = DBUtil.getConnection();

    String sql = "SELECT * FROM area WHERE cityID=?";

    PreparedStatement ps = null;

    ResultSet rs = null;

    try {
    ps = connection.prepareStatement(sql);
    ps.setInt(1, cityID);
    rs = ps.executeQuery();
    while(rs.next()){
    result.add(new Area(rs.getInt("areaID"), rs.getString("area")));
    }


    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    DBUtil.close(rs,ps,connection);
    }

    return result;
    }

    }

    DBUtil.java

    package com.cdsxt.utils;

    /**
    * @author Administrator
    *
    */
    public class DBUtil {

    private static Properties properties = new Properties();

    static {
    InputStream in = DBUtil.class.getClassLoader().getResourceAsStream("META-INF/config/db.properties");
    try {
    properties.load(in);
    } catch (IOException e) {
    e.printStackTrace();
    }finally{
    try {
    in.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    /**
    * 得到数据库连接
    * @return
    */
    public static Connection getConnection(){
    //显示加载驱动
    try {
    Class.forName(properties.getProperty("db.driver"));
    Connection connection = DriverManager.getConnection(properties.getProperty("db.host")+"/"+properties.getProperty("db.name"), properties.getProperty("db.username"), properties.getProperty("db.password"));
    return connection;
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }catch (SQLException e) {
    e.printStackTrace();
    }
    return null;
    }

    /**
    *
    * 释放资源
    * @param objects
    */
    public static void close(Object ...objects){
    if(objects!=null && objects.length>0){
    try {
    for(Object o:objects){
    if(o instanceof ResultSet){
    ((ResultSet)o).close();
    }else if(o instanceof Statement){
    ((Statement)o).close();
    }else if(o instanceof Connection){
    ((Connection)o).close();
    }
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    public static void main(String[] args) {
    System.out.println(DBUtil.getConnection());
    }
    }

    # MYSQL config
    db.driver=com.mysql.jdbc.Driver
    db.username=root
    db.password=123456
    db.name=test
    db.host=jdbc:mysql://localhost:3306


    # Oracle config

  • 相关阅读:
    原来不是女的是男生啊,这才科学么吓死我了
    实现“老木马”关键技术之一自启动
    映像劫持(IFEO)的原理及实现
    设计模式学习使用go实现装饰模式 Zhan
    设计模式学习使用go实现适配器模式 Zhan
    设计模式学习使用go实现模板模式 Zhan
    设计模式学习使用go实现观察者模式 Zhan
    设计模式学习使用go实现外观模式 Zhan
    设计模式学习使用go实现责任链模式 Zhan
    设计模式学习使用go实现组合模式 Zhan
  • 原文地址:https://www.cnblogs.com/hwgok/p/5854548.html
Copyright © 2020-2023  润新知