iteye_14754
iteye_14754
2010-05-12 17:35

使用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 qwe_rt 11年前

    [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。

    点赞 评论 复制链接分享
  • qwe_rt qwe_rt 11年前

    TransientDataAccessResourceException描述:

    [quote]This exception hierarchy aims to let user code find and handle the kind of error encountered without knowing the details of the particular data access API in use (e.g. JDBC). Thus it is possible to react to an optimistic locking failure without knowing that JDBC is being used. [/quote]

    点赞 评论 复制链接分享
  • qwe_rt qwe_rt 11年前

    错误提示:使用没有实现Serializable接口的对象。

    [code="java"]public class DaoBaseOperation [/code][b]这个类的类型参数E必须限定边界(也就是说必须是继承Serializable接口)[/b]。
    如下方式修改:
    [code="java"]
    public class DaoBaseOperation {
    ......
    }[/code]

    点赞 评论 复制链接分享
  • iteye_21211 iteye_21211 11年前

    PreparedStatementCallback; SQL [update res_grade_table set gradeName=:gradeName where gradeId=:gradeId]; Invalid argument value,

    你的sql出来之后的语法有问题,比如字符传没带引号,数字的,你给了个字符串,整数的 你给了个浮点数。

    点赞 评论 复制链接分享
  • yaerfeng yaerfeng 7年前
    点赞 评论 复制链接分享

相关推荐