博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
三级联动及读取配置文件
阅读量:5172 次
发布时间:2019-06-13

本文共 6182 字,大约阅读时间需要 20 分钟。

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

转载于:https://www.cnblogs.com/hwgok/p/5854548.html

你可能感兴趣的文章
用JS实现版面拖拽效果
查看>>
二丶CSS
查看>>
《avascript 高级程序设计(第三版)》 ---第二章 在HTML中使用Javascript
查看>>
JS一些概念知识及参考链接
查看>>
TCP/IP协议原理与应用笔记24:网际协议(IP)之 IP协议的简介
查看>>
SAP HANA开发中常见问题- 基于SAP HANA平台的多团队产品研发
查看>>
游戏中的心理学(一):认知失调有前提条件
查看>>
WHAT I READ FOR DEEP-LEARNING
查看>>
【Ruby】Ruby在Windows上的安装
查看>>
Objective C 总结(十一):KVC
查看>>
BZOJ 3747 洛谷 3582 [POI2015]Kinoman
查看>>
vue实战(7):完整开发登录页面(一)
查看>>
Visual Studio自定义模板(二)
查看>>
【Mood-20】滴滤咖啡做法 IT工程师加班必备 更健康的coffee 项目经理加班密鉴
查看>>
读《构建之法-软件工程》第四章有感
查看>>
使用 Printf via SWO/SWV 输出调试信息
查看>>
.net 分布式架构之分布式锁实现(转)
查看>>
Problem E: Automatic Editing
查看>>
SpringBoot 使用 MyBatis 分页插件 PageHelper 进行分页查询
查看>>
《DSP using MATLAB》Problem 6.17
查看>>