Spring中的JDBC模板给出异常

时间:2017-06-07 22:52:03

标签: java spring jdbctemplate

所以我尝试过很多改变,但是我不知道我的代码到底出了什么问题。这是我在Spring的第一个项目。我的sql方法给了一个空指针异常。

我的JdbcDAO.java

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.datasource.*;
import org.springframework.stereotype.Repository;

/**
 * Created by Albedo on 6/6/2017.
 */

@Repository("reviewDAO")
public class JdbcReviewDAO {
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Bean
    public JdbcTemplate getTemplate(){
        return jdbcTemplate;
    }

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

    // sql methods go here
}

我的spring-Datasource.xml

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

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
                </bean>
 <bean id="reviewDAO" class="com.JdbcReviewDAO">
    <property name="dataSource" ref="dataSource" />
</bean>
</beans>

我的application.java:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        //ApplicationContext context =
        //      new ClassPathXmlApplicationContext("Spring-Review.xml");
        JdbcReviewDAO reviewDAO = new JdbcReviewDAO();
        reviewDAO.grabFirstPost(1);
        SpringApplication.run(Application.class, args);
    }
}

(我已经删除了软件包名称 - 请放心,它们已在项目中正确定义。还有SQL方法可以删除冗余。如果您怀疑它们是问题,请告诉我是否应该包含它们。)我认为这是更多的“基础设施”问题(原谅我的无知,仍然是一个noobie)而不是代码。有人有想法吗?

错误日志:\

Exception in thread "main" java.lang.NullPointerException
    at JdbcDAO.grabFirstPost(JdbcDAO.java:63)
    at Application.main(Application.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1

提供错误的方法*:

public String grabFirstPost(int id) {
    String sql = "SELECT * FROM post WHERE postid ='" + id + "'";

    String postContent = (String)jdbcTemplate.queryForObject(sql, new Object[] {id}, String.class);
    return postContent;
}

2 个答案:

答案 0 :(得分:1)

你的主要方法是错误的。您没有加载应用程序上下文,而是直接创建DAO。也许你需要改变这个:

ApplicationContext context =
      new ClassPathXmlApplicationContext("Spring-Review.xml");
JdbcReviewDAO reviewDAO = context.getBean("reviewDAO");

答案 1 :(得分:0)

看起来变量jdbcTemplate包含null。最可能的原因是Spring没有注入它,因为setter没有注释@Autowired