(三十一)web 开发基础项目
1. 编写index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="servlet/login?action=add" method="post"> 11 用户名:<input type="text" name="userName" /><br/> 12 密 码:<input type="password" name="passWd" /><br/> 13 年 龄:<input type="text" name="age" /><br/> 14 <input type="submit" /> 15 </form> 16 </body> 17 </html>
2. 设计数据库,确定po包、dao包、bo包、service包
2.1 设计数据库
2.2 确定po包,存放的是永久化对象,即与数据库的表一一对应。
userPO.java
1 package po; 2 3 import java.util.Date; 4 5 public class UserPO { 6 private String userName; 7 private String passWd; 8 private Date birthday; 9 10 public UserPO(){ 11 } 12 13 public UserPO(String userName, String passWd, Date birthday) { 14 super(); 15 this.userName = userName; 16 this.passWd = passWd; 17 this.birthday = birthday; 18 } 19 20 public String getUserName() { 21 return userName; 22 } 23 24 public void setUserName(String userName) { 25 this.userName = userName; 26 } 27 28 public String getPassWd() { 29 return passWd; 30 } 31 32 public void setPassWd(String passWd) { 33 this.passWd = passWd; 34 } 35 36 public Date getBirthday() { 37 return birthday; 38 } 39 40 public void setBirthday(Date birthday) { 41 this.birthday = birthday; 42 } 46 }
2.3 确定dao包,包里存的是操作数据的对象(对数据库的增删改查),一般有接口+实现+工具类.
- UserDAOI.java
1 package dao; 2 3 import java.util.List; 4 5 import po.UserPO; 6 7 public interface UserDAOI { 8 public boolean addUser(UserPO user); 9 public boolean delUser(String userName); 10 public boolean uptUser(UserPO user); 11 public List<UserPO> gets(); 12 13 }
- DBUtil.java
1 package dao; 2 3 import java.beans.PropertyVetoException; 4 import java.sql.Connection; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.text.DateFormat; 9 import java.util.ArrayList; 10 import java.util.List; 11 12 import com.mchange.v2.c3p0.ComboPooledDataSource; 13 14 import po.UserPO; 15 16 public class DBUtil { 17 public static ComboPooledDataSource dateSource; 18 19 static{ 20 dateSource=new ComboPooledDataSource(); 21 final String URL="jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8"; 22 final String USER="root"; 23 final String PASSWD=""; 24 final String Driver="com.mysql.jdbc.Driver"; 25 26 try { 27 dateSource.setDriverClass(Driver); 28 dateSource.setJdbcUrl(URL); 29 dateSource.setUser(USER); 30 dateSource.setPassword(PASSWD); 31 dateSource.setMaxPoolSize(100); 32 dateSource.setMinPoolSize(10); 33 dateSource.setMaxStatements(10000); 34 } catch (Exception e) { 35 e.printStackTrace(); 36 } 37 38 } 39 40 /** 41 * 42 * @return 连接池中的连接 43 */ 44 public static Connection getConn(){ 45 try { 46 return dateSource.getConnection(); 47 } catch (SQLException e) { 48 e.printStackTrace(); 49 } 50 return null; 51 } 52 53 /** 54 * 55 * 增删改方法 56 * @param sql sql语句 57 * @param list sql语句中的?值 58 * @return 59 */ 60 public static boolean cud(String sql,List<Object> list){ 61 boolean flag=false; 62 Connection conn=null; 63 PreparedStatement pstat=null; 64 try{ 65 conn=getConn(); 66 pstat=conn.prepareStatement(sql); 67 68 if(list!=null){ 69 for(int i=0;i<list.size();i++){ 70 pstat.setObject(i+1, list.get(i)); 71 } 72 } 73 74 int res=pstat.executeUpdate(); 75 if(res>0){ 76 flag=true; 77 } 78 79 80 }catch(Exception e){ 81 e.printStackTrace(); 82 }finally{ 83 close(conn, pstat, null); 84 } 85 86 87 return flag; 88 } 89 90 /** 91 * 关闭流 92 * @param conn 连接流 93 * @param pstat sql语句流 94 * @param res 结果集流 95 */ 96 public static void close(Connection conn,PreparedStatement pstat,ResultSet res){ 97 98 try{ 99 if(conn!=null) 100 { 101 conn.close(); 102 } 103 if(pstat!=null) 104 { 105 pstat.close(); 106 }if(res!=null) 107 { 108 res.close(); 109 } 110 } 111 catch(Exception e) 112 { 113 e.printStackTrace(); 114 } 115 116 117 } 118 119 /**查找数据库中所有po对象并返回list
*@param sql sql语句
*@param list 存放sql预处理语句中的?值
*/ 120 public static List<UserPO> gets(String sql,List<Object> list) { 121 List<UserPO> users=new ArrayList<UserPO>(); 122 UserPO user=null; 123 Connection conn=null; 124 PreparedStatement pstat=null; 125 ResultSet res=null; 126 try 127 { 128 conn=DBUtil.getConn(); 129 pstat=conn.prepareStatement(sql); 130 131 if(list!=null) 132 { 133 for(int i=0;i<list.size();i++) 134 { 135 pstat.setObject(i+1, list.get(i)); 136 } 137 138 } 139 140 141 142 res=pstat.executeQuery(); 143 144 while(res.next()) 145 { 146 user=new UserPO(); 147 user.setUserName(res.getString("userName")); 148 user.setBirthday(res.getDate("birthday")); 149 user.setPassWd(res.getString("passWd")); 150 users.add(user); 151 } 152 153 } 154 catch(Exception e) 155 { 156 e.printStackTrace(); 157 }finally 158 { 159 close(conn, pstat, res); 160 } 161 return users; 162 } 163 164 }
- UserDAOImpl.java
package dao; import java.awt.geom.CubicCurve2D; import java.sql.Connection; import java.sql.PreparedStatement; import java.util.ArrayList; import java.util.List; import po.UserPO; public class UserPOImpl implements UserDAOI { public boolean addUser(UserPO user) { String sql="insert into user values(?,?,?)"; List<Object> list=new ArrayList<Object>(); list.add(user.getUserName()); list.add(user.getPassWd()); list.add(user.getBirthday()); return DBUtil.cud(sql, list); } public boolean delUser(String userName) { String sql="delete from user where userName=?"; List<Object> list=new ArrayList<Object>(); list.add(userName); return DBUtil.cud(sql, list); } public boolean uptUser(UserPO user) { String sql="update user set passWd=?,birthday=? where userName=?"; List<Object> list=new ArrayList<Object>(); list.add(user.getPassWd()); list.add(user.getBirthday()); list.add(user.getUserName()); return DBUtil.cud(sql, list); } public List<UserPO> gets() { String sql="select * from user"; return DBUtil.gets(sql, null); } }
2.4 确定bo包,存放的是对象,即与用户提供的信息一一对应。
- userBO.java
1 package bo; 2 3 public class UserBO { 4 private String userName; 5 private String passWd; 6 private String age; 7 8 public UserBO() 9 { 10 11 } 12 13 public UserBO(String userName, String passWd, String age) { 14 super(); 15 this.userName = userName; 16 this.passWd = passWd; 17 this.age = age; 18 } 19 20 public String getUserName() { 21 return userName; 22 } 23 24 public void setUserName(String userName) { 25 this.userName = userName; 26 } 27 28 public String getPassWd() { 29 return passWd; 30 } 31 32 public void setPassWd(String passWd) { 33 this.passWd = passWd; 34 } 35 36 public String getAge() { 37 return age; 38 } 39 40 public void setAge(String age) { 41 this.age = age; 42 } 43 44 45 }
解析: bo对象与po对象不同点在于: bo对象是业务对象,存放的数据是用户直接提供的,以本题为例,用户数据为userName、passWd、age,bo对象成员属性也为这三个,而
po对象则是存放于数据库中的数据,即用户数据经过处理之后才能存放于数据库中(比如用户注册的age数据,如果数据库中直接存放age毫无意义,因为第二年的时候age+1,数据库管理员还得给每个age+1),po对象存放的数据为userName、passWd、birthday。
2.5 确定service包,将servlet得到的原始数据(即用户数据)转换为DAO包里的数据格式。
- userServiceI.java
1 package service; 2 3 import java.util.List; 4 5 import bo.UserBO; 6 7 public interface UserServiceI { 8 public boolean addUser(UserBO user); 9 public boolean delUser(String userName); 10 public boolean uptUser(UserBO user); 11 public List<UserBO> gets(); 12 }
- userServiceImpl.java
1 package service; 2 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.List; 6 7 import bo.UserBO; 8 import dao.UserPOImpl; 9 import po.UserPO; 10 11 public class UserServiceImpl implements UserServiceI { 12 UserPOImpl im=new UserPOImpl(); 13 14 public boolean addUser(UserBO user) { 15 16 return im.addUser(bo2po(user)); 17 } 18 19 public boolean delUser(String userName) { 20 im.delUser(userName); 21 return true; 22 } 23 24 public boolean uptUser(UserBO user) { 25 26 return im.uptUser(bo2po(user)); 27 } 28 29 public List<UserBO> gets() { 30 31 32 List<UserPO> poList=im.gets(); 33 34 return po2bo(poList); 35 } 36 37 /** 38 * 把BO对象转为PO对象,这个方法只适用于增加和修改操作 39 * 40 * @param user 41 * @return 42 */ 43 private UserPO bo2po(UserBO user) 44 { 45 46 UserPO userpo=new UserPO(); 47 48 Date date=new Date(); 49 @SuppressWarnings("deprecation") 50 int year=date.getYear(); //拿到今年第几年 51 int yearDB=year-Integer.parseInt(user.getAge()); //得到存入数据库的年份 52 @SuppressWarnings("deprecation") 53 Date date1=new Date(yearDB,date.getMonth(),date.getDay()); 54 55 userpo.setBirthday(date1); 56 userpo.setUserName(user.getUserName()); 57 userpo.setPassWd(user.getPassWd()); 58 59 return userpo; 60 } 61 /** 62 * 把存有PO对象的List转为存有BO对象的List 63 * 步骤: 64 * 1. 拿到存有PO对象的list容器之后,进行遍历 65 * 2. 每次遍历都要new一个BO对象,然后把PO对象的值对应转化为BO对象 66 * 3. 返回存放BO对象的list 67 * @param userpo 68 * @return BO对象 69 */ 70 private List<UserBO> po2bo(List<UserPO> poList) 71 { 72 List<UserBO> boList=new ArrayList<UserBO>(); 73 UserBO userbo=null; 74 for(int i=0;i<poList.size();i++) 75 { 76 userbo=new UserBO(); 77 userbo.setUserName(poList.get(i).getUserName()); 78 userbo.setPassWd(poList.get(i).getPassWd()); 79 Date birthday=poList.get(i).getBirthday(); 80 Date getYear=new Date(); 81 int age=getYear.getYear()-birthday.getYear(); 82 userbo.setAge(String.valueOf(age)); 83 84 boList.add(userbo); 85 } 86 87 return boList; 88 } 89 90 }
3. 编写servlet包
- LoginServlet.java
package srevlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bo.UserBO; import service.UserServiceImpl; public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; public LoginServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //拿到用户数据 UserBO user=null; UserServiceImpl userIm=new UserServiceImpl(); String userName=request.getParameter("userName"); String passWd=request.getParameter("passWd"); String age=request.getParameter("age"); String action=request.getParameter("action"); if(action.equals("add")) { //增加一条记录 user=new UserBO(); user.setUserName(userName); user.setPassWd(passWd); user.setAge(age); userIm.addUser(user); } if(action.equals("del")) { //删除一条记录 String name=request.getParameter("userName"); userIm.delUser(name); } if(action.equals("upt")) { user=new UserBO(); user.setAge(request.getParameter("age")); user.setPassWd(request.getParameter("passWd")); user.setUserName(request.getParameter("userName")); userIm.uptUser(user); } if(action.equals("gets")) { //拿到数据库的全部数据 List<UserBO> listbo=userIm.gets(); for(int i=0;i<listbo.size();i++) { System.out.println(listbo.get(i).getUserName()+" "+listbo.get(i).getAge()+" "+listbo.get(i).getPassWd()); } } } }
结果:
解析: 查看servlet 当action=del的时候,会执行servlet的相应操作,request.getParameter("userName"); 可以得到地址栏里的userName的值“nihao”,这样就可以从数据库中删除。