Java web人口普查系统-java代码
bean层 package 人口普查系统; public class Bean { private int id; private String hubie; private String livetype; private int area; private int roomnum; private String name; private String idcard; private String sex; private String nation; private String education; public int getId() { return id; } public void setId(int id) { this.id = id; } 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 int getArea() { return area; } public void setArea(int area) { this.area = area; } public int getRoomnum() { return roomnum; } public void setRoomnum(int 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 Bean(int id, String hubie, String livetype, int area, int roomnum, String name, String idcard,String sex, String nation, String education) { this.id = id; this.hubie = hubie; this.livetype = livetype; this.area = area; this.roomnum = roomnum; this.name = name; this.idcard = idcard; this.sex = sex; this.nation = nation; this.education = education; } public String toString() { return "Census{" + "id=" + id + ", hubie='" + hubie + '\'' + ", livetype='" + livetype + '\'' + ", area=" + area + ", roomnum=" + roomnum + ", name='" + name + '\'' + ", idcard='" + idcard + '\'' + ", sex='" + sex + '\'' + ", nation='" + nation + '\'' + ", education='" + education + '\'' + '}'; } }
DBUtil//链接数据库层 package 人口普查系统; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { private static String url = "jdbc:mysql://localhost:3306/course?serverTimezone=UTC"; private static String user = "root"; private static String password = "123456"; private static String jdbcName="com.mysql.jdbc.Driver"; private Connection con=null; public static Connection getConnection() { Connection con=null; try { Class.forName(jdbcName); con=DriverManager.getConnection(url, user, password); //System.out.println("鏁版嵁搴撹繛鎺ユ垚鍔�"); } catch (Exception e) { // TODO Auto-generated catch block //System.out.println("鏁版嵁搴撹繛鎺ュけ璐�"); e.printStackTrace(); } try { con = DriverManager.getConnection(url,user,password); System.out.println("杩炴帴鎴愬姛"); } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); } return con; } public static void main(String[] args)throws SQLException { Connection conn = getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; String sql ="select * from person"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); System.out.println(getConnection()); while(rs.next()){ System.out.println("鎴愬姛"); } } // return con; public static void close(Connection con) { if(con!=null) try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(Statement state, Connection conn) { if(state!=null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs, Statement state, Connection conn) { if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(state!=null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
dao层 package 人口普查系统; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class Dao { private DBUtil dbutil=new DBUtil(); public Dao() { // TODO Auto-generated constructor stub } public boolean insert(Bean bean) {//鎻掑叆鏁版嵁鐨勬柟娉� boolean f=false; 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()+"')"; Connection conn=DBUtil.getConnection();//鏁版嵁搴撹繛鎺ワ紝鍔犺浇椹卞姩 Statement state=null; try { state=conn.createStatement();//瀹炰緥鍖朣tatement瀵硅薄,鏂逛究瀵箂ql璇彞杩涜鎿嶄綔 System.out.println(conn); state.executeUpdate(sql); f=true; //鎵ц鏁版嵁搴撴洿鏂版搷浣滅敤浜庢墽琛孖NSERT銆乁PDATE鎴朌ELETE璇彞浠ュ強SQLDDL锛堟暟鎹畾涔夎瑷�锛夎鍙ワ紝 //渚嬪CREATETABLE鍜孌ROPTABLE,锛堝垱寤鸿〃鍜屽垹闄よ〃锛� }catch(Exception e)//褰搕ry璇彞涓璼鍑虹幇寮傚父鏃讹紝浼氭墽琛宑atch涓殑璇彞 { e.printStackTrace();//鎹曡幏寮傚父鐨勮鍙� } finally //finally浣滀负寮傚父澶勭悊鐨勪竴閮ㄥ垎锛屽畠鍙兘鐢ㄥ湪try/catch璇彞涓紝骞朵笖闄勫甫涓�涓鍙ュ潡锛岃〃绀鸿繖娈佃鍙ユ渶缁堜竴瀹氫細琚墽琛岋紙涓嶇鏈夋病鏈夋姏鍑哄紓甯革級锛岀粡甯歌鐢ㄥ湪闇�瑕侀噴鏀捐祫婧愮殑鎯呭喌涓嬨�� { DBUtil.close(conn); } return f; } public boolean delete(int id ) {//鍒犻櫎鏂规硶 String sql="delete from person where id='"+id+"'"; boolean f=false; Connection conn =DBUtil.getConnection(); Statement st=null; try { st=conn.createStatement(); st.executeUpdate(sql); f=true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ DBUtil.close(st, conn); } return f; } public boolean update(Bean bean) {//鏇存柊鏂规硶 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()+"'"; Connection conn=DBUtil.getConnection(); boolean f=false; Statement st=null; try { st=conn.createStatement(); st.executeUpdate(sql); f=true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return f; } public List<Bean> list(){//鏌ヨ鎵�鏈夋柟娉� String sql="select * from person order by id ASC"; Connection conn=DBUtil.getConnection(); Statement st=null; List<Bean> list=new ArrayList<>(); ResultSet rs=null; Bean bean=null; try { st=conn.createStatement(); st.executeQuery(sql); rs=st.executeQuery(sql); while(rs.next()) { int id=rs.getInt("id"); String hubie = rs.getString("hubie"); String livetype = rs.getString("livetype"); int area=rs.getInt("area"); int roomnum=rs.getInt("roomnum"); String name = rs.getString("name"); String idcard=rs.getString("idcard"); String sex = rs.getString("sex"); String nation = rs.getString("nation"); String education = rs.getString("education"); bean=new Bean(id,hubie,livetype,area,roomnum, name, idcard, sex,nation, education); list.add(bean); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(rs, st, conn); } return list; } public List<Bean> searchByName(String str) throws SQLException{//鏌ヨ鏉′欢鏂规硶 String sql="select * from person where(name like '%"+str+"%')"; Connection conn=DBUtil.getConnection(); Statement st=null; PreparedStatement pt = conn.prepareStatement(sql); List<Bean> search=new ArrayList<>(); ResultSet rs=null; Bean bean=null; try { pt=conn.prepareStatement(sql); rs=pt.executeQuery(); while(rs.next()) { int id=rs.getInt("id"); String hubie = rs.getString("hubie"); String livetype = rs.getString("livetype"); int area=rs.getInt("area"); int roomnum=rs.getInt("roomnum"); String name = rs.getString("name"); String idcard=rs.getString("idcard"); String sex = rs.getString("sex"); String nation = rs.getString("nation"); String education = rs.getString("education"); bean=new Bean(id,hubie,livetype,area,roomnum, name, idcard, sex,nation, education); search.add(bean); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(rs, st, conn); } return search; } public List<Bean> searchBySex(String str) throws SQLException{//鏌ヨ鏉′欢鏂规硶 String sql="select * from person where(sex like '%"+str+"%')"; Connection conn=DBUtil.getConnection(); Statement st=null; PreparedStatement pt = conn.prepareStatement(sql); List<Bean> search=new ArrayList<>(); ResultSet rs=null; Bean bean=null; try { pt=conn.prepareStatement(sql); rs=pt.executeQuery(); while(rs.next()) { int id=rs.getInt("id"); String hubie = rs.getString("hubie"); String livetype = rs.getString("livetype"); int area=rs.getInt("area"); int roomnum=rs.getInt("roomnum"); String name = rs.getString("name"); String idcard=rs.getString("idcard"); String sex = rs.getString("sex"); String nation = rs.getString("nation"); String education = rs.getString("education"); bean=new Bean(id,hubie,livetype,area,roomnum, name, idcard, sex,nation, education); search.add(bean); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(rs, st, conn); } return search; } public List<Bean> searchByEducation(String str) throws SQLException{//鏌ヨ鏉′欢鏂规硶 String sql="select * from person where(education like '%"+str+"%')"; Connection conn=DBUtil.getConnection(); Statement st=null; PreparedStatement pt = conn.prepareStatement(sql); List<Bean> search=new ArrayList<>(); ResultSet rs=null; Bean bean=null; try { pt=conn.prepareStatement(sql); rs=pt.executeQuery(); while(rs.next()) { int id=rs.getInt("id"); String hubie = rs.getString("hubie"); String livetype = rs.getString("livetype"); int area=rs.getInt("area"); int roomnum=rs.getInt("roomnum"); String name = rs.getString("name"); String idcard=rs.getString("idcard"); String sex = rs.getString("sex"); String nation = rs.getString("nation"); String education = rs.getString("education"); bean=new Bean(id,hubie,livetype,area,roomnum, name, idcard, sex,nation, education); search.add(bean); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(rs, st, conn); } return search; } public List<Bean> searchByNation(String str) throws SQLException{//鏌ヨ鏉′欢鏂规硶 String sql="select * from person where(nation like '%"+str+"%')"; Connection conn=DBUtil.getConnection(); Statement st=null; PreparedStatement pt = conn.prepareStatement(sql); List<Bean> search=new ArrayList<>(); ResultSet rs=null; Bean bean=null; try { pt=conn.prepareStatement(sql); rs=pt.executeQuery(); while(rs.next()) { int id=rs.getInt("id"); String hubie = rs.getString("hubie"); String livetype = rs.getString("livetype"); int area=rs.getInt("area"); int roomnum=rs.getInt("roomnum"); String name = rs.getString("name"); String idcard=rs.getString("idcard"); String sex = rs.getString("sex"); String nation = rs.getString("nation"); String education = rs.getString("education"); bean=new Bean(id,hubie,livetype,area,roomnum, name, idcard, sex,nation, education); search.add(bean); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(rs, st, conn); } return search; } }
servlet层 package 人口普查系统; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class servlet */ @WebServlet("/servlet") public class servlet extends HttpServlet { Dao dao=new Dao(); private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public servlet() { super(); // TODO Auto-generated constructor stub } private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); int id = Integer.parseInt(request.getParameter("id")); String hubie = request.getParameter("hubie"); String livetype = request.getParameter("livetype"); int area= Integer.parseInt(request.getParameter("area")); int roomnum = Integer.parseInt(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"); Bean bean=new Bean(id,hubie,livetype,area,roomnum,name,idcard,sex,nation,education); dao.update(bean); request.setAttribute("message", "修改成功"); request.getRequestDispatcher("servlet?method=list").forward(request, response); } private void list(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); List<Bean> list = dao.list(); request.setAttribute("list", list); request.getRequestDispatcher("list.jsp").forward(request,response); } private void delete(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); int id=Integer.parseInt(request.getParameter("id")); dao.delete(id); //进行数据库的删除操作 request.setAttribute("message", "删除成功"); request.getRequestDispatcher("servlet?method=list").forward(request, response); } private void insert(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); int id = Integer.parseInt(request.getParameter("id")); String hubie = request.getParameter("hubie"); String livetype = request.getParameter("livetype"); int area= Integer.parseInt(request.getParameter("area")); int roomnum = Integer.parseInt(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"); Bean bean=new Bean(id,hubie,livetype,area,roomnum, name, idcard, sex,nation, education); if(dao.insert(bean)) { request.setAttribute("message", "添加成功"); request.getRequestDispatcher("index.jsp").forward(request, response); } } private void search(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); String cxfs = request.getParameter("cxfs"); System.out.print("cxfs"); request.setAttribute("search", "查询成功"); request.getRequestDispatcher("list.jsp").forward(request, response); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String method=request.getParameter("method"); if("insert".equals(method)) { insert(request,response); } else if("delete".equals(method)) { try { delete(request,response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if("update".equals(method)) { update(request,response); } else if("list".equals(method)) { try { list(request,response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }} else if("search".equals(method)) { try { search(request,response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }