Reading Pro Spring 5, an example that show Spring JDBC is using MySQL DB. I use the book source code. Here is the code
@Configuration
@PropertySource("classpath:db/jdbc2.properties")
@ComponentScan(basePackages = "com.apress.prospring5.ch6")
public class AppConfig {
private static Logger logger = LoggerFactory.getLogger(AppConfig.class);
@Value("${driverClassName}")
private String driverClassName;
@Value("${url}")
private String url;
@Value("${username}")
private String username;
@Value("${password}")
private String password;
@Bean(destroyMethod = "close")
public DataSource dataSource() {
try {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
} catch (Exception e) {
logger.error("DBCP DataSource bean cannot be created!", e);
return null;
}
}
jdbc2.properties
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/musicdb?useSSL=true
username=prospring5
password=prospring5
The test
public class AnnotationJdbcTest {
private GenericApplicationContext ctx;
private SingerDao singerDao;
@Before
public void setUp() {
ctx = new AnnotationConfigApplicationContext(AppConfig.class);
singerDao = ctx.getBean(SingerDao.class);
assertNotNull(singerDao);
}
@Test
public void testFindAll() {
List<Singer> singers = singerDao.findAll();
assertTrue(singers.size() == 3);
listSingers(singers);
ctx.close();
}
My MySQL instance already have the user prospring5 and the schema created and populated
When I try to run AnnotationJdbcTest, I get this exception:
Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Cannot create PoolableConnectionFactory (Access denied for user 'Mehdi'@'localhost' (using password: YES))
org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Cannot create PoolableConnectionFactory (Access denied for user 'Mehdi'@'localhost' (using password: YES))
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:82)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:612)
As you can see the project is using my computer name 'Mehdi' instead of the one in the .properties file 'prospring5' . Why is That? and how Can I fix it?
You can yourself download the source code and run it from here: https://github.com/Apress/pro-spring-5 the project is: chapter6/spring-jdbc-annotations
EDIT: I printed the values as suggested by @STaefi and here is the output:
com.mysql.cj.jdbc.Driver
jdbc:mysql://localhost:3306/musicdb?useSSL=false
Mehdi
prospring5
Code
@Bean()
public DataSource dataSource() {
try {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClassName);
System.out.println(driverClassName);
dataSource.setUrl(url);
System.out.println(url);
dataSource.setUsername(username);
System.out.println(username);
dataSource.setPassword(password);
System.out.println(password);
return dataSource;
First I tried setting the values at initialization and that was no good. but after I used username = "prospring5"; dataSource.setUsername(username);
it worked. so what does this mean. why Spring cannot load the username like it successfully loaded the url and the password.