?
我想在
JSP
頁面中向數(shù)據(jù)庫表
staff
插入一條記錄:頁面能跳轉(zhuǎn)到
addStaffSuccess.jsp
;
刷新頁面時,添加的記錄能在頁面中顯示出來,我還以為成功了,于是我想通過企業(yè)管理器打開數(shù)據(jù)庫表看是不是數(shù)據(jù)庫表更新了,發(fā)現(xiàn)數(shù)據(jù)庫表不顯示數(shù)據(jù),我插入的是第二條記錄,于是我讓它返回首行,首行正常顯示。我用查詢分析器查詢
Staff
表的數(shù)據(jù),一直是“正在執(zhí)行批查詢狀態(tài)
………
”說明我對數(shù)據(jù)庫的操作影響到了數(shù)據(jù)庫,但是出了我不知道的異常或錯誤,但是又不報錯,我不知道怎么去找原因。忘了,當我停止
tomcat
時,數(shù)據(jù)表正常顯示,但插入的數(shù)據(jù)沒有顯示在表里,說明沒有插入。難道是我沒停止
tomcat
前,數(shù)據(jù)庫一直在更新么?
我用的環(huán)境是,
eclipse3.2+Myeclipse5.5 + MS SQL server2000+ tomcat5.5
我添加了
struts
和
hibernate
支持,各個持久化類和
DAO
類
,
是通過映射后
hibernate
自動生成的。
Action
代碼是我寫的,不足道有錯沒,請指正。
???
添加后,返回原頁面,顯示了添加的信息:(但數(shù)據(jù)庫沒更新,郁悶!)
?
數(shù)據(jù)庫:
staff (staffid,//
主鍵
staffname,//not null
password,//not null
departmentname;//
允許空
sex;//
允許空
email;//
允許空
mobile;//
允許空
)
在
StaffDAO
里面:(這是
hibernate
映射數(shù)據(jù)庫表后自動生成的方法)
????
public
void
save(Staff transientInstance) {
??????
log
.debug(
"saving Staff instance"
);
??????
try
{
?????????? getSession().save(transientInstance);
??????????
log
.debug(
"save successful"
);
?????? }
catch
(
RuntimeException
re) {
??????????
log
.error(
"save failed"
, re);
??????????
throw
re;
?????? }
??? }
在
AddStaffAction
里面:
??
public
ActionForward execute(ActionMapping mapping, ActionForm form,
?????????? HttpServletRequest request, HttpServletResponse response) {
??????
//
獲得表單信息;
?????? AddStaffForm addStaffForm = (AddStaffForm) form;
//
TODO
Auto-generated method stub
?????? String staffid = addStaffForm.getStaffid();
?????? String staffname = addStaffForm.getStaffname();
?????? String departmentname = addStaffForm.getDepartmentname();
?????? String sex = addStaffForm.getSex();
?????? String password =
new
String(
"000000"
);
??????
??????
//
轉(zhuǎn)換字符格式
??????
try
{
??????????
?staffid =
new
String(staffid.getBytes(
"ISO-8859-1"
),
"GB2312"
);
??????????
?staffname =
new
String(staffname.getBytes(
"ISO-8859-1"
),
"GB2312"
);
??????????
?departmentname =
new
String(departmentname.getBytes(
"ISO-8859-1"
),
"GB2312"
);
??????????
?sex =
new
String(sex.getBytes(
"ISO-8859-1"
),
"GB2312"
);
??????????
?password =
new
String(password.getBytes(
"ISO-8859-1"
),
"GB2312"
);????????
??????
??? }
catch
(UnsupportedEncodingException e) {
??????????
?
//
TODO
Auto-generated catch block
??????????
?? e.printStackTrace();
??????
??? }
??????
??? Staff staff =
new
Staff();
??????????
? staff.setStaffid(staffid);
??????????
? staff.setStaffname(staffname);
??????????
? staff.setPassword(password);
??????????
? staff.setDepartmentname(departmentname);
??????????
? staff.setSex(sex);???????? ?????????????????????????????????????????????????????
??????
???
try
{
????????????? StaffDAO dao =
new
StaffDAO();
?????????????
???????? dao.save(staff);
?????????? }
catch
(Exception e) {
?????????????
//
TODO
Auto-generated catch block
????????????? e.printStackTrace();???????????
?????????? }
????????????????
return
mapping.findForward(
"addStaffSuccess"
);??????? ??? }