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(Connection target, boolean suppressClose) 

Source Link

Document

Create a new SingleConnectionDataSource with a given Connection.

Usage

From source file:com.kaidad.utilities.MBTilesBase64Converter.java

public static void main(String... args) throws SQLException {
    if (args.length != 1) {
        System.out.println("Usage: MBTilesBase64Converter /path/to/file.mbtiles");
        System.exit(1);//  w  w  w .j  av a2  s  .c  o m
    }
    Connection c = null;
    try {
        c = connectToDb(args[0]);
        //        Statement st = c.createStatement();
        //        ResultSet rs = st.executeQuery("SELECT type, name, tbl_name FROM sqlite_master WHERE type='table'");
        //        while(rs.next()) {
        //            System.out.println("type: " + rs.getString(1) + ", name: " + rs.getString(2) + ", tbl_name: " + rs.getString(3));
        //        }
        JdbcTemplate template = new JdbcTemplate(new SingleConnectionDataSource(c, true));
        executeConversion(template);
        c.commit();
    } finally {
        if (c != null) {
            c.close();
        }
    }
}

From source file:org.jooq.java8.goodies.sql.SQLGoodies.java

public static void main(String[] args) throws Exception {

    Class.forName("org.h2.Driver");
    try (Connection c = getConnection("jdbc:h2:~/test", "sa", "")) {
        String sql = "select schema_name, is_default from information_schema.schemata order by schema_name";

        System.out.println("Fetching data into a with JDBC / Java 7 syntax");
        System.out.println("----------------------------------------------");
        try (PreparedStatement stmt = c.prepareStatement(sql); ResultSet rs = stmt.executeQuery()) {

            while (rs.next()) {
                System.out.println(new Schema(rs.getString("SCHEMA_NAME"), rs.getBoolean("IS_DEFAULT")));
            }//from   ww w.jav a2  s.  c  om
        }

        System.out.println();
        System.out.println("Fetching data into a lambda expression with jOOQ");
        System.out.println("------------------------------------------------");
        DSL.using(c).fetch(sql)
                .map(r -> new Schema(r.getValue("SCHEMA_NAME", String.class),
                        r.getValue("IS_DEFAULT", boolean.class)))
                // could also be written as
                // .into(Schema.class)
                .forEach(System.out::println);

        System.out.println();
        System.out.println("Fetching data into a lambda expression with Spring JDBC");
        System.out.println("-------------------------------------------------------");
        new JdbcTemplate(new SingleConnectionDataSource(c, true))
                .query(sql,
                        (rs, rowNum) -> new Schema(rs.getString("SCHEMA_NAME"), rs.getBoolean("IS_DEFAULT")))
                .forEach(System.out::println);

        System.out.println();
        System.out.println("Fetching data into a lambda expression with Apache DbUtils");
        System.out.println("-------------------------------------------------------");
        new QueryRunner().query(c, sql, new ArrayListHandler()).stream()
                .map(array -> new Schema((String) array[0], (Boolean) array[1])).forEach(System.out::println);
    }
}

From source file:org.danann.cernunnos.sql.DataSourceRetrievalUtil.java

public static DataSource getDataSource(Phrase dataSourcePhrase, Phrase connectionPhrase, TaskRequest req,
        TaskResponse res) {/*from   ww  w  .j a v a2  s. co  m*/
    final DataSource dataSource;
    final Connection conn = (Connection) connectionPhrase.evaluate(req, res);
    if (conn == null) {
        // This is good... this is what we want...
        dataSource = (DataSource) dataSourcePhrase.evaluate(req, res);
    } else {
        // This is *less* good... the Cernunnos XML should be updated...
        if (LOG.isWarnEnabled()) {
            String msg = "The CONNECTION reagent has been deprecated.  Please "
                    + "update the Cernunnos XML to use DATA_SOURCE.";
            LOG.warn(msg);
        }
        dataSource = new SingleConnectionDataSource(conn, false);
    }

    return dataSource;
}

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

protected JdbcTemplate getJdbcTemplate(Connection connection) {
    DataSource dataSource = new SingleConnectionDataSource(connection, true);
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    return jdbcTemplate;
}

From source file:org.cirdles.topsoil.app.sqlite.SQLiteMyBatisModule.java

@Provides
DataSource provideDataSource(@Named("JDBC.url") String url) {
    return new SingleConnectionDataSource(url, SUPPRESS_CLOSE);
}

From source file:org.flywaydb.core.internal.resolver.spring.SpringJdbcMigrationExecutor.java

@Override
public void execute(Connection connection) {
    try {/*from   ww w.  ja  v  a 2 s. c  o m*/
        springJdbcMigration.migrate(new org.springframework.jdbc.core.JdbcTemplate(
                new SingleConnectionDataSource(connection, true)));
    } catch (Exception e) {
        throw new FlywayException("Migration failed !", e);
    }
}

From source file:com.googlecode.flyway.core.resolver.java.JavaMigrationExecutor.java

public void execute(JdbcTemplate jdbcTemplate, DbSupport dbSupport) {
    try {/*from w  w w  .  j  a va 2  s . co  m*/
        javaMigration.migrate(new org.springframework.jdbc.core.JdbcTemplate(
                new SingleConnectionDataSource(jdbcTemplate.getConnection(), true)));
    } catch (Exception e) {
        throw new FlywayException("Migration failed !", e);
    }
}

From source file:com.oracle2hsqldb.SchemaReader.java

public SchemaReader(Configuration config, Connection connection) {
    this.config = config;
    this.dataSource = new SingleConnectionDataSource(connection, true);
}

From source file:com.googlecode.flyway.core.resolver.spring.SpringJdbcMigrationExecutor.java

public void execute(JdbcTemplate jdbcTemplate, DbSupport dbSupport) {
    try {//from  w ww  .ja v  a 2s .  c o m
        springJdbcMigration.migrate(new org.springframework.jdbc.core.JdbcTemplate(
                new SingleConnectionDataSource(jdbcTemplate.getConnection(), true)));
    } catch (Exception e) {
        throw new FlywayException("Migration failed !", e);
    }
}

From source file:test.RowMapperTests.java

@Override
@Before//from www . j  av  a 2s.  c o  m
public void setUp() throws SQLException {
    connection = mock(Connection.class);
    statement = mock(Statement.class);
    preparedStatement = mock(PreparedStatement.class);
    resultSet = mock(ResultSet.class);
    given(connection.createStatement()).willReturn(statement);
    given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
    given(statement.executeQuery(anyString())).willReturn(resultSet);
    given(preparedStatement.executeQuery()).willReturn(resultSet);
    given(resultSet.next()).willReturn(true, true, false);
    given(resultSet.getString(1)).willReturn("tb1", "tb2");
    given(resultSet.getInt(2)).willReturn(1, 2);
    template = new JdbcTemplate();
    template.setDataSource(new SingleConnectionDataSource(connection, false));
    template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
    template.afterPropertiesSet();
}