|
楼主 |
发表于 2020-6-12 23:45:01
|
显示全部楼层
连接肯定没问题,因为前面都用到连接的
User类中的insertUser方法:
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;
}
}
registerAction中的execute方法:
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response){
RegisterForm registerForm = (RegisterForm) form;
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();
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();
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);
return mapping.findForward("success");
}
} |
|