public class UserServlet extends HttpServlet{
public voiddoGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
doPost(request, response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
String fun =request.getParameter(“fun”);
//to 新增
else if (“toInsert”.equals(fun)) {
toInsert(request, response);
}
//新增
else if(“insert”.equals(fun)) {
insert(request, response);
}
public void toInsert(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
//添加用户,应该先把用户组查出来,作为下拉框
//查询用户组下拉框
IRoleService roleService = new RoleServiceImpl();
List listRole =roleService.findAll();
request.setAttribute(“listRole”,listRole);
//转发
request.getRequestDispatcher("/jsp/user/user_insert.jsp").forward(request,response);
}
public void insert(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取页面数据
String name =request.getParameter(“name”);
String password =request.getParameter(“password”);
String sex =request.getParameter(“sex”);
String age = request.getParameter(“age”);
String phone =request.getParameter(“phone”);
String address =request.getParameter(“address”);
String rid =request.getParameter(“rid”);
//使用MD5加密
password = MD5Util.getMD5(password);
UserPo user = new UserPo();
user.setName(name);
user.setPassword(password);
if (Tools.isNum(sex)) {
user.setSex(Integer.parseInt(sex));
}
if (Tools.isNum(age)) {
user.setAge(Integer.parseInt(age));
}
user.setPhone(phone);
user.setAddress(address);
if (Tools.isNum(rid)) {
user.setRid(Integer.parseInt(rid));
}
IUserService userService = new UserSerivceImpl();
boolean success = userService.insert(user);//调用insert
//是否成功
if (success) {
request.getSession().setAttribute(“strMsg”,“新增成功”);//strMsg提示
} else {
request.getSession().setAttribute(“strMsg”,“新增失败”);
}
response.sendRedirect(“UserServlet?fun=list”);
}
}
}
@Override
public boolean insert(UserPo t) {
// TODO Auto-generated method stub
boolean returnA = false;
try {
conn=DbUtil.getConnection();
ps = conn.prepareStatement(insert);
//设置值
//注意按照表格的序列号设置值,不然会导致表格的值不一致
ps.setString(1, t.getName());
ps.setString(2, t.getPassword());
ps.setInt(3, t.getRid());
ps.setInt(4, t.getSex());
ps.setInt(5, t.getAge());
ps.setString(6, t.getPhone());
ps.setString(7, t.getAddress());
if (ps.executeUpdate()>0) {
//新增一条返回true
returnA=true;
}
} catch (Exception e) {
// TODO: handle exception
}
finally{
DbUtil.close(conn,ps, rs);
}
return returnA;
}
//新增返回id
@Override
public int insertReturnId(UserPo t) {
// TODO Auto-generated method stub
int inta = 0;
try {
conn = DbUtil.getConnection();
ps =conn.prepareStatement(insert);
//注意按照表格的序列号设置值,不然会导致表格的值不一致
ps.setString(1,t.getName());
ps.setString(2,t.getPassword());
ps.setInt(3, t.getRid());
ps.setInt(4, t.getAge());
ps.setInt(5, t.getSex());
ps.setString(6,t.getPhone());
ps.setString(7,t.getAddress());
if (ps.executeUpdate()>0) {
//查询结果集去获取Key(获取新增成功的Key或者id)
rs=ps.getGeneratedKeys();
while (rs.next()) {
inta =rs.getInt(1);
}
}
} catch (Exception e) {
// TODO: handle exception
}
finally
{
DbUtil.close(conn,ps, rs);
}
return inta;
}
2.新增页面数据
3.新增成功数据