1

I have spring boot application setup. Now I need to add Spring JDBC Template to it. While doing this, I am facing below exception.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'XXX': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.jdbc.core.JdbcTemplate com..XXX.jdbcTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "LOCAL" are currently active).
Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "LOCAL" are currently active).

Below is the code.

@Service
public class XXX {

    @Autowired
    JdbcTemplate jdbcTemplate;

    public void testDataSource() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from C_MASTER");
        System.out.println("list : " + list);
    }

}

Java Config

@Configuration
@ComponentScan
@EnableTransactionManagement
public class DAODataServiceManagerConfiguration {

    @Bean
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
    dataSource.setUrl("jdbc:oracle:thin:@g9u1769.houston.hpecorp.net:1525:ODSDBD");
    dataSource.setUsername("Solid_batch");
    dataSource.setPassword("solid_batch123");

    return dataSource;
    }

}

As spring boot looks for application.properties, I have added that too in the resources directory. appliation.properties.

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@g9u1769.houston.hpecorp.net:1525:ODSDBD
spring.datasource.username=Solid_batch
spring.datasource.password=solid_batch123

spring.datasource.initialize=true

It is unable to build the application. Correct me if I am doing anything wrong.

9
  • Can you show us your main() method? Usually when you get the Cannot determine driver type... error, you're not actually using the configuration you think you are. Also, do not combine duplicate configuration in a properties file with Java config. That's only going to confuse things down the road. Commented Aug 14, 2016 at 15:26
  • Do you have ojdbc jar on your classpath? Commented Aug 14, 2016 at 15:42
  • @ChrisThompson My application is a rest service, when request comes I need to invoke this method. So I don't have any main(). I think when we are building the application first it should read the configuration files and have to for the service. So it is failing in the service class (XXX). In the starting I didn't have application.properties. I just followed some blogs but no use of that. Commented Aug 14, 2016 at 15:49
  • @AdityaSarma : I have ojdbc.jar. If I don't have it should give different error I hope so. (like class not found). Commented Aug 14, 2016 at 15:50
  • @abc how are you running the Spring Boot application? Commented Aug 14, 2016 at 15:51

3 Answers 3

1

You are missing ojdbc jar in your project classpath, follow below steps to download, install and use it as a dependency:

  1. Download ojdbc6.jar from here.

  2. Install it, running command -

    mvn install:install-file -Dfile={Path/to/your/ojdbc.jar} -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar

For jar version, extract the jar file and check the Implementation-Version in MANIFEST.MF, for instance:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_51-b10 (Sun Microsystems Inc.)
Implementation-Vendor: Oracle Corporation
Implementation-Title: JDBC
Implementation-Version: 11.2.0.4.0
Repository-Id: JAVAVM_11.2.0.4.0_LINUX.X64_RELEASE
Specification-Vendor: Sun Microsystems Inc.
Specification-Title: JDBC
Specification-Version: 4.0
Main-Class: oracle.jdbc.OracleDriver
sealed: true

Name: oracle/sql/converter/
Sealed: false

Name: oracle/sql/
Sealed: false

Name: oracle/sql/converter_xcharset/
Sealed: false

Name: oracle/replay/driver/
Sealed: false
  1. Add as a dependency in the project, as follows:

    <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0</version> </dependency>

Sign up to request clarification or add additional context in comments.

3 Comments

I have ojdbc.jar in my buildpath
Did you installed ojdbc.jar, as I suggested in Step 2?
I don't think it is required. I have checked buildpath and I found ojdbc.jar there. To say exactly I am able to run the above class using test class. It is working find. But while building it is failing.
0
Use jdbctemplate by extends JdbcDaoSupport .
By it programmer not concern about the open and close the connection.
Use commons-dbcp2-2.1.1.jar and commons-pool2-2.4.2.jar for use dbcp2  because dbcp2 support Connection pooling.
It's a technique to allow multiple clinets to make use of a cached set of shared and reusable connection objects providing access to a database


public class XXX extends JdbcDaoSupport {

    public void testDataSource() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from C_MASTER");
        System.out.println("list : " + list);
    }

}

In spring.xml write

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/database_name" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>

    <bean id="xXX" class="your_package_name.XXX">
        <property name="dataSource" ref="dataSource" />
    </bean>

1 Comment

I know this works but I just have to write using spring boot jdbc. Thanks for your help.
0

I have gone through spring boot reference document. I came to know that if we are using (H2, HSQL or Derby) databases then we don't require application.properties.

If the project is having Oracle database, then application.properties should be updated. In my case I have updated the properties file.

So I have updated only following things, then it worked properly.

pom.xml

<dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

configuration file

@Configuration
public class DaoConfig {

    @Bean
    public DataSource getDataSource() {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
        dataSource.setUrl("xxx");
        dataSource.setUsername("xxx");
        dataSource.setPassword("xxx");
        return dataSource;
    }

    @Bean
    public JdbcTemplate getJdbcTemplate() {

        return new JdbcTemplate(getDataSource());
    }

I hope this might help someone. Thanks @ChrisThompson and @Arpit

2 Comments

Spring boot autoconfigures the datasource for you. In most circumstances, you need not declare a Datasource @Bean. Spring boot initializes a datasource based on the datasource properties specified in application.properties. However,you still have the option of declaring them when the need arises.
@CodeItLikeAmeen: Thanks Ameen. Yes, this I have created on a purpose for my project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.