1. 程式人生 > 實用技巧 >期中考試--人口普查

期中考試--人口普查

第七次全國人口普查登記

1、專案需求:

開展第七次全國人口普查,將為編制“十四五”規劃提供重要資訊支援;推動實現人口與經濟社會、資源環境協調發展,為深化供給側結構性改革,推動經濟高質量發展,建設現代化經濟體系提供強有力的支援;人口普查還將為完善人口發展戰略和政策體系,促進人口長期均衡發展提供重要資訊支援。

2.系統要求與功能設計

2.1 頁面功能要求

1)能夠在Tomcat伺服器中正確部署,並通過瀏覽器檢視;(1分)

2)網站頁面整體風格統一;

1 人口普查系統功能結構圖

3)主頁面:要求顯示人口登記、修改人口資訊、刪除人口資訊、查詢人口資訊,人口資訊瀏覽五個子選單。(1分)

4)人口登記頁面:(6分)

①完成新增人口資訊登記,基本資訊和填報限制如下表所示

戶 別

家庭戶 集體戶(單選框)

住房型別

家庭住宅、集體住所、工作地住所、其他住宅、無住宅(單選框)

本戶現住房面積

XXXXX平方米(文字框,只能輸入這個整數)

本戶現住房間數

XXXXX間(文字框,只能輸入這個整數)

戶主姓名

XXXXX(文字框)

身份證號碼

XXXXXXXXXXXXXXXXXX(判斷身份證位數13位並且只能錄入數字、最後一位可以錄入X)

性別

男/女(單選框)

民族

(文字框)

受教育程度

研究生、大學本科、大學專科、高中、初中、小學、未上過學(下拉框實現)

點選“提交”按鈕,儲存成功則跳轉到人口基本資訊瀏覽介面,新錄入的資訊置頂顯示。失敗則提示錯誤資訊,返回當前頁面

評分標準:

①完成人口登記頁面(未完成0分,完成1分)

②儲存人口資訊入庫(未完成0分,完成1分)

③戶別、住房類別、性別實現一個單選框錄入0.5分,全部實現1分。(未完成0分,完成一個0.5,全部完成1分)

④本戶現住房面積或本戶現住房間數實現整數判斷(未完成0分,完成0.5分)

⑤判斷身份證按照要求錄入,實現位數和數字錄入判斷0.5分,實現最後一位可以輸入數字或X判斷0.5分。(1分)

⑥受教育程度下拉框錄入(未完成0分,完成0.5分)

⑦提交後頁面跳轉功能;(未完成0分,完成1分)

5)修改人口資訊頁面:3分)

輸入戶主姓名,顯示其餘資訊,可對身份證號碼、性別、民族、受教育程度進行修改。(身份證號碼、性別、受教育程度必須符合錄入要求);如果該名戶主資訊資料庫不存在,則提示“該戶主資訊不存在”。(3分)

評分標準:

①完成修改戶主個人資訊頁面(未完成0分,完成0.5分)

② 實現資料庫中資訊更新(未完成0分,完成1分)

③修改資訊判斷是否符合要求。(未完成0分,完成0.5分)

④輸入姓名,顯示其餘資訊,若該資訊不存在,提示錯誤資訊;(未完成0分,完成1分)

6)刪除人口資訊頁面:錄入戶主姓名,顯示詳細資訊後,點選“刪除”按鈕,彈出提示框,提示“是否確認刪除該戶主資訊”,確認後刪除該資訊。(1分)

評分標準:

①輸入戶主姓名可顯示其餘資訊。(未完成0分,完成0.5分)

②對應刪除資料庫中資訊(未完成0分,完成0.5分)

7)瀏覽人口資訊頁面:(2分)

以列表形式顯示人口基本資訊,結果列表中顯示姓名、性別,民族、受教育程度基本資訊,點選姓名,可以跳轉到個人詳細資訊。

①實現以列表形式顯示人口基本資訊,結果列表中顯示姓名、性別,民族、受教育程度基本資訊(未完成0分,完成1分)

②實現跳轉個人詳細資訊頁面。(未完成0分,完成1分)

8)查詢人口資訊頁面:(3分)

要求可以根據人口姓名、性別、民族、受教育程度四種條件實現模糊查詢,輸出結果以列表形式顯示,顯示人口姓名、性別、民族、受教育程度基本資訊,點選列表中的姓名,跳轉到人口個人詳細資訊頁面。

評分標準:

①缺少一種查詢條件扣除1分。(未完成0分,完成3分)

2.2 功能要求

1)設計出合理的資料庫和資料表,要求使用mysql、sqlserver、oracle三種資料庫中一種(1分)

2)使用Serverlet實現頁面互動(1分)。

3)使用JavaBean封裝資料庫連線操作(1分。)

程式碼如下

  1 import java.sql.Connection;
  2 import java.sql.PreparedStatement;
  3 import java.sql.ResultSet;
  4 import java.sql.SQLException;
  5 import java.sql.Statement;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 
  9 public class Dao {//dao層
 10     private DBUtil dbutil = new DBUtil();
 11 
 12 
 13     public Dao() {
 14         // TODO Auto-generated constructor stub
 15     }
 16 
 17     public boolean insert(Bean bean) {//插入資料的方法
 18         boolean f = false;
 19         String sql = "insert into person(id,hubie,livetype,area,roomnum, name, idcard, sex,nation, education) values('" + bean.getId() + "','" + bean.getHubie() + "','" + bean.getLivetype() + "','" + bean.getArea() + "','" + bean.getRoomnum() + "','" + bean.getName() + "','" + bean.getIdcard() + "','" + bean.getSex() + "','" + bean.getNation() + "','" + bean.getEducation() + "')";
 20         Connection conn = DBUtil.getConnection();//資料庫連線,載入驅動
 21         Statement state = null;
 22         try {
 23             state = conn.createStatement();//例項化Statement物件,方便對sql語句進行操作
 24             System.out.println(conn);
 25             state.executeUpdate(sql);
 26             f = true;
 27             //執行資料庫更新操作用於執行INSERT、UPDATE或DELETE語句以及SQLDDL(資料定義語言)語句,
 28             //例如CREATETABLE和DROPTABLE,(建立表和刪除表)
 29         } catch (Exception e)//當try語句中s出現異常時,會執行catch中的語句
 30         {
 31             e.printStackTrace();//捕獲異常的語句
 32         } finally //finally作為異常處理的一部分,它只能用在try/catch語句中,並且附帶一個語句塊,表示這段語句最終一定會被執行(不管有沒有丟擲異常),經常被用在需要釋放資源的情況下。
 33         {
 34             DBUtil.close(conn);
 35         }
 36         return f;
 37     }
 38 
 39     public boolean delete(int id) {//刪除方法
 40         String sql = "delete from person where id='" + id + "'";
 41         boolean f = false;
 42         Connection conn = DBUtil.getConnection();
 43         Statement st = null;
 44         try {
 45             st = conn.createStatement();
 46             st.executeUpdate(sql);
 47             f = true;
 48         } catch (SQLException e) {
 49             // TODO Auto-generated catch block
 50             e.printStackTrace();
 51         } finally {
 52             DBUtil.close(st, conn);
 53         }
 54         return f;
 55     }
 56 
 57     public boolean update(Bean bean) {//更新方法
 58         String sql = "update person set hubie='" + bean.getHubie() + "',livetype='" + bean.getLivetype() + "',area='" + bean.getArea() + "',roomnum='" + bean.getRoomnum() + "',name='" + bean.getName() + "',idcard='" + bean.getIdcard() + "',sex='" + bean.getSex() + "',nation='" + bean.getNation() + "',education='" + bean.getEducation() + "'where id='" + bean.getId() + "'";
 59         Connection conn = DBUtil.getConnection();
 60         boolean f = false;
 61         Statement st = null;
 62         try {
 63             st = conn.createStatement();
 64             st.executeUpdate(sql);
 65             f = true;
 66         } catch (SQLException e) {
 67             // TODO Auto-generated catch block
 68             e.printStackTrace();
 69         }
 70         return f;
 71     }
 72 
 73     public List<Bean> list() {//查詢所有方法
 74         String sql = "select * from person order by id ASC";
 75         Connection conn = DBUtil.getConnection();
 76         Statement st = null;
 77         List<Bean> list = new ArrayList<>();
 78         ResultSet rs = null;
 79         Bean bean = null;
 80         try {
 81             st = conn.createStatement();
 82             st.executeQuery(sql);
 83             rs = st.executeQuery(sql);
 84             while (rs.next()) {
 85                 int id = rs.getInt("id");
 86                 String hubie = rs.getString("hubie");
 87                 String livetype = rs.getString("livetype");
 88                 int area = rs.getInt("area");
 89                 int roomnum = rs.getInt("roomnum");
 90                 String name = rs.getString("name");
 91                 String idcard = rs.getString("idcard");
 92                 String sex = rs.getString("sex");
 93                 String nation = rs.getString("nation");
 94                 String education = rs.getString("education");
 95                 bean = new Bean(id, hubie, livetype, area, roomnum, name, idcard, sex, nation, education);
 96                 list.add(bean);
 97             }
 98         } catch (SQLException e) {
 99             // TODO Auto-generated catch block
100             e.printStackTrace();
101         } finally {
102             DBUtil.close(rs, st, conn);
103         }
104         return list;
105     }
106 
107 
108     public List<Bean> searchByName(String str) throws SQLException {//查詢條件方法
109         String sql = "select * from person where(name like '%" + str + "%')";
110         Connection conn = DBUtil.getConnection();
111         Statement st = null;
112         PreparedStatement pt = conn.prepareStatement(sql);
113         List<Bean> search = new ArrayList<>();
114         ResultSet rs = null;
115         Bean bean = null;
116         try {
117             pt = conn.prepareStatement(sql);
118             rs = pt.executeQuery();
119             while (rs.next()) {
120                 int id = rs.getInt("id");
121                 String hubie = rs.getString("hubie");
122                 String livetype = rs.getString("livetype");
123                 int area = rs.getInt("area");
124                 int roomnum = rs.getInt("roomnum");
125                 String name = rs.getString("name");
126                 String idcard = rs.getString("idcard");
127                 String sex = rs.getString("sex");
128                 String nation = rs.getString("nation");
129                 String education = rs.getString("education");
130                 bean = new Bean(id, hubie, livetype, area, roomnum, name, idcard, sex, nation, education);
131                 search.add(bean);
132             }
133         } catch (SQLException e) {
134             // TODO Auto-generated catch block
135             e.printStackTrace();
136         } finally {
137             DBUtil.close(rs, st, conn);
138         }
139         return search;
140     }
141 
142     public List<Bean> searchBySex(String str) throws SQLException {//查詢條件方法
143         String sql = "select * from person where(sex like '%" + str + "%')";
144         Connection conn = DBUtil.getConnection();
145         Statement st = null;
146         PreparedStatement pt = conn.prepareStatement(sql);
147         List<Bean> search = new ArrayList<>();
148         ResultSet rs = null;
149         Bean bean = null;
150         try {
151             pt = conn.prepareStatement(sql);
152             rs = pt.executeQuery();
153             while (rs.next()) {
154                 int id = rs.getInt("id");
155                 String hubie = rs.getString("hubie");
156                 String livetype = rs.getString("livetype");
157                 int area = rs.getInt("area");
158                 int roomnum = rs.getInt("roomnum");
159                 String name = rs.getString("name");
160                 String idcard = rs.getString("idcard");
161                 String sex = rs.getString("sex");
162                 String nation = rs.getString("nation");
163                 String education = rs.getString("education");
164                 bean = new Bean(id, hubie, livetype, area, roomnum, name, idcard, sex, nation, education);
165                 search.add(bean);
166             }
167         } catch (SQLException e) {
168             // TODO Auto-generated catch block
169             e.printStackTrace();
170         } finally {
171             DBUtil.close(rs, st, conn);
172         }
173         return search;
174     }
175 
176     public List<Bean> searchByEducation(String str) throws SQLException {//查詢條件方法
177         String sql = "select * from person where(education like '%" + str + "%')";
178         Connection conn = DBUtil.getConnection();
179         Statement st = null;
180         PreparedStatement pt = conn.prepareStatement(sql);
181         List<Bean> search = new ArrayList<>();
182         ResultSet rs = null;
183         Bean bean = null;
184         try {
185             pt = conn.prepareStatement(sql);
186             rs = pt.executeQuery();
187             while (rs.next()) {
188                 int id = rs.getInt("id");
189                 String hubie = rs.getString("hubie");
190                 String livetype = rs.getString("livetype");
191                 int area = rs.getInt("area");
192                 int roomnum = rs.getInt("roomnum");
193                 String name = rs.getString("name");
194                 String idcard = rs.getString("idcard");
195                 String sex = rs.getString("sex");
196                 String nation = rs.getString("nation");
197                 String education = rs.getString("education");
198                 bean = new Bean(id, hubie, livetype, area, roomnum, name, idcard, sex, nation, education);
199                 search.add(bean);
200             }
201         } catch (SQLException e) {
202             // TODO Auto-generated catch block
203             e.printStackTrace();
204         } finally {
205             DBUtil.close(rs, st, conn);
206         }
207         return search;
208     }
209 
210     public List<Bean> searchByNation(String str) throws SQLException {//查詢條件方法
211         String sql = "select * from person where(nation like '%" + str + "%')";
212         Connection conn = DBUtil.getConnection();
213         Statement st = null;
214         PreparedStatement pt = conn.prepareStatement(sql);
215         List<Bean> search = new ArrayList<>();
216         ResultSet rs = null;
217         Bean bean = null;
218         try {
219             pt = conn.prepareStatement(sql);
220             rs = pt.executeQuery();
221             while (rs.next()) {
222                 int id = rs.getInt("id");
223                 String hubie = rs.getString("hubie");
224                 String livetype = rs.getString("livetype");
225                 int area = rs.getInt("area");
226                 int roomnum = rs.getInt("roomnum");
227                 String name = rs.getString("name");
228                 String idcard = rs.getString("idcard");
229                 String sex = rs.getString("sex");
230                 String nation = rs.getString("nation");
231                 String education = rs.getString("education");
232                 bean = new Bean(id, hubie, livetype, area, roomnum, name, idcard, sex, nation, education);
233                 search.add(bean);
234             }
235         } catch (SQLException e) {
236             // TODO Auto-generated catch block
237             e.printStackTrace();
238         } finally {
239             DBUtil.close(rs, st, conn);
240         }
241         return search;
242     }
243 
244 
245 }
246 
247 
248 
249 
250 import static org.junit.Assert.*;
251 
252 import java.sql.SQLException;
253 
254 import org.junit.Test;
255 import 人口普查系統.Dao;
256 
257 public class DaoTest {
258 
259     @Test
260     public void test() throws SQLException {
261         Dao dao=new Dao();
262         System.out.print(dao.searchByName("2").size());
263     }
264 }
Dao
  1 package 人口普查系統;
  2 
  3 public class Bean {
  4     private int id;
  5     private String hubie;
  6     private String livetype;
  7     private int area;
  8     private int roomnum;
  9     private String name;
 10     private String idcard;
 11     private String sex;
 12     private String nation;
 13     private String education;
 14 
 15     public int getId() {
 16         return id;
 17     }
 18 
 19     public void setId(int id) {
 20         this.id = id;
 21     }
 22 
 23     public String getHubie() {
 24         return hubie;
 25     }
 26 
 27     public void setHubie(String hubie) {
 28         this.hubie = hubie;
 29     }
 30 
 31     public String getLivetype() {
 32         return livetype;
 33     }
 34 
 35     public void setLivetype(String livetype) {
 36         this.livetype = livetype;
 37     }
 38 
 39     public int getArea() {
 40         return area;
 41     }
 42 
 43     public void setArea(int area) {
 44         this.area = area;
 45     }
 46 
 47     public int getRoomnum() {
 48         return roomnum;
 49     }
 50 
 51     public void setRoomnum(int roomnum) {
 52         this.roomnum = roomnum;
 53     }
 54 
 55     public String getName() {
 56         return name;
 57     }
 58 
 59     public void setName(String name) {
 60         this.name = name;
 61     }
 62 
 63     public String getIdcard() {
 64         return idcard;
 65     }
 66 
 67     public void setIdcard(String idcard) {
 68         this.idcard = idcard;
 69     }
 70 
 71     public String getSex() {
 72         return sex;
 73     }
 74 
 75     public void setSex(String sex) {
 76         this.sex = sex;
 77     }
 78 
 79     public String getNation() {
 80         return nation;
 81     }
 82 
 83     public void setNation(String nation) {
 84         this.nation = nation;
 85     }
 86 
 87     public String getEducation() {
 88         return education;
 89     }
 90 
 91     public void setEducation(String education) {
 92         this.education = education;
 93     }
 94 
 95     public Bean(int id, String hubie, String livetype, int area, int roomnum, String name, String idcard, String sex, String nation, String education) {
 96         this.id = id;
 97         this.hubie = hubie;
 98         this.livetype = livetype;
 99         this.area = area;
100         this.roomnum = roomnum;
101         this.name = name;
102         this.idcard = idcard;
103         this.sex = sex;
104         this.nation = nation;
105         this.education = education;
106     }
107 
108     public String toString() {
109         return "Census{" +
110                 "id=" + id +
111                 ", hubie='" + hubie + '\'' +
112                 ", livetype='" + livetype + '\'' +
113                 ", area=" + area +
114                 ", roomnum=" + roomnum +
115                 ", name='" + name + '\'' +
116                 ", idcard='" + idcard + '\'' +
117                 ", sex='" + sex + '\'' +
118                 ", nation='" + nation + '\'' +
119                 ", education='" + education + '\'' +
120                 '}';
121     }
122 }
Bean
  1 package 人口普查系統;
  2 
  3 import java.sql.Connection;
  4 import java.sql.DriverManager;
  5 import java.sql.PreparedStatement;
  6 import java.sql.ResultSet;
  7 import java.sql.SQLException;
  8 import java.sql.Statement;
  9 
 10 
 11 public class DBUtil {
 12     private static String url = "jdbc:mysql://localhost:3306/course?useUnicode=true&characterEncoding=utf8";
 13     private static String user = "root";
 14     private static String password = "15732767817lxy11";
 15     private static String jdbcName = "com.mysql.jdbc.Driver";
 16     private Connection con = null;
 17 
 18     public static Connection getConnection() {
 19         Connection con = null;
 20         try {
 21             Class.forName(jdbcName);
 22             con = DriverManager.getConnection(url, user, password);
 23             //System.out.println("資料庫連線成功");
 24         } catch (Exception e) {
 25             // TODO Auto-generated catch block
 26             //System.out.println("資料庫連線失敗");
 27             e.printStackTrace();
 28         }
 29         try {
 30             con = DriverManager.getConnection(url, user, password);
 31             System.out.println("連線成功");
 32 
 33 
 34         } catch (SQLException e) {
 35             // TODO: handle exception
 36             e.printStackTrace();
 37         }
 38         return con;
 39     }
 40 
 41     public static void main(String[] args) throws SQLException {
 42         Connection conn = getConnection();
 43         PreparedStatement pstmt = null;
 44         ResultSet rs = null;
 45         String sql = "select * from person";
 46         pstmt = conn.prepareStatement(sql);
 47         rs = pstmt.executeQuery();
 48         System.out.println(getConnection());
 49         while (rs.next()) {
 50             System.out.println("成功");
 51         }
 52 
 53     }
 54 
 55     // return con;
 56 
 57 
 58     public static void close(Connection con) {
 59         if (con != null)
 60             try {
 61                 con.close();
 62             } catch (SQLException e) {
 63                 // TODO Auto-generated catch block
 64                 e.printStackTrace();
 65             }
 66 
 67     }
 68 
 69     public static void close(Statement state, Connection conn) {
 70         if (state != null) {
 71             try {
 72                 state.close();
 73             } catch (SQLException e) {
 74                 e.printStackTrace();
 75             }
 76         }
 77         if (conn != null) {
 78             try {
 79                 conn.close();
 80             } catch (SQLException e) {
 81                 e.printStackTrace();
 82             }
 83         }
 84     }
 85 
 86     public static void close(ResultSet rs, Statement state, Connection conn) {
 87         if (rs != null) {
 88             try {
 89                 rs.close();
 90             } catch (SQLException e) {
 91                 e.printStackTrace();
 92             }
 93         }
 94         if (state != null) {
 95             try {
 96                 state.close();
 97             } catch (SQLException e) {
 98                 e.printStackTrace();
 99             }
100         }
101         if (conn != null) {
102             try {
103                 conn.close();
104             } catch (SQLException e) {
105                 e.printStackTrace();
106             }
107         }
108     }
109 
110 }
DRUtil
  1 import java.io.IOException;
  2 import java.io.UnsupportedEncodingException;
  3 import java.util.List;
  4 
  5 import javax.servlet.ServletException;
  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  * Servlet implementation class servlet
 14  */
 15 @WebServlet("/servlet")
 16 public class servlet extends HttpServlet {
 17     Dao dao = new Dao();
 18     private static final long serialVersionUID = 1L;
 19 
 20     /**
 21      * @see HttpServlet#HttpServlet()
 22      */
 23     public servlet() {
 24         super();
 25         // TODO Auto-generated constructor stub
 26     }
 27 
 28 
 29     private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 30         // TODO Auto-generated method stub
 31         request.setCharacterEncoding("utf-8");
 32         int id = Integer.parseInt(request.getParameter("id"));
 33         String hubie = request.getParameter("hubie");
 34         String livetype = request.getParameter("livetype");
 35         int area = Integer.parseInt(request.getParameter("area"));
 36         int roomnum = Integer.parseInt(request.getParameter("roomnum"));
 37         String name = request.getParameter("name");
 38         String idcard = request.getParameter("idcard");
 39         String sex = request.getParameter("sex");
 40         String nation = request.getParameter("nation");
 41         String education = request.getParameter("education");
 42         Bean bean = new Bean(id, hubie, livetype, area, roomnum, name, idcard, sex, nation, education);
 43         dao.update(bean);
 44         request.setAttribute("message", "修改成功");
 45         request.getRequestDispatcher("servlet?method=list").forward(request, response);
 46     }
 47 
 48     private void list(HttpServletRequest request, HttpServletResponse response) throws Exception {
 49         // TODO Auto-generated method stub
 50         request.setCharacterEncoding("utf-8");
 51         List<Bean> list = dao.list();
 52         request.setAttribute("list", list);
 53         request.getRequestDispatcher("list.jsp").forward(request, response);
 54     }
 55 
 56     private void delete(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {
 57         // TODO Auto-generated method stub
 58         request.setCharacterEncoding("UTF-8");
 59         int id = Integer.parseInt(request.getParameter("id"));
 60         dao.delete(id); //進行資料庫的刪除操作
 61         request.setAttribute("message", "刪除成功");
 62         request.getRequestDispatcher("servlet?method=list").forward(request, response);
 63     }
 64 
 65 
 66     private void insert(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
 67         // TODO Auto-generated method stub
 68         request.setCharacterEncoding("utf-8");
 69         int id = Integer.parseInt(request.getParameter("id"));
 70         String hubie = request.getParameter("hubie");
 71         String livetype = request.getParameter("livetype");
 72         int area = Integer.parseInt(request.getParameter("area"));
 73         int roomnum = Integer.parseInt(request.getParameter("roomnum"));
 74         String name = request.getParameter("name");
 75         String idcard = request.getParameter("idcard");
 76         String sex = request.getParameter("sex");
 77         String nation = request.getParameter("nation");
 78         String education = request.getParameter("education");
 79         Bean bean = new Bean(id, hubie, livetype, area, roomnum, name, idcard, sex, nation, education);
 80 
 81         if (dao.insert(bean)) {
 82             request.setAttribute("message", "新增成功");
 83             request.getRequestDispatcher("index.jsp").forward(request, response);
 84         }
 85     }
 86 
 87 
 88     private void search(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {
 89         // TODO Auto-generated method stub
 90         request.setCharacterEncoding("UTF-8");
 91         String cxfs = request.getParameter("cxfs");
 92         System.out.print("cxfs");
 93         request.setAttribute("search", "查詢成功");
 94         request.getRequestDispatcher("list.jsp").forward(request, response);
 95     }
 96 
 97 
 98     /**
 99      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
100      */
101     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
102         // TODO Auto-generated method stub
103         request.setCharacterEncoding("utf-8");
104         String method = request.getParameter("method");
105         if ("insert".equals(method)) {
106             insert(request, response);
107         } else if ("delete".equals(method)) {
108             try {
109                 delete(request, response);
110             } catch (Exception e) {
111                 // TODO Auto-generated catch block
112                 e.printStackTrace();
113             }
114 
115         } else if ("update".equals(method)) {
116             update(request, response);
117         } else if ("list".equals(method)) {
118             try {
119                 list(request, response);
120             } catch (Exception e) {
121                 // TODO Auto-generated catch block
122                 e.printStackTrace();
123             }
124         } else if ("search".equals(method)) {
125             try {
126                 search(request, response);
127             } catch (Exception e) {
128                 // TODO Auto-generated catch block
129                 e.printStackTrace();
130             }
131         }
132 
133     }
134 
135     /**
136      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
137      */
138     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
139         // TODO Auto-generated method stub
140         doGet(request, response);
141 
142     }
143 
144 }
145 
146 
147 
148 import java.io.IOException;
149 import java.sql.SQLException;
150 import java.util.List;
151 
152 import javax.servlet.ServletException;
153 import javax.servlet.annotation.WebServlet;
154 import javax.servlet.http.HttpServlet;
155 import javax.servlet.http.HttpServletRequest;
156 import javax.servlet.http.HttpServletResponse;
157 
158 /**
159  * Servlet implementation class searchServlet
160  */
161 @WebServlet("/searchServlet")
162 public class searchServlet extends HttpServlet {
163     private static final long serialVersionUID = 1L;
164        
165     /**
166      * @see HttpServlet#HttpServlet()
167      */
168     public searchServlet() {
169         super();
170         // TODO Auto-generated constructor stub
171     }
172 
173     /**
174      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
175      */
176     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
177         // TODO Auto-generated method stub
178         //response.getWriter().append("Served at: ").append(request.getContextPath());
179         request.setCharacterEncoding("utf-8");
180          response.setCharacterEncoding("utf-8");
181          String cxfs=request.getParameter("cxfs");
182          System.out.print(cxfs);
183 
184          String str=request.getParameter("value");
185          Dao dao=new Dao();
186          List<Bean> list = null;
187         
188              try {
189                  if("1".equals(cxfs))
190                  {
191                 list=dao.searchByName(str);
192                 } 
193                  if("2".equals(cxfs))
194                  {
195                 list=dao.searchBySex(str);
196                 } 
197                  if("3".equals(cxfs))
198                  {
199                 list=dao.searchByEducation(str);
200                 } 
201                  if("4".equals(cxfs))
202                  {
203                 list=dao.searchByNation(str);
204                 } 
205                 
206                  
207             } catch (SQLException e) {
208                 // TODO 自動生成的 catch 塊
209                 e.printStackTrace();
210             }
211        request.setAttribute("list", list);
212     request.getRequestDispatcher("list.jsp").forward(request,response);
213          System.out.print(list.size());
214     }    
215 
216     /**
217      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
218      */
219     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
220         // TODO Auto-generated method stub
221         doGet(request, response);
222     }
223 
224 }
Servlet