Example usage for org.springframework.jdbc.datasource SimpleDriverDataSource setUsername

List of usage examples for org.springframework.jdbc.datasource SimpleDriverDataSource setUsername

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource SimpleDriverDataSource setUsername.

Prototype

public void setUsername(@Nullable String username) 

Source Link

Document

Set the JDBC username to use for connecting through the Driver.

Usage

From source file:com.webapp.config.DataBaseConfig.java

/**
 * This creates a generic datasource for the web application.
 * /*from  ww w. ja va2  s  . c om*/
 * <p>It uses system properties to determine the configuration.</p>
 * 
 * @return A datasource 
 */
@Bean
public DataSource dataSource() {
    LOG.info("Creating a datasource..");
    SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
    try {
        LOG.info("Classname: " + env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
        @SuppressWarnings("unchecked")
        Class<? extends Driver> driverClass = (Class<? extends Driver>) Class
                .forName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
        dataSource.setDriverClass(driverClass);

        LOG.info("URL: " + env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
        dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));

        LOG.info("User: " + env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
        dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));

        dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
        LOG.info("Datasource created successfully.");
    } catch (ClassNotFoundException | IllegalStateException e) {
        LOG.fatal("Could not create driver class '" + env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER)
                + "'", e);
    }

    return dataSource;
}

From source file:org.netxilia.api.impl.storage.DataSourceConfigurationServiceImpl.java

@SuppressWarnings("unchecked")
public DataSource buildSimpleDataSource(DataSourceConfiguration cfg) {
    SimpleDriverDataSource dataSource = new SimpleDriverDataSource();

    Class<? extends Driver> driverClass;
    try {/*from   w  ww  .  j ava2s.  c  o  m*/
        driverClass = (Class<? extends Driver>) Class.forName(cfg.getDriverClassName());
    } catch (ClassNotFoundException e) {
        throw new NetxiliaResourceException("Cannot find class driver:" + cfg.getDriverClassName());
    }
    dataSource.setDriverClass(driverClass);
    dataSource.setUrl(cfg.getUrl().replace(NETXILIA_HOME_VAR, path));
    dataSource.setUsername(cfg.getUsername());
    dataSource.setPassword(cfg.getPassword());
    return dataSource;
}