JAVA注册功能步骤

来源:互联网 发布:网络储备人才招聘 编辑:程序博客网 时间:2024/05/21 14:45

注册功能是java开发中的较简单的功能,以下代码为本人在公司练手的项目中注册功能的实现步骤,不足之处请各位大牛多多批评指教。

1.新建User类

public class User {private String uid;private String username;private String password;private String name;private String email;private String telephone;private Date birthday;private String sex;private int state;//是否激活private String code;//激活码public String getUid() {return uid;}public void setUid(String uid) {this.uid = uid;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getTelephone() {return telephone;}public void setTelephone(String telephone) {this.telephone = telephone;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getState() {return state;}public void setState(int state) {this.state = state;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}}

2.新建UserDao类


// 添加用户public int addUser(User user) {QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource());String sql = "insert into user values(?,?,?,?,?,?,?,?,?,?)";try {int update = queryRunner.update(sql, user.getUid(), user.getUsername(), user.getPassword(), user.getName(),user.getEmail(), user.getTelephone(), user.getBirthday(), user.getSex(), user.getState(),user.getCode());return update;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return 0;}

3.UserService类


    public boolean  addUser(User user){int update=userDao.addUser(user);return update>0?true:false;}

4.CommonsUtils 工具类


public class CommonsUtils {//生成uuidpublic static String getUUID(){return UUID.randomUUID().toString();}}

5.RegServlet类

public class RegServlet extends HttpServlet {private static final long serialVersionUID = 1L;    private UserService userService=new UserService();protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   this.doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      request.setCharacterEncoding("UTF-8");  Map<String,String[]> properties= request.getParameterMap();  User user=new User();    try {  //自定义类型转换器  ConvertUtils.register(new Converter(){public Object convert(Class cla, Object value) {  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  try {return sdf.parse(value.toString());} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}  },Date.class);  BeanUtils.populate(user, properties);} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}  user.setTelephone(null);  String activeCode=CommonsUtils.getUUID();  user.setUid(activeCode);  user.setState(0);//是否激活 0:表示未激活  1:表示激活  user.setCode(activeCode);  //是否注册成功  boolean isRegSuccess=  userService.addUser(user);}


原创粉丝点击