Spring+jdbc在插入数据的时候报错-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

Spring+jdbc在插入数据的时候报错

各位帮帮忙,谢谢!!!用了springmvc+spring+jdbc

这是异常信息:log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [insert into X_upfile(id,fileInfo,fileName,upFileDate,fileType,user_id)values(?,?,?,?,?,?)]; Invalid argument value: java.sql.SQLException; nested exception is java.sql.SQLException: Invalid argument value: java.sql.SQLException
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:106)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:607)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:792)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:850)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:858)
at com.yu.dao.impl.FileInfoDaoImpl.save(FileInfoDaoImpl.java:58)
at com.yu.Test.main(Test.java:31)
Caused by: java.sql.SQLException: Invalid argument value: java.sql.SQLException
at com.mysql.jdbc.PreparedStatement.setSerializableObject(PreparedStatement.java:2348)
at com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:1127)
at org.apache.commons.dbcp.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:166)
at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:346)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:217)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:145)
at org.springframework.jdbc.core.ArgPreparedStatementSetter.setValues(ArgPreparedStatementSetter.java:51)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:796)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:591)
... 5 more


这是DaoImpl:

/**
     * 数据源
     */
@SuppressWarnings("unused")
private DataSource dataSource;

    /**
     * spring提供的jdbc操作辅助类
     */
private JdbcTemplate jdbcTemplate;

/**设置数据源*/ 
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
public void save(FileINfoEntity entity) {
String sql = "insert into X_upfile(id,fileInfo,fileName,upFileDate,fileType,user_id)"
+ "values(?,?,?,?,?,?)";
jdbcTemplate.update(sql, new Object[] { 
entity.getId(),entity.getFileInfo(),
entity.getFileName(),entity.getUpFileDate(), 
entity.getFileType(),entity.getUser_id(),new int[]{
java.sql.Types.VARCHAR,java.sql.Types.VARCHAR,
java.sql.Types.VARCHAR,java.sql.Types.VARCHAR,
java.sql.Types.VARCHAR,java.sql.Types.VARCHAR
}});
}
这是我的Spring配置文件:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 读取jdbc.properties配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="${maxActive}" />
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}" />
<!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}" />
</bean>

<!--
采用注解方式来配置事务。针对数据源的事务管理器
,把我们定义的数据源注入到DataSourceTransactionManager类的属性dataSource中
-->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<!--
引入命名空间: 1.xmlns:tx="http://www.springframework.org/schema/tx
2.http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
采用@Transaction注解方式使用事务管理器
-->
<tx:annotation-driven transaction-manager="txManager" />

<!--配置控制器的映射-->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="uploadFile.do">uploadFileAction</prop>
</props>
</property>
</bean>

<!-- 文件Dao -->
<bean id="fileInfoDao" class="com.yu.dao.impl.FileInfoDaoImpl">
<!-- 向属性dataSource注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>

<!--配置视图-->
<!-- 视图扩展名解析器 对模型视图名称的解析,即在模型视图名称添加前后缀-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- <property name="prefix" value=""/> -->
<property name="suffix" value=".jsp" />
</bean>


<!--
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
-->
<!-- one of the properties available; the maximum file size in bytes -->
<!--  <property name="maxUploadSize" value="10000000" />10M -->
<!-- </bean>-->


<!-- 上传文件服务 -->
<bean id="uploadFileAction" class="com.yu.service.UploadFileServiceAction">
<property name="commandClass" value="com.yu.entity.FileINfoEntity">
</property>
</bean>

展开
收起
a123456678 2016-03-16 09:47:48 2907 0
1 条回答
写回答
取消 提交回答
  • a123456678

    String sql = "insert into X_upfile(id,fileInfo,fileName,upFileDate,fileType,user_id)"

    • "values(?,?,?,?,?,?)";
    2019-07-17 19:03:24
    赞同 展开评论 打赏
问答排行榜
最热
最新
相关电子书
更多
Spring Boot2.0实战Redis分布式缓存
立即下载
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题)
立即下载
Java Spring Boot开发实战系列课程【第15讲】:Spring Boot 2.0 API与Spring REST Docs实战
立即下载