|
The connection is definitely no problem, because the connection is used before
The insertUser method in the User class:
public int insertUser(DataSource ds){
int result=0;
Connection conn;
try {
conn = ds.getConnection();
PreparedStatement pstmt=conn.prepareStatement("insert into tb_customer(username,password,cusTrueName,"+
"cusAddress, cusPostcode, cusProvince, cusCity, cusTel, cusEmail, cusCardType, cusCardNo) "+
"values(?,?,?,?,?,?,?,?,?,?,?)");
pstmt.setString(1,username);
pstmt.setString(2,password);
pstmt.setString(3,cusTrueName);
pstmt.setString(4,cusAddress);
pstmt.setString(5,cusPostcode);
pstmt.setString(6,cusProvince);
pstmt.setString(7,cusCity);
pstmt.setString(8,cusTel);
pstmt.setString(9,cusEmail);
pstmt.setString(10,cusCardType);
pstmt.setString(11,cusCardNo);
pstmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
The execute method in registerAction:
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response){
The
RegisterForm registerForm = (RegisterForm) form;
The
String username=registerForm.getUsername();
String password=registerForm.getPassword();
String cusTrueName=registerForm.getCusTrueName();
String cusAddress=registerForm.getCusAddress();
String cusPostcode=registerForm.getCusPostcode();
String cusProvince=registerForm.getCusProvince();
String cusCity=registerForm.getCusCity();
String cusTel=registerForm.getCusTel();
String cusEmail=registerForm.getCusEmail();
String cusCardType=registerForm.getCusCardType();
String cusCardNo=registerForm.getCusCardNo();
The
ActionMessages errors=new ActionMessages();
DataSource ds=getDataSource(request);
DBUtil dbu=new DBUtil(ds);
if(UserDao.userExist(dbu,username)){
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("errors.username.exist"));
if(!errors.isEmpty()){
saveErrors(request,errors);
}
dbu.close();
return mapping.findForward("failure");
}
dbu.close();
The
User user=new User(username,
password,
cusTrueName,
cusAddress,
cusPostcode,
cusProvince,
cusCity,
cusTel,
cusEmail,
cusCardType,
cusCardNo);
int result=0;
result=user.insertUser(ds);
if(result!=1){
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("errors.insert.failed"));
if(!errors.isEmpty()){
saveErrors(request,errors);
}
return mapping.findForward("failure");
}
HttpSession session=request.getSession();
session.setAttribute("username",username);
The
return mapping.findForward("success");
}
} |
|