gpt4 book ai didi

java - 在 Spring 中使用 jdbcTemplate 的简单示例时出现空指针异常

转载 作者:行者123 更新时间:2023-12-01 18:36:53 27 4
gpt4 key购买 nike

我正在使用 Spring 学习 JdbcTemplate,当我运行我的 java 应用程序时,我在这一行得到空指针:

return jdbcTemplate.queryForMap(sql, id);

请在下面找到全类:

public class CustomerDaoImpl implements CustomerDAO{

private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource ds) {
dataSource = ds;
}

public Map getCustomerById(String id) {
String sql = "SELECT * FROM CUSTOMER WHERE CUST_ID = ?";

return jdbcTemplate.queryForMap(sql, id);
}

请在下面找到我的 SpringContext 文件:

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

<!-- Scans within the base package of the application for @Components to
configure as beans -->

<context:property-placeholder location="classpath:db.properties" />


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>

<bean id="customerDAO" class="com.tuto.dao.impl.CustomerDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>


</beans>

请在下面找到我的主要方法:

public class JdbcTemplateApp {
public static void main(String[] args) {
// if you have time,
// it's better to create an unit test rather than testing like this :)

ApplicationContext context = new ClassPathXmlApplicationContext(
"integration.xml");

CustomerDAO customerDAO = (CustomerDAO) context.getBean("customerDAO");

Map customerA = customerDAO.getCustomerById("1");
System.out.println("Customer A : " + customerA);

}
}

请在 Eclipse 控制台中找到我的异常:

INFO: Loaded JDBC driver: oracle.jdbc.driver.OracleDriver
Exception in thread "main" java.lang.NullPointerException
at com.tuto.dao.impl.CustomerDaoImpl.getCustomerById(CustomerDaoImpl.java:23)
at com.tuto.main.JdbcTemplateApp.main(JdbcTemplateApp.java:23)

知道为什么它为空吗?

预先感谢您的帮助

最佳答案

将 setDataSource 更改为:

public void setDataSource(DataSource ds) {
jdbcTemplate = new JdbcTemplate(ds);
}

关于java - 在 Spring 中使用 jdbcTemplate 的简单示例时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21602135/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com