当前位置: 首页>编程语言>正文

java人口数据矩阵 人口统计java

service和servlet层

service.java

1 import java.util.List;
 2 /**
 3  * CourseService
 4  * 服务层
 5  * @author Hu
 6  *
 7  */
 8 
 9 //service层相当于一个封装层,封装那些与业务相关的通用数据接口
10 
11 public class service
12 {
13     dao cdao=new dao();
14     /**
15      * 添加
16      * @param course
17      * @return
18      */
19     public boolean insert(Grade grade)
20     {
21 //        boolean f=false;
22 //        if(!cdao.id(grade.getID()))
23 //        {
24 //            cdao.insert(grade);
25 //            f=true;
26 //        }
27         cdao.insert(grade);
28         return true;
29     }
30     
31     //删除
32     
33     public void delete(String name)
34     {
35         cdao.delete(name);
36         System.out.println(2);
37     }
38     
39     //修改
40     
41     public void update (Grade grade)
42     {
43         cdao.update(grade);
44     }
45     
46     //通过ID得到一个Course    
47     
48     //通过name得到一个Course    
49 
50     //查找
51     
52     
53     //全部数据遍历
54     
55     public List<Grade> list()
56     {
57         return cdao.list();
58     }
59 
60     public Grade selectByName(String name) {
61         // TODO Auto-generated method stub
62         return cdao.selectByName(name);
63     }
64 
65 
66 }

 

servlet层

servlet.html" class="superseo">java

1 import java.io.IOException;
  2 import java.util.List;
  3 
  4 import javax.servlet.ServletException;
  5 import javax.servlet.annotation.WebServlet;
  6 import javax.servlet.http.HttpServlet;
  7 import javax.servlet.http.HttpServletRequest;
  8 import javax.servlet.http.HttpServletResponse;
  9 import com.service;
 10 
 11 
 12 import com.User;
 13 
 14 @WebServlet("/servlet")
 15 public class servlet extends HttpServlet // 继承的是 HttpServlet方法,和网页交互的方法
 16 {
 17     private static final long serialVersionUID = 1L;
 18 
 19     service service = new service();
 20 
 21     // 方法选择
 22 
 23     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 24         // req是Request(请求)的简写,指的是从jsp/html界面请求回数据,即 jsp/html → 底层
 25         // resp是Response(回答)的简写,指的是发送给jsp/html的数据,,即 底层 → jsp/html
 26      
 27         req.setCharacterEncoding("utf-8");// 设置从jsp或HTML中请求数据的值,也就是设置为中文,防止乱码
 28         String method = req.getParameter("method");// getParameter()获取的是客户端设置的数据。
 29         if ("insert".equals(method)) {
 30             insert(req, resp);
 31         }
 32 //        // 增
 33         else if ("delete".equals(method)) {
 34             delete(req, resp);
 35         }
 36 //        // 删
 37         else if ("update".equals(method)) {
 38             update(req, resp);
 39         }
 40         // 改
 41         else if ("list".equals(method)) {
 42             list(req, resp);
 43         }else if ("add1".equals(method)) {
 44             add1(req, resp);
 45         }
 46         // 查
 47     }
 48 
 49     // 添加
 50 
 51     private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
 52         // 首先读取前端网页数据并将数据存入我创建的grade存储空间里,即 jsp/html → servlet
 53 
 54         req.setCharacterEncoding("utf-8"); // 从网页获得数据
 55         //String name = req.getParameter("name");
 56         //String sex = req.getParameter("sex");
 57         // 因为req.getParameter只能获得String类型的数据
 58         // 所以下面需要用Integer.parseInt(req.getParameter())来把String类型的数据转换成我们所需的int数据
 59         String Name = req.getParameter("name");
 60         String Sex = req.getParameter("sex");
 61         String nation = req.getParameter("nation");
 62         String ID = req.getParameter("ID");
 63         String hometype = req.getParameter("hometype");
 64         String roomtype = req.getParameter("roomtype");
 65         String roomnumber = req.getParameter("roomnumber");
 66         String xueli = req.getParameter("xueli");
 67         String square = req.getParameter("square");
 68         String s[]=new String[10];
 69        
 70         s[0]="";
 71         s = req.getParameterValues("category");
 72       
 73         Grade grade = new Grade(hometype,roomtype,square,roomnumber,Name,ID,Sex,nation,xueli);
 74 
 75         // 其次进行把grade里的数据与添加到数据库里,即 servlet → mysql数据库
 76         // 并跳转,req.getRequestDispatcher("add.jsp").forward(req,
 77         // resp);这一步是跳转到前端jsp/html界面
 78 
 79         if (service.insert(grade)) // 如果添加成功
 80         {
 81             req.setAttribute("message", "添加"+grade.getname()+"成功");
 82             // 这里表示,在req的作用域中设置一个massage变量,变量内容是:添加成功
 83             // 然后,当跳转到它下一个界面时,这个作用域里的值可以通过Object message
 84             // =request.getAttribute("message")拿出来使用
 85             req.getRequestDispatcher("index.jsp").forward(req, resp);
 86             // 处理完了,分发到下一个JSP页面或者下一个Action继续处理。
 87         } else {
 88             req.setAttribute("message", "添加失败!");
 89             req.getRequestDispatcher("insert.jsp").forward(req, resp);
 90         }
 91     }
 92 
 93     // 删除delete
 94 
 95     private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
 96         req.setCharacterEncoding("UTF-8");
 97         String name = req.getParameter("name");
 98         service.delete(name); // 进行数据库的删除操作
 99         req.setAttribute("message", "删除成功");
100         req.getRequestDispatcher("index.jsp").forward(req, resp);
101     }
102 
103     // 修改update
104 
105     private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
106         req.setCharacterEncoding("utf-8");
107         
108        
109         
110         
111         String name = req.getParameter("name");
112         String sex = req.getParameter("sex");
113         String nation = req.getParameter("nation");
114         String ID = req.getParameter("ID");
115         String hometype = req.getParameter("hometype");
116         String roomtype = req.getParameter("roomtype");
117         String roomnumber = req.getParameter("roomnumber");
118         String xueli = req.getParameter("xueli");
119         String square = req.getParameter("square");
120         String s[]=new String[10];
121         s[0]="";
122         s = req.getParameterValues("category");
123         
124         
125         
126         Grade user = new Grade(hometype,roomtype,square,roomnumber,name,ID,sex,nation,xueli);
127         service.update(user);// 进行数据库的修改操作
128         req.setAttribute("message", "修改成功");
129         req.setAttribute("user", user);
130         req.getRequestDispatcher("delete.jsp").forward(req, resp);
131         // ?method=list表示传一个叫做method的参数,他的值是list,你可以在Servlet中用request.getParam...获取到。
132     }
133 
134     // 全部
135 
136       private void add1(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
137             req.setCharacterEncoding("utf-8");
138             String username = req.getParameter("username");
139             String password = req.getParameter("password");
140 
141             User user = new User(username,password);
142 
143             dao dao =new dao();
144             boolean f=dao.add1(user);
145             if(f) {
146                 req.setAttribute("message", "登录成功!");
147                 req.getRequestDispatcher("index.jsp").forward(req,resp);
148             } else {
149                 
150                 req.setAttribute("message", "用户名或者账号错误!");
151                 req.getRequestDispatcher("denglu.jsp").forward(req,resp);
152             }
153         }
154     
155     private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
156         req.setCharacterEncoding("utf-8");
157         List<Grade> grade_list = service.list();
158 
159         req.setAttribute("grade_list", grade_list);
160 
161         req.getRequestDispatcher("list.jsp").forward(req, resp);
162     }
163 
164 }

 

 

findservlet.java

1 import java.io.IOException;
 2 import java.util.List;
 3 
 4 import javax.servlet.ServletException;
 5 import javax.servlet.ServletRequest;
 6 import javax.servlet.annotation.WebServlet;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11  
12 
13 /**
14  * Servlet implementation class findservlet
15  */
16 @WebServlet("/findservlet")
17 public class findservlet extends HttpServlet {
18     private static final long serialVersionUID = 1L;
19 
20     service service=new service();
21     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
22         //req是Request(请求)的简写,指的是从jsp/html界面请求回数据,即 jsp/html → 底层
23         //resp是Response(回答)的简写,指的是发送给jsp/html的数据,,即 底层 → jsp/html
24         
25         req.setCharacterEncoding("utf-8");//设置从jsp或HTML中请求数据的值,也就是设置为中文,防止乱码
26         String method=req.getParameter("method");
27         if(method.equals("selectByName1")) {
28             selectByName1(req,resp);
29         }else if(method.equals("selectByName2")) {
30             selectByName2(req,resp);
31         }else if(method.equals("selectByName3")) {
32             selectByName3(req,resp);
33         }
34 }
35         
36     private void selectByName1(HttpServletRequest req,HttpServletResponse resp)throws IOException, ServletException {
37         req.setCharacterEncoding("utf-8");
38         String name=req.getParameter("name");
39         System.out.println(name+"....");
40         Grade user=service.selectByName(name);
41     if(user==null) {
42         req.setAttribute("message", "未找到此人!");
43         req.getRequestDispatcher("find.jsp").forward(req,resp);
44     }else {
45         System.out.println("**"+user.getname());
46         req.setAttribute("user", user);
47         req.getRequestDispatcher("delete.jsp").forward(req,resp);
48     }
49         //?method=list表示传一个叫做method的参数,他的值是list,你可以在Servlet中用request.getParam...获取到。
50     }
51     private void selectByName2(HttpServletRequest req,HttpServletResponse resp)throws IOException, ServletException {
52         req.setCharacterEncoding("utf-8");
53         String name=req.getParameter("name");
54         Grade user=service.selectByName(name);
55         if(user==null) {
56             req.setAttribute("message", "未找到此人!");
57             req.getRequestDispatcher("find.jsp").forward(req,resp);
58         }else {
59         req.setAttribute("user", user);
60         
61         req.getRequestDispatcher("update.jsp").forward(req,resp);
62         }
63         //?method=list表示传一个叫做method的参数,他的值是list,你可以在Servlet中用request.getParam...获取到。
64     }
65     private void selectByName3(HttpServletRequest req,HttpServletResponse resp)throws IOException, ServletException {
66         req.setCharacterEncoding("utf-8");
67         String name=req.getParameter("name");
68         System.out.println("--"+name);
69         Grade user=service.selectByName(name);
70         if(user==null) {
71             req.setAttribute("message", "未找到此人!");
72             req.getRequestDispatcher("find.jsp").forward(req,resp);
73         }else{
74         req.setAttribute("user", user);
75         req.getRequestDispatcher("delete.jsp").forward(req,resp);
76         }
77         //?method=list表示传一个叫做method的参数,他的值是list,你可以在Servlet中用request.getParam...获取到。
78     }
79 }

 


https://www.xamrdz.com/lan/5ur1960356.html

相关文章: