【发布时间】:2021-09-03 02:34:21
【问题描述】:
我正在尝试在我的 Spring Boot 项目中使用 JDBCTemplate 连接到 postgresql 数据库,但是当我尝试进行查询时遇到空指针异常
应用程序属性
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://url/test
spring.datasource.username=postgres
spring.datasource.password=pass
Pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
代码
public class TestDaoImpl implements TestDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void testconnection() throws Exception {
// TODO Auto-generated method stub
int x=jdbcTemplate
.queryForObject("select count(*) from table", Integer.class);
System.out.println(x);
}
我无法检测出问题所在...请您帮帮我...提前致谢!
【问题讨论】:
标签: spring spring-boot jdbctemplate