本文介绍了JdbcTemplate NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring的JdbcTemplate将数据保存到数据库,但是却收到此错误消息.如果我使用PreparedStatements正常进行操作,则它可以正常工作.

I am trying to save data to database with Spring's JdbcTemplate, but I get this error message. If I do it normal way with PreparedStatements it's working.

我的CarDAO课程:

My CarDAO class:

@Repository
@Service
public class CarDAO implements CarDAOService {

private JdbcTemplate jdbcTemplate;

public JdbcTemplate getJdbcTemplate() {
    return jdbcTemplate;
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}



public void saveCarToDB(CarBean carbean) {

    final String sql = "insert into car (make, model) values (?,?)";

    Object[] parameters = new Object[] {carbean.getMake()+
            carbean.getModel()};

    //if I do here system.out.print(Arrays.toString(parameters));
    //it will print right make/model.

    jdbcTemplate.update(sql, parameters);

   //console says it is that row above, but I don't get how. Both parameters has values?
   //WARNING: StandardWrapperValve[spring-dispatcher]: Servlet.service() for servlet
   //springdispatcher threw exception

}

Spring-base.xml

Spring-base.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="bean, dao" />


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/Jsp/" />
    <property name="suffix" value=".jsp" />
</bean>


<!-- DATA SOURCE -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close">
    <property name="driverClassName" value="org.mariadb.jdbc.Driver" />
    <property name="url" value="jdbc:mariadb://XXXXX" />
    <property name="username" value="XXXXX" />
    <property name="password" value="XXXXX" />
    <property name="initialSize" value="1" />
    <property name="maxActive" value="5" />
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
</bean>

<mvc:annotation-driven />

</beans>

我没有包括Controller或CarDAOService类,因为我认为问题不存在.他们正在将正确的参数转发给CarDAO类.

I didn't include my Controller or CarDAOService class, because I think the problem isn't there. They are forwarding right parameters to CarDAO class.

推荐答案

您需要在设置器上放置@Autowired批注:

You need to put the @Autowired annotation on your setter:

@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

这篇关于JdbcTemplate NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 20:05