Ajax Study Notes-three levels of Association

Source: Internet
Author: User

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4C/04/wKiom1Q2m8GRuHAhAACI9GIF62k176.jpg "Title =" qq20141009222851.png "alt =" wkiom1q2m8gruhaaci9gif62k176.jpg "/>

Three entity classes

Location. Java

public class Location {private Integer location_id;private String city;//...}

Department. Java

public class Department {private Integer department_id;private String departmentName;private Integer location_id;//...}

Employee. Java

public class Employee {private Integer employeeId;private String lastName;private String email;private double salary;private Integer department_id;//...}

There is only one line of code in emplooyes. jsp under webcontent, jump to Servlet

<%response.sendRedirect("EmployeeServlet?method=listLocations");%>

Employeeservlet. Java

public class EmployeeServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String methodName = request.getParameter("method");try {Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);method.invoke(this, request, response);} catch (Exception e) {e.printStackTrace();}}protected void listLocations(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Connection connection = DBManager.getConnection();String sql = "select * from location";List<Location>locations = new ArrayList<Location>();Statement statement = null;ResultSet resultSet = null;Location location = null;try {statement = connection.createStatement();resultSet = statement.executeQuery(sql);while(resultSet.next()){location = new Location();location.setCity(resultSet.getString("city"));location.setLocation_id(resultSet.getInt("location_id"));locations.add(location);}} catch (SQLException e) {e.printStackTrace();}finally{DBManager.close(resultSet, statement, connection);}request.setAttribute("locations", locations);request.getRequestDispatcher("/WEB-INF/pages/employees.jsp").forward(request, response);}protected void listDepartments(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {int locationId = Integer.parseInt(request.getParameter("locationId"));String sql = "select * from department where location_id = " + locationId;Connection connection = DBManager.getConnection();List<Department>departments = new ArrayList<Department>();Department department = null;ResultSet resultSet = null;Statement statement = null;try {statement = connection.createStatement();resultSet = statement.executeQuery(sql);while(resultSet.next()){department = new Department();department.setDepartmentName(resultSet.getString("departmentName"));department.setDepartment_id(resultSet.getInt("department_id"));department.setLocation_id(resultSet.getInt("location_id"));departments.add(department);}} catch (SQLException e) {e.printStackTrace();}finally{DBManager.close(resultSet, statement, connection);}ObjectMapper mapper = new ObjectMapper();String result = mapper.writeValueAsString(departments);System.out.println(result);response.setContentType("text/javascript");response.setCharacterEncoding("UTF-8");response.getWriter().print(result);}protected void listEmployees(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {int department_id = Integer.parseInt(request.getParameter("department_id"));String sql = "select * from employee where department_id = " + department_id;Connection connection = DBManager.getConnection();Statement statement = null;ResultSet resultSet = null;List<Employee>employees = new ArrayList<Employee>();Employee employee = null;try {statement = connection.createStatement();resultSet = statement.executeQuery(sql);while(resultSet.next()){employee = new Employee();employee.setDepartment_id(resultSet.getInt("department_id"));employee.setEmail(resultSet.getString("email"));employee.setEmployeeId(resultSet.getInt("employee_id"));employee.setLastName(resultSet.getString("last_name"));employee.setSalary(resultSet.getDouble("salary"));employees.add(employee);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBManager.close(resultSet, statement, connection);}ObjectMapper mapper = new ObjectMapper();String result = mapper.writeValueAsString(employees);System.out.println(result);response.setContentType("text/javascript");response.setCharacterEncoding("UTF-8");response.getWriter().print(result);}protected void listEmployeeInfo(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {int employeeId = Integer.parseInt(request.getParameter("employeeId"));String sql = "select * from employee where employee_id = " + employeeId;System.err.println(sql);Connection connection = DBManager.getConnection();Statement statement = null;ResultSet resultSet = null;Employee employee = null;try {statement = connection.createStatement();resultSet = statement.executeQuery(sql);if (resultSet.next()) {employee = new Employee();employee.setDepartment_id(resultSet.getInt("department_id"));employee.setEmail(resultSet.getString("email"));employee.setEmployeeId(resultSet.getInt("employee_id"));employee.setLastName(resultSet.getString("last_name"));employee.setSalary(resultSet.getDouble("salary"));}}catch(Exception e){e.printStackTrace();}finally{DBManager.close(resultSet, statement, connection);}ObjectMapper mapper = new ObjectMapper();String result = mapper.writeValueAsString(employee);System.out.println(result);response.setContentType("text/javascript");response.setCharacterEncoding("UTF-8");response.getWriter().print(result);}public static void main(String[] args) {System.out.println(DBManager.getConnection());}}

/WEB-INF/pages/employees. jsp browser not accessible, only through servlet jump to this page

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

Source http://yunpan.cn/cgephkTV2Tcmh (extract code: 2492)

This article is from the "Avatar" blog, please be sure to keep this source http://shamrock.blog.51cto.com/2079212/1561911

Ajax Study Notes-three levels of Association

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.