首先我的承认我有些画蛇添足了.但是为了尝试封装DAO,为以后遥不可及的更换DAO层做准备我做了这个尝试,结果出现问题.
我先贴一段出错的提示吧:
信息: SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
[b]org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [update res_grade_table set gradeName=:gradeName where gradeId=:gradeId]; Invalid argument value: [/b]java.io.NotSerializableException; nested exception is java.sql.SQLException: Invalid argument value: java.io.NotSerializableException
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:107)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:786)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:842)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:850)
at Org.Config.DaoBaseOperation.updateByBean(DaoBaseOperation.java:37)
at res.grade.Action.gradeAction.execute(gradeAction.java:46)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at Org.Config.EncodingFilter.doFilter(EncodingFilter.java:66)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
[b]Caused by: java.sql.SQLException: Invalid argument value: java.io.NotSerializableException[/b] at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
出错的重点部分我加粗了.
我把SimpleJdbcTemplate 中的一些日常操作写到了DaoBaseOperation()类中内容如下,我在gradeAction 这个类中调用updateByBean这个方法结果出现上面的错误,后面我直接使用SimpleJdbcTemplate 中的update方法错误消失(见gradeAction中注释部分),我很奇怪这个异常,传递的参数能够打印出来,可是异常却提示无效的参数值。不知道自己错在哪里。请各位朋友赐教
[code="java"]
package Org.Config;
import java.util.List;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
public class DaoBaseOperation {
static SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(
DBConnectionPool.getDataSource());
//插入数据返回数据ID
public int update(String sql,E Bean){
SqlParameterSource param = new BeanPropertySqlParameterSource(Bean);
KeyHolder keyHolder = new GeneratedKeyHolder();
this.simpleJdbcTemplate.getNamedParameterJdbcOperations().update(sql,
param, keyHolder);
return keyHolder.getKey().intValue();
}
//修改或删除数据
public void updateByObjectArray(String sql,Object[] args){
this.simpleJdbcTemplate.getJdbcOperations().update(sql, args);
}
//修改或删除数据
public void updateByBean(String sql,E Bean){
SqlParameterSource param = new BeanPropertySqlParameterSource(Bean);
System.out.println(param.getValue("gradeId"));
System.out.println(param.getValue("gradeName"));//参数传递正常
this.simpleJdbcTemplate.getJdbcOperations().update(sql, param);
}
//查询数据
public Object find(String sql,Object[] args,Class<?> clazz){
return this.simpleJdbcTemplate.getJdbcOperations().queryForObject(sql, args, new BeanPropertyRowMapper(clazz));
}
//数据列表
public List list(String sql,Object[] args,Class clazz){
return this.simpleJdbcTemplate.getJdbcOperations().query(sql, args, new BeanPropertyRowMapper(clazz));
}
public List list(String sql,Class clazz){
return this.simpleJdbcTemplate.getJdbcOperations().query(sql, new BeanPropertyRowMapper(clazz));
}
public List list(String sql,Object... args){
return this.simpleJdbcTemplate.queryForList(sql,args);
}
}
[/code]
[code="java"]package res.grade.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import res.grade.Bean.gradeBean;
import Admin.AdminInfo;
import Admin.BO.AdminBO;
import Org.Config.DBConnectionPool;
import Org.Config.DaoBaseOperation;
public class gradeAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
ActionMessages errors = new ActionMessages();
try {
AdminBO adminBO = new AdminBO();
adminBO.validateAdminSession(request);
adminBO.validateModuleSession(request, AdminInfo.AdminResourcesModuleId);
gradeBean gradeBean = (gradeBean) form;
/*System.out.println("GradeId:"+gradeBean.getGradeId());
System.out.println("GradeName:"+gradeBean.getGradeName());*/
DaoBaseOperation<gradeBean> daobase = new DaoBaseOperation<gradeBean>();
SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(
DBConnectionPool.getDataSource());
if (gradeBean.getGradeId()==0) {
daobase.update("insert into res_grade_table (gradeName) values (:gradeName)", gradeBean);
} else {
/*String sql = "update res_grade_table set gradeName=:gradeName where gradeId=:gradeId ";
simpleJdbcTemplate.update(sql, new BeanPropertySqlParameterSource(gradeBean));*/
daobase.updateByBean("update res_grade_table set gradeName=:gradeName where gradeId=:gradeId", gradeBean);
}
return mapping.findForward("success");
} catch (Throwable e) {
e.printStackTrace();
ActionMessage error = new ActionMessage(e.getMessage());
errors.add(ActionMessages.GLOBAL_MESSAGE, error);
}
saveErrors(request, errors);
return new ActionForward(mapping.getInput());
}
}
[/code]