小弟在用JBuilder9作添加用户时总是报错,用地是MYsql数据库,userId是自增长列,userName是唯一约束
提交后跳转到错误页面
控制台错误信息:Duplicate entry 'tgyy' for key 2,tgyy是输入的用户名。
可报错后数据还是添加到数据库中了,不知何故,还求高手指点一下,下面是一段代码:
public int setRegister(User u) throws Exception {
int ok=0;
DBConnection db=new DBConnection();
String sql="insert into userinfo values(?,?,?,?)";
conn=db.getConnection();
try {
pst = conn.prepareStatement(sql);
pst.setString(1,MyUtil.encoding(u.getUserId()));
pst.setString(2,u.getUserName());
pst.setString(3,u.getUserPass());
pst.setString(4,u.getUserTel());
ok=pst.executeUpdate();
}
catch (SQLException ex) {
ex.printStackTrace();
ok=ex.getErrorCode();
}finally {
try {
pst.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return ok;
}
servlet中:
String user=request.getParameter("userName").trim();
String pass=request.getParameter("userPass").trim();
String tell=request.getParameter("userTel").trim();
UserBean ub=new UserBean();
User u=new User();
u.setUserId(null);
u.setUserName(user);
u.setUserPass(pass);
u.setUserTel(tell);
int x = 0;
try {
x = ub.setRegister(u);
}
catch (Exception ex) {
}
if(x==0)
{
response.sendRedirect("/client/admin/compQuery.jsp");
}
else
{
response.sendRedirect("/client/admin/reg_fail.jsp");
}