public void userReg(HttpServletRequest req,HttpServletResponse res) { String id=String.valueOf(new Date().getTime()); String loginname=req.getParameter("loginname"); String loginpw=req.getParameter("loginpw"); String name=req.getParameter("name"); String sex=req.getParameter("sex"); String age=req.getParameter("age"); String address=req.getParameter("address"); String tel=req.getParameter("tel"); String email=req.getParameter("email"); String qq=req.getParameter("qq"); String del="no"; String sql="insert into t_user values(?,?,?,?,?,?,?,?,?,?,?)"; Object[] params={id,loginname,loginpw,name,sex,age,address,tel,email,qq,del}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); String targetURL = "/common/add_success.jsp"; dispatch(targetURL, req, res); }修改为封装对象形式
时间: 2024-04-19 19:29:36 浏览: 120
public void userReg(HttpServletRequest req, HttpServletResponse res) {
// 创建 User 对象并设置属性值
User user = new User();
user.setId(String.valueOf(new Date().getTime()));
user.setLoginname(req.getParameter("loginname"));
user.setLoginpw(req.getParameter("loginpw"));
user.setName(req.getParameter("name"));
user.setSex(req.getParameter("sex"));
user.setAge(req.getParameter("age"));
user.setAddress(req.getParameter("address"));
user.setTel(req.getParameter("tel"));
user.setEmail(req.getParameter("email"));
user.setQq(req.getParameter("qq"));
user.setDel("no");
// 将 User 对象插入数据库
String sql = "insert into t_user values(?,?,?,?,?,?,?,?,?,?,?)";
Object[] params = {user.getId(), user.getLoginname(), user.getLoginpw(), user.getName(), user.getSex(), user.getAge(), user.getAddress(), user.getTel(), user.getEmail(), user.getQq(), user.getDel()};
DB mydb = new DB();
mydb.doPstm(sql, params);
mydb.closed();
String targetURL = "/common/add_success.jsp";
dispatch(targetURL, req, res);
}
阅读全文