当前位置: 代码迷 >> Java Web开发 >> MVC实现注册功能的有关问题
  详细解决方案

MVC实现注册功能的有关问题

热度:90   发布时间:2016-04-13 22:11:32.0
MVC实现注册功能的问题
一个简单的注册模块,注册的时候表面看起来一切正常,控制台也没报错,可数据库里面就是没写入注册的数据。。。跪求大神给看看


<form action="${pageContext.request.contextPath }/servlet/regServlet" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password" /><br/>
油箱:<input type="text" name="email"/><br/>
生日:<input type="text" name="birthday"/><br/>
<input type="submit" value="注册"/>
</form>

User user = new User();
//把字符串转换成日期
try {
ConvertUtils.register(new DateLocaleConverter(), Date.class);//将字符串转换成日期
BeanUtils.populate(user, request.getParameterMap());//此方法默认会把八种数据类型自动转换
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//调用业务逻辑
UserService us = new UserServiceImpl();
try {
us.register(user);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//分发转向
out.print("注册成功!2秒后跳到首页");
response.setHeader("refresh", "2;url='"+request.getContextPath()+"/index.jsp'");
}


//用户注册
public void insert(User user)throws Exception {
Connection conn = null;
PreparedStatement ps = null;

try {
conn = DBUtils.getConnection();
String sql = "INSERT INTO users VALUES(null,?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
ps.setString(3, user.getEmail());
ps.setDate(4,new Date(user.getBirthday().getTime()));
} catch (Exception e) {
throw new RuntimeException();
}finally{
DBUtils.closeAll(null, ps, conn);
}

}



public class UserServiceImpl implements UserService {

UserDao userDao = new UserDaoImpl();
public void register(User user) throws UserException {
try {
userDao.insert(user);
} catch (Exception e) {
//写入日志
throw new UserException("注册失败!");
}
}

public interface UserService {
/**
 * 用户注册
 * @param user
 * @throws UserException
 */
public void register(User user) throws UserException;
}

public class User {
private int id;
private String username;
private String password;
private String email;
private Date birthday;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}

------解决思路----------------------
应该dao层加上ps.excute...就可以提交数据了
------解决思路----------------------
你只是放入数据没有执行excute啊
ps.executeQuery(sql)
  相关解决方案