package cn.edu.zucc.personplan.comtrol.example; import cn.edu.zucc.personplan.itf.IUserManager; import cn.edu.zucc.personplan.model.BeanUser; import cn.edu.zucc.personplan.util.BaseException; import java.util.Date; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import cn.edu.zucc.personplan.util.BaseException; import cn.edu.zucc.personplan.util.BusinessException; import cn.edu.zucc.personplan.util.DBUtil; import cn.edu.zucc.personplan.util.DbException; import java.sql.Connection; public class ExampleUserManager implements IUserManager { @Override public BeanUser reg(String userid, String pwd,String pwd2) throws BaseException { // TODO Auto-generated method stub Connection conn = null; try { if (!pwd.equals(pwd2)) { throw new BaseException("密码不一致!!!"); } BeanUser zz=new BeanUser(); zz.setUser_id(userid); zz.setUser_pwd(pwd); zz.setRegister_time(new java.util.Date()); conn = DBUtil.getConnection(); String sql = "insert into tbl_user(user_id,user_pwd,register_time)values('"+ userid+"','"+pwd+"','"+"20200701"+"');"; //System.out.println(sql); java.sql.Statement st = conn.createStatement(); st.execute(sql); } catch (SQLException e){ e.printStackTrace(); throw new DbException(e); } finally { if (conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return null; } @Override public BeanUser login(String userid, String pwd) throws BaseException { // TODO Auto-generated method stub Connection conn = null; try { BeanUser zz=new BeanUser(); zz.setUser_id(userid); conn = DBUtil.getConnection(); String sql = "select user_pwd from tbl_user where user_id = '"+userid+"'"; System.out.println(sql); java.sql.PreparedStatement pst = conn.prepareStatement(sql); java.sql.ResultSet rs = pst.executeQuery(); boolean f=false; while (rs.next()) { f=true; zz.setUser_pwd(rs.getString(1)); } if (f==false) { throw new BaseException("用户不存在"); } if (!pwd.equals(zz.getUser_pwd())) { throw new BaseException("密码错误"); } rs.close(); pst.close(); return zz; } catch (SQLException e){ e.printStackTrace(); throw new DbException(e); } finally { if (conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } //return null; } @Override public void changePwd(BeanUser user, String oldPwd, String newPwd, String newPwd2) throws BaseException { // TODO Auto-generated method stub Connection conn = null; try { conn = DBUtil.getConnection(); String sql = "update tbl_user set user_pwd = '"+newPwd+"' where user_id = '"+user.getUser_id()+"'"; System.out.println(sql); java.sql.Statement st = conn.createStatement(); st.execute(sql); } catch (SQLException e){ e.printStackTrace(); throw new DbException(e); } finally { if (conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }