I am trying to do a simple connection using spring jdbc that returns a type Connection
, at my project I am using spring jdbc with spring data, automatically configured.
At my code, I need to return this connection (with my local info).
Is it possible to do it? and in the case it is, is it possible to get the information that is being used at the moment? (I mean, dbname, password and so on..)
Thanks
EDIT------
The original dataSource bean looks like this
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="jdbc:mysql://${jdbc.host}:${jdbc.port}"/>
</bean>
But I cannot make it with annotations, any idea?
Ive tried to do this
DriverManagerDataSource source = new org.springframework.jdbc.datasource.DriverManagerDataSource();
THE ERROR
I Keep getting that this
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
This class extends of JdbcDaoSupport
that seems like it needs of it...
Here is the JdbcDaoSupport.class
package org.springframework.jdbc.core.support;
import java.sql.Connection;
import javax.sql.DataSource;
import org.springframework.dao.support.DaoSupport;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.support.SQLExceptionTranslator;
public abstract class JdbcDaoSupport extends DaoSupport {
private JdbcTemplate jdbcTemplate;
public JdbcDaoSupport() {
}
public final void setDataSource(DataSource dataSource) {
if(this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource()) {
this.jdbcTemplate = this.createJdbcTemplate(dataSource);
this.initTemplateConfig();
}
}
protected JdbcTemplate createJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
public final DataSource getDataSource() {
return this.jdbcTemplate != null?this.jdbcTemplate.getDataSource():null;
}
public final void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
this.initTemplateConfig();
}
public final JdbcTemplate getJdbcTemplate() {
return this.jdbcTemplate;
}
protected void initTemplateConfig() {
}
protected void checkDaoConfig() {
if(this.jdbcTemplate == null) {
throw new IllegalArgumentException("\'dataSource\' or \'jdbcTemplate\' is required");
}
}
protected final SQLExceptionTranslator getExceptionTranslator() {
return this.getJdbcTemplate().getExceptionTranslator();
}
protected final Connection getConnection() throws CannotGetJdbcConnectionException {
return DataSourceUtils.getConnection(this.getDataSource());
}
protected final void releaseConnection(Connection con) {
DataSourceUtils.releaseConnection(con, this.getDataSource());
}
}
Ive declared this class as @bean
and do this
public DriverManagerDataSource provideSource() {
DriverManagerDataSource dataSource = new org.springframework.jdbc.datasource.DriverManagerDataSource();
//this.dataSource = dataSource;
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUsername("user");
dataSource.setPassword("pass");
dataSource.setUrl("jdbc:mysql://localhost:3306/db");
return dataSource;
}
@Bean
MyClientDao myClientDao(){
MyClientDao myClientDao = new MyClientDao();
myClientDao().setDatabaseName("db");
myClientDao().setDataSource(provideSource());
return myClientDao();
}
Any idea how to do it?