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:org.compass.gps.device.jdbc.JdbcUtils.java

/**
 * Returns a jdbc connection, and in case of failure, wraps the sql
 * exception with a Jdbc device exception.
 * //from  w  w w.  ja  va  2  s  .c om
 * @param dataSource
 * @return A connection from the datasource.
 * @throws JdbcGpsDeviceException
 */
public static Connection getConnection(DataSource dataSource) throws JdbcGpsDeviceException {
    try {
        return dataSource.getConnection();
    } catch (SQLException e) {
        throw new JdbcGpsDeviceException("Failed to open jdbc connection", e);
    }
}

From source file:org.biblionum.ouvrage.modele.CategorieOuvrageModele.java

/**
 * Java method that creates the generated table
 *
 * @param con (open java.sql.Connection)
 * @throws SQLException//from   w  ww  .  j  ava 2s . c o  m
 */
public static boolean createTableCategorieouvrage(DataSource ds) throws SQLException {
    con = ds.getConnection();
    Statement statement = con.createStatement();
    String sql = "CREATE TABLE categorieouvrage(id int AUTO_INCREMENT, "
            + "`designation_categorie` VARCHAR(255),)";
    statement.execute(sql);
    statement.close();
    con.close();
    return true;
}

From source file:com.alibaba.druid.pool.bonecp.TestPSCache.java

public static void f(DataSource ds, int count) throws Exception {
    Connection conn = ds.getConnection();

    for (int i = 0; i < count; ++i) {
        PreparedStatement stmt = conn.prepareStatement("SELECT 1");
        System.out.println(System.identityHashCode(unwrap(stmt)));
        stmt.close();/*from w w w .  j  a  v a2s  .  c o  m*/
    }

    conn.close();
}

From source file:io.kazuki.v0.internal.helper.TestHelper.java

public static void dropSchema(DataSource database) {
    Connection conn = null;/* ww  w.ja v a2s  . com*/
    try {
        conn = database.getConnection();
        conn.prepareStatement("DROP ALL OBJECTS").execute();
        conn.commit();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
        }
    }

}

From source file:com.jt.leave.tools.DataFixtures.java

/**
 * DataSource?Connection(??)?url??Connection.
 *///from   w ww. jav  a 2s  .  c o  m
protected static IDatabaseConnection getConnection(DataSource dataSource)
        throws DatabaseUnitException, SQLException {
    Connection connection = dataSource.getConnection();
    String jdbcUrl = connection.getMetaData().getURL();
    if (StringUtils.contains(jdbcUrl, ":h2:")) {
        return new H2Connection(connection, null);
    } else if (StringUtils.contains(jdbcUrl, ":mysql:")) {
        return new MySqlConnection(connection, null);
    } else if (StringUtils.contains(jdbcUrl, ":oracle:")) {
        return new OracleConnection(connection, null);
    } else {
        return new DatabaseConnection(connection);
    }
}

From source file:com.micmiu.modules.test.data.DataFixtures.java

/**
 * DataSource?Connection(??)?url??Connection.
 *//*  w ww  .j ava 2 s  .  c o  m*/
protected static IDatabaseConnection getConnection(DataSource dataSource)
        throws DatabaseUnitException, SQLException {
    Connection connection = dataSource.getConnection();
    String dbName = connection.getMetaData().getURL();
    if (StringUtils.contains(dbName, ":h2:")) {
        return new H2Connection(connection, null);
    } else if (StringUtils.contains(dbName, ":mysql:")) {
        return new MySqlConnection(connection, null);
    } else if (StringUtils.contains(dbName, ":oracle:")) {
        return new OracleConnection(connection, null);
    } else {
        return new DatabaseConnection(connection);
    }
}

From source file:org.biblionum.ouvrage.modele.CategorieOuvrageModele.java

/**
 * Java method that deletes a row from the generated sql table
 *
 * @param con (open java.sql.Connection)
 * @param keyId (the primary key to the row)
 * @throws SQLException//w  w  w.j a  v a  2  s  .  c  om
 */
public static void deleteFromCategorieouvrage(DataSource ds, int keyId) throws SQLException {
    con = ds.getConnection();
    String sql = "DELETE FROM categorieouvrage WHERE id = ?";
    PreparedStatement statement = con.prepareStatement(sql);
    statement.setInt(1, keyId);
    statement.executeUpdate();
    statement.close();
    con.close();
}

From source file:org.red5.server.plugin.admin.dao.UserDAO.java

public static boolean addUser(String username, String hashedPassword) {
    boolean result = false;

    Connection conn = null;/*from  w  ww .  j  ava2  s .c o m*/
    PreparedStatement stmt = null;
    try {
        // JDBC stuff
        DataSource ds = UserDatabase.getDataSource();

        conn = ds.getConnection();
        //make a statement
        stmt = conn
                .prepareStatement("INSERT INTO APPUSER (username, password, enabled) VALUES (?, ?, 'enabled')");
        stmt.setString(1, username);
        stmt.setString(2, hashedPassword);
        log.debug("Add user: {}", stmt.execute());
        //add role
        stmt = conn.prepareStatement("INSERT INTO APPROLE (username, authority) VALUES (?, 'ROLE_SUPERVISOR')");
        stmt.setString(1, username);
        log.debug("Add role: {}", stmt.execute());
        //
        result = true;
    } catch (Exception e) {
        log.error("Error connecting to db", e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }
    return result;
}

From source file:org.springside.modules.test.data.H2Fixtures.java

protected static H2Connection getConnection(DataSource dataSource) throws DatabaseUnitException, SQLException {
    return new H2Connection(dataSource.getConnection(), null);
}

From source file:org.springside.modules.test.utils.DbUnitUtils.java

/**
 * DBUnit OperationXML??./*w w  w.  j  ava 2 s .  c  o m*/
 * 
 * @param xmlFilePaths ?Spring Resource?.
 */
private static void execute(DatabaseOperation operation, DataSource h2DataSource, String... xmlFilePaths)
        throws DatabaseUnitException, SQLException {
    IDatabaseConnection connection = new H2Connection(h2DataSource.getConnection(), "");
    for (String xmlPath : xmlFilePaths) {
        try {
            InputStream input = resourceLoader.getResource(xmlPath).getInputStream();
            IDataSet dataSet = new FlatXmlDataSetBuilder().setColumnSensing(true).build(input);
            operation.execute(connection, dataSet);
        } catch (IOException e) {
            logger.warn(xmlPath + " file not found", e);
        }
    }
}