gpt4 book ai didi

java - 使用 Spring JDBC 连接数据库

转载 作者:行者123 更新时间:2023-11-30 11:02:50 24 4
gpt4 key购买 nike

我正在尝试使用 Spring JDBC 连接到我的数据库

Beans.xml

<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-3.0.xsd">

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5434/lab1_aop" />
<property name="username" value="postgres" />
<property name="password" value="passw" />
</bean>

</beans>

我要连接的地方:

public class BookService { 
private Connection connection;

public BookService() {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
}
}

我得到这些错误

The constructor ClassPathXmlApplicationContext(String) refers to the missing type BeansException    BookService.java
The type org.springframework.beans.BeansException cannot be resolved. It is indirectly referenced from required .class files

我做错了什么?

最佳答案

看起来,您的项目中缺少一些 Spring Jars!使用任何标准的依赖关系管理工具,如 Gradle、Maven 或 Ant 来解决您的依赖关系。

在这里您将找到一个如何配置 Spring project using Gradle 的示例.

关于java - 使用 Spring JDBC 连接数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30634416/

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