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 trialJose Mejia
16,758 PointsAdd 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!!
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;
}
}
org.h2.Driver
jdbc:h2:mem:contactmgr
1 Answer
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 PointsHey 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
16,758 PointsThanks, to take your time and answer my question. That help me!
Benjamin Smith
18,184 PointsBenjamin Smith
18,184 Pointspackage 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();
} }
// Assign names for the values in "app.properties"
contactmgr.db.driver = org.h2.Driver
contactmgr.db.url = jdbc:h2:mem:contactmgr