GeR
GeR

Reputation: 135

Connecting to database using Spring JDBC

I am trying to connect to my database using 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>

The place I want to make the connection:

public class BookService { 
    private Connection connection;

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

I get these errors

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

What am I doing wrong?

Upvotes: 2

Views: 179

Answers (2)

user3145373 ツ
user3145373 ツ

Reputation: 8156

You need to add spring-context and spring-beans dependency for it, if you are using maven.

For better dependency management use maven or gradle.

add in pom.xml:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>{spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>{spring.version}</version>
    </dependency>

Upvotes: 0

Sazzadur Rahaman
Sazzadur Rahaman

Reputation: 7126

Looks like, you are missing some Spring Jars in your project! Use any standard Dependency management tool like Gradle, Maven or Ant to resolve your dependencies.

Here you will find an example of how to configure Spring project using Gradle.

Upvotes: 1

Related Questions