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


    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

  • 相关阅读:
    mysql的存储过程与事务入门
    关于CreateProcess函数一些经验
    windows下dump文件调试
    指针相关
    nsi脚本中执行.bat文件要隐藏dos窗口问题
    C 预处理
    __declspec(dllimport)与__declspec(dllexport)作用总结
    [转]window下使用SetUnhandledExceptionFilter捕获让程序的崩溃
    字符集与编码[转]
    vc下项目的头文件包含目录以及库导入预计库目录设置
  • 原文地址:https://www.cnblogs.com/hwgok/p/5854548.html
Copyright © 2020-2023  润新知