iteye_14754 2010-05-12 17:35
浏览 773
已采纳

使用spring SimpleJdbcTemplate遇到的问题

首先我的承认我有些画蛇添足了.但是为了尝试封装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]

  • 写回答

5条回答 默认 最新

  • qwe_rt 2010-05-13 17:05
    关注

    [quote]the details of the particular data access[/quote]
    就是说这个异常抛出的原因,数据库系统的细节上的一个写特殊数据访问原因导致。

    看看这个异常抛出的原因,在源码中已经说明白了,看注释。
    [code="java"]TRANSIENT_DATA_ACCESS_RESOURCE_CODES.add("JW"); // Sybase: internal I/O error
    TRANSIENT_DATA_ACCESS_RESOURCE_CODES.add("JZ"); // Sybase: unexpected I/O error
    TRANSIENT_DATA_ACCESS_RESOURCE_CODES.add("S1"); // DB2: communication failure
    [/code]

    [code="java"]if (TRANSIENT_DATA_ACCESS_RESOURCE_CODES.contains(classCode)) {
    return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
    }

    [/code]

    直接调用simpleJdbcTemplate来进行update操作没有出错,就用update。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的