Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Spring with Hibernate Integrating Hibernate with Spring Configure Spring for Hibernate

Jose Mejia
Jose Mejia
16,758 Points

Add a Spring Bean to the DataConfig class that configures a BasicDataSource with the following settings: - Connection UR

Bummer! The driver class name is incorrect. expected:<org.h2.Driver> but was:<null>

I dont know what I am doing wrong ? can someone explain me whats missing? Thanks!!

com/teamtreehouse/contactmgr/config/DataConfig.java
package com.teamtreehouse.contactmgr.config;

import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

import javax.sql.DataSource;
@Configuration
@PropertySource("app.properties")
public class DataConfig {
  @Autowired
  private Environment env;

  @Bean
    public DataSource dataSource() {
      BasicDataSource ds = new BasicDataSource();

      // Driver class name
      ds.setDriverClassName(env.getProperty("org.h2.Driver"));

      // Set URL
      ds.setUrl(env.getProperty("jdbc:h2:mem:contactmgr"));

      return ds;
    }


}
app.properties
org.h2.Driver
jdbc:h2:mem:contactmgr
Benjamin Smith
Benjamin Smith
18,184 Points

package com.teamtreehouse.contactmgr.config;

import org.apache.tomcat.dbcp.dbcp2.BasicDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

import javax.sql.DataSource;

@Configuration @PropertySource("app.properties") public class DataConfig {

@Autowired private Environment env;

@Bean public DataSource dataSource() { BasicDataSource ds = new BasicDataSource();

// Use the name assigned to the value in "app.properties"
ds.setDriverClassName(env.getProperty("contactmgr.db.driver"));

// Use the name assigned to the value in "app.properties"
ds.setUrl(env.getProperty("contactmgr.db.url"));

return ds;

} }

// Assign names for the values in "app.properties"

contactmgr.db.driver = org.h2.Driver

contactmgr.db.url = jdbc:h2:mem:contactmgr

1 Answer

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Hey Jose,

"org.h2.Driver" is the driver name already so you don't have to fetch it as a property. Same goes for the connection URL.

Change

      ds.setDriverClassName(env.getProperty("org.h2.Driver"));
      ds.setUrl(env.getProperty("jdbc:h2:mem:contactmgr"));

to

      ds.setDriverClassName("org.h2.Driver");
      ds.setUrl("jdbc:h2:mem:contactmgr");

and you should be fine.

Kind Regards, Florian

Jose Mejia
Jose Mejia
16,758 Points

Thanks, to take your time and answer my question. That help me!