Example usage for javax.sql DataSource getConnection

List of usage examples for javax.sql DataSource getConnection

Introduction

In this page you can find the example usage for javax.sql DataSource getConnection.

Prototype

Connection getConnection() throws SQLException;

Source Link

Document

Attempts to establish a connection with the data source that this DataSource object represents.

Usage

From source file:com.dbconnection.DataSourceConnection.java

public JdbcRowSet getJdbcRowSet() throws SQLException {

    ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
    DataSource datSour = (DataSource) appContext.getBean("dataSource");

    JdbcRowSet jr = new JdbcRowSetImpl(datSour.getConnection());
    return jr;//from ww  w. ja  va2 s  .  co m
}

From source file:com.manydesigns.portofino.model.database.JndiConnectionProvider.java

public Connection acquireConnection() throws Exception {
    DataSource ds = getDataSource();
    return ds.getConnection();
}

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

@Test
public void h2() throws Exception {
    DataSource ds = new H2MemorySingleConnectionDataSource();
    Connection cnn = ds.getConnection();
    cnn.createStatement()/*w w  w .  j a v a 2 s . co m*/
            .execute("CREATE TABLE BOCAS_TABLE(BOCAS_KEY BINARY(32) PRIMARY KEY, BOCAS_VALUE BLOB)");
    cnn.close();
    test(JDBCBocasServices.newBuilder().build(ds));
}

From source file:org.hsweb.web.mybatis.SpringApplication.java

@Bean
public SqlExecutor sqlExecutor(DataSource dataSource) throws SQLException {
    Connection connection = dataSource.getConnection();
    try {//w w  w  .ja  v a  2  s .  c  om
        DataSourceHolder.install(dataSource, DatabaseType.fromJdbcUrl(connection.getMetaData().getURL()));
    } finally {
        connection.close();
    }
    return new AbstractJdbcSqlExecutor() {
        @Override
        public Connection getConnection() {
            return DataSourceUtils.getConnection(dataSource);
        }

        @Override
        public void releaseConnection(Connection connection) throws SQLException {
            DataSourceUtils.releaseConnection(connection, dataSource);
        }
    };
}

From source file:mvc.dao.UsuarioDAO.java

@Autowired
public UsuarioDAO(DataSource dataSource) {
    try {/*www .j  a va 2  s . c om*/
        this.connection = dataSource.getConnection();
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:id.go.kemdikbud.tandajasa.dao.GolonganDaoTest.java

@Before
public void resetDatabase() {
    try {/*from ww  w .j  a v  a 2  s .  com*/
        DataSource ds = ctx.getBean(DataSource.class);
        Connection koneksiDatabase = ds.getConnection();
        DatabaseOperation.CLEAN_INSERT.execute(new DatabaseConnection(koneksiDatabase),
                new FlatXmlDataSetBuilder().build(new File("src/test/resources/pegawai.xml")));
        koneksiDatabase.close();
    } catch (Exception ex) {
        Logger.getLogger(GolonganDaoTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Main.java

private Connection getConnection() {
    Connection connection = null;
    try {//from w  w  w. ja  v  a  2 s . co  m
        InitialContext context = new InitialContext();
        DataSource dataSource = (DataSource) context.lookup("jdbc/DataSource");
        connection = dataSource.getConnection();
    } catch (NamingException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return connection;
}

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()/*  ww w . j  av  a  2  s .c o  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:com.gopivotal.cloudfoundry.test.core.DataSourceUtils.java

public String checkAccess(DataSource dataSource) {
    try {//w ww  .ja v a  2  s  . co m
        Connection connection = dataSource.getConnection();
        PreparedStatement selectOne = connection.prepareStatement(SELECT_ONE);
        selectOne.execute();
        return "ok";
    } catch (SQLException e) {
        return "failed with " + e.getMessage();
    }
}

From source file:vn.edu.vnuk.tasks_jpa.dao.TaskDao.java

@Autowired
public TaskDao(DataSource dataSource) throws SQLException {
    this.connection = dataSource.getConnection();
}