observer
observer

Reputation: 725

instantiate a datasource using Spring's JdbcTemplate

I want to instantiate a Datasource in the Dao Class. I'm following the Spring tutorial http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/jdbc.html. This is my code snippet:

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.*;


public class JdbcUserDao implements UserDao {

    private JdbcTemplate jdbcTemplate;

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

but I get a "DataSource cannot be resolved to a type". How can I fix this?

Upvotes: 0

Views: 8527

Answers (3)

Pravin Mishra
Pravin Mishra

Reputation: 8434

I solved this problem by adding 'org.springframework' dependencies in pom.xml file.

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>3.2.0.RELEASE</version>
</dependency>

In my case, I was facing problem with import methods itself like "The import org.springframework.jdbc cannot be resolved"

Upvotes: 1

jags
jags

Reputation: 176

What is the DataSource that you have configured in the Spring configuration? You should have a datasource library similar to org.apache.commons.dbcp.BasicDataSource in your project. The tutorial link http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/jdbc.html shows the configuration under section '13.2.1.2 JdbcTemplate best practices'.
Actions:
1) Check if you have the datasource library in your project. If you are using the DataSource mentioned in the tutorial (BasicDataSource) then ensure you have Apache commons DBCP library is in your classpath.
2) Ensure you have imported the same in your DAO class.

Upvotes: 1

skaffman
skaffman

Reputation: 403481

You need to import it, that's all:

import javax.sql.DataSource;

Upvotes: 2

Related Questions