Example usage for org.springframework.jdbc.datasource SingleConnectionDataSource SingleConnectionDataSource

List of usage examples for org.springframework.jdbc.datasource SingleConnectionDataSource SingleConnectionDataSource

Introduction

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

Prototype

public SingleConnectionDataSource(String url, String username, String password, boolean suppressClose) 

Source Link

Document

Create a new SingleConnectionDataSource with the given standard DriverManager parameters.

Usage

From source file:springobjectmapper.Example.java

public static void main(String[] args) {
    // Dialect dialect = new HsqlDbDialect();
    // SingleConnectionDataSource dataSource = new
    // SingleConnectionDataSource("jdbc:hsqldb:mem:test", "sa", "", false);

    Dialect dialect = new MySqlDialect();
    SingleConnectionDataSource dataSource = new SingleConnectionDataSource("jdbc:mysql://localhost/foo", "root",
            "", false);

    SimpleJdbcTemplate template = new SimpleJdbcTemplate(dataSource);

    template.update("CREATE TABLE country (id " + dialect.defaultIdFieldType()
            + " PRIMARY KEY, code CHAR(2) NOT NULL, name VARCHAR(50) NOT NULL)");
    template.update("CREATE TABLE person (id " + dialect.defaultIdFieldType()
            + " PRIMARY KEY,first_name VARCHAR(50) NOT NULL,last_name VARCHAR(50) NOT NULL,country_id BIGINT NOT NULL,email VARCHAR(50) NOT NULL,FOREIGN KEY (country_id) REFERENCES country(id))");

    PersonRepository persons = new PersonRepository(template, dialect);
    CountryRepository countries = new CountryRepository(template, dialect);

    Country uk = new Country("UK", "United Kingdom");
    countries.save(uk);//  w  w  w.  ja v a  2s.c  o m

    Country ussr = new Country("SU", "Soviet Union");
    countries.save(ussr);

    persons.save(new Person("James", "Bond", "007@mi6.co.uk", uk));
    persons.save(new Person("Tatiana", "Romanova", "tatiana.romanova@kgb.su", ussr));
    persons.save(new Person("Alexander", "Trevelyan", "006@mi6.co.uk", uk));

    System.out.println("Good guys:");
    List<Person> results = persons
            .query(new Query("country_id=?", Order.by("last_name").descending(), uk.id()));
    for (Person person : results) {
        System.out.println(person.getFirstName() + " " + person.getLastName());
    }

    System.out.println("Bad guys:");
    results = persons.query(new Query("country_id!=?", Order.by("last_name").descending(), uk.id()));
    for (Person person : results) {
        System.out.println(person.getFirstName() + " " + person.getLastName());
    }
    dataSource.destroy();
}

From source file:com.jaxio.celerio.configuration.database.mysql.MySqlEnumExtractorTest.java

private JdbcTemplate jdbcTemplate() {
    return new JdbcTemplate(
            new SingleConnectionDataSource("jdbc:mysql://localhost/sakila", "root", "root", true));
}

From source file:com.univocity.app.utils.DatabaseImpl.java

public DatabaseImpl(String databaseName, File dirWithCreateTableScripts) {
    try {/* w ww.  ja va2  s . c om*/
        Class.forName("org.hsqldb.jdbcDriver");
        DataSource dataSource = new SingleConnectionDataSource("jdbc:hsqldb:mem:" + databaseName, "sa", "",
                true);
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    } catch (Exception ex) {
        throw new IllegalStateException("Error creating database " + databaseName, ex);
    }

    createTables(dirWithCreateTableScripts);
}

From source file:com.github.ferstl.spring.jdbc.oracle.dsconfig.SingleConnectionDataSourceConfiguration.java

@Bean
public DataSource dataSource() throws SQLException {
    SingleConnectionDataSource ds = new SingleConnectionDataSource(this.env.getProperty("db.url"),
            this.env.getProperty("db.username"), this.env.getProperty("db.password"),
            this.env.getProperty("db.defaultAutoCommit", Boolean.class));

    ds.setAutoCommit(false);/*from  w w w.  jav  a2  s  . c  o m*/
    return ds;
}

From source file:fr.xebia.springframework.security.core.userdetails.jdbc.ExtendedJdbcUserDetailsManagerTest.java

@Before
public void before() throws Exception {
    SingleConnectionDataSource dataSource = new SingleConnectionDataSource("jdbc:h2:mem:jmx-demo-db", "sa", "",
            false);/*from  w  w  w.ja  v a  2 s .c o  m*/

    Connection connection = dataSource.getConnection();
    String createUsersTable = "create table users(username varchar(256), password varchar(256), enabled int, allowedRemoteAddresses varchar(256), comments varchar(256))";
    connection.createStatement().execute(createUsersTable);

    String createAuthoritiesTable = "create table authorities(username varchar(256), authority varchar(256))";
    connection.createStatement().execute(createAuthoritiesTable);

    userDetailsManager = new ExtendedJdbcUserDetailsManager();

    userDetailsManager.setDataSource(dataSource);
    simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);

}

From source file:net.derquinse.bocas.jdbc.JDBCBocasTest.java

public void mysql() throws Exception {
    DataSource ds = new SingleConnectionDataSource("url", "user", "passwd", true);
    Connection cnn = ds.getConnection();
    cnn.createStatement()/*from  w  w w  . j  a  va 2  s  . co  m*/
            .execute("CREATE TABLE BOCAS_TABLE(BOCAS_KEY BINARY(32) PRIMARY KEY, BOCAS_VALUE LONGBLOB)");
    cnn.close();
    test(JDBCBocasServices.newBuilder().dialect(JDBCBocasDialect.MYSQL).build(ds));
}

From source file:springobjectmapper.repository.PersonRepositorySpec.java

@Override
public void create() {
    dataSource = new SingleConnectionDataSource("jdbc:hsqldb:mem:test" + (counter++), "sa", "", false);
    template = new SimpleJdbcTemplate(dataSource);
    createFixture();//from  w  w  w. j a v a 2  s. c  o  m
    CountryRepository countryRepository = new CountryRepository(template, dialect);
    country = new Country("SE", "Sweden");
    countryRepository.save(country);
}

From source file:springbook.user.dao.UserDaoOldTest.java

@Before
//  ?? ?  .//  ww  w . jav  a2  s.  co  m
// gtest ?  ?? ?   ?.
public void setUp() {
    dao = new UserDaoOld();

    DataSource dataSource = new SingleConnectionDataSource(url, username, password, true);

    dao.setDataSource(dataSource);

    user1 = new User("akinodaia", "", "rlathwjd");
    user2 = new User("user2", "?", "qkrxotjr");
    user3 = new User("user3", "", "qkrghdbs");
}

From source file:com.univocity.articles.databases.Database.java

/**
 * Initializes this Database object by creating a {@link DataSource} to provide connections to your database.
 * Tables will be created automatically if required.
 *
 * @param tablesToCreate a sequence of table names to create in this database, if they have not been created yet
 * @param connectionUrl the JDBC URL to use for accessing the {@link java.sql.DriverManager}
 * @param username the username to connect to the database
 * @param password the password of the given username, if required
 *//*from  w  w  w  .  ja  v a 2s.  co m*/
void initialize(String tablesToCreate, String connectionUrl, String username, String password) {
    try {
        Class.forName(getDriverClassName());
        DataSource dataSource = new SingleConnectionDataSource(connectionUrl, username, password, true);
        this.jdbcTemplate = new JdbcTemplate(dataSource);

    } catch (Exception ex) {
        throw new IllegalStateException(
                "Error creating database using scripts for database " + getDatabaseName(), ex);
    }

    initializeDatabase(tablesToCreate);
}

From source file:com.univocity.articles.dumpload.databases.Database.java

/**
 * Initializes this Database object by creating a {@link DataSource} to provide connections to your database.
 * Tables will be created automatically if required.
 *
 * @param tablesToCreate a sequence of table names to create in this database, if they have not been created yet
 * @param connectionUrl the JDBC URL to use for accessing the {@link java.sql.DriverManager}
 * @param username the username to connect to the database
 * @param password the password of the given username, if required
 *///  w  w w .  ja  v  a2s .  co m
void initialize(String tablesToCreate, String connectionUrl, String username, String password) {
    try {
        Class.forName(getDriverClassName());
        DataSource dataSource = new SingleConnectionDataSource(connectionUrl, username, password, true);
        this.jdbcTemplate = new JdbcTemplate(dataSource);

    } catch (Exception ex) {
        throw new IllegalStateException(
                "Error creating database using scripts for database " + getDatabaseName(), ex);
    }

    if (tablesToCreate != null) {
        initializeDatabase(tablesToCreate);
    }
}