1、實體類
package com.edu.empity;
public class People {
private String hubie;
private String livetype;
private String area;
private String roomnum;
private String name;
private String idcard;
private String sex;
private String nation;
private String education;
public String getHubie() {
return hubie;
}
public void setHubie(String hubie) {
this.hubie = hubie;
public String getLivetype() {
return livetype;
public void setLivetype(String livetype) {
this.livetype = livetype;
public String getArea() {
return area;
public void setArea(String area) {
this.area = area;
public String getRoomnum() {
return roomnum;
public void setRoomnum(String roomnum) {
this.roomnum = roomnum;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
public String getIdcard() {
return idcard;
public void setIdcard(String idcard) {
this.idcard = idcard;
public String getSex() {
return sex;
public void setSex(String sex) {
this.sex = sex;
public String getNation() {
return nation;
public void setNation(String nation) {
this.nation = nation;
public String getEducation() {
return education;
public void setEducation(String education) {
this.education = education;
public People(String hubie, String livetype, String area, String roomnum, String name, String idcard, String sex,
String nation, String education) {
super();
public People() {
// TODO 自動生成的構造函數存根
}
2、dao層
package com.edu.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.edu.dbu.DBUtil;
import com.edu.empity.People;
public class Peopledao {
public List<People> getallPeople() {
List<People> list = new ArrayList<People>();
Connection conn = null;
conn = DBUtil.getConnection();
PreparedStatement pst =null;
ResultSet rs = null;
String sql = "select * from people";
try {
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()) {
People people = new People();
people.setHubie(rs.getString("hubie"));
}
} catch (SQLException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
return list;
public boolean Addpeople(People people) throws SQLException {
// TODO 自動生成的方法存根
String sql = "insert into people(hubie,livetype,area,roomnum,name,idcard,sex,nation,education)values(?,?,?,?,?,?,?,?,?)";
Connection conn = DBUtil.getConnection();
int p =0;
pst.setString(1, people.getHubie());
pst.setString(2, people.getLivetype());
pst.setString(3, people.getArea());
pst.setString(4, people.getRoomnum());
pst.setString(5, people.getName());
pst.setString(6, people.getIdcard());
pst.setString(7, people.getSex());
pst.setString(8, people.getNation());
pst.setString(9, people.getEducation());
p = pst.executeUpdate();
pst.close();
conn.close();
}finally {
if(p != 0) {
System.out.println("dao資訊添加成功");
}
return true;
public boolean Updatepeople(People people) throws SQLException {
String sql = "update people set hubie=? livetype=? area=? roomnum=? idcard=? sex=? nation=? education=? where name = ?";
pst.setString(5, people.getIdcard());
pst.setString(6, people.getSex());
pst.setString(7, people.getNation());
pst.setString(8, people.getEducation());
pst.setString(9, people.getName());
System.out.println("dao資訊修改成功");
public boolean Deletepeople(String name) throws SQLException {
String sql = "delete from people where name = ?";
pst.setString(1, name);
System.out.println("dao資訊删除成功");
3、servlet
1)Add
package com.edu.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.edu.dao.Peopledao;
@WebServlet("/Addservlet")
public class Addservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String hubie = request.getParameter("hubie");
String livetype = request.getParameter("livetype");
String area = request.getParameter("area");
String roomnum = request.getParameter("roomnum");
String name = request.getParameter("name");
String idcard = request.getParameter("idcard");
String sex = request.getParameter("sex");
String nation = request.getParameter("nation");
String education = request.getParameter("education");
System.out.println(hubie+livetype+area+roomnum+name+idcard+sex+nation+education);
Peopledao dao = new Peopledao();
People people = new People(hubie,livetype,area,roomnum,name,idcard,sex,nation,education);
boolean flag = false;
flag=dao.Addpeople(people);
if(flag) {
System.out.println("servlet資訊添加成功");
request.getRequestDispatcher("showpeople.jsp").forward(request, response);
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
2)delete
@WebServlet("/Deleteservlet")
public class Deleteservlet extends HttpServlet {
if(name!=null) {
dao.Deletepeople(name);
3)update
@WebServlet("/Updateservlet")
public class Updateservlet extends HttpServlet {
try {
if(name != null) {
dao.Updatepeople(people);
}
} catch (SQLException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();