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.micromux.cassandra.jdbc.PooledTest.java

@Test
public void statementClosed() throws Exception {
    CassandraDataSource connectionPoolDataSource = new CassandraDataSource(HOST, PORT, KEYSPACE, USER, PASSWORD,
            VERSION, CONSISTENCY, TRUST_STORE, TRUST_PASS);

    DataSource pooledCassandraDataSource = new PooledCassandraDataSource(connectionPoolDataSource);

    Connection connection = pooledCassandraDataSource.getConnection();

    Statement statement = connection.createStatement();

    ResultSet resultSet = statement.executeQuery("SELECT someInt FROM pooled_test WHERE somekey = 'world'");
    assertTrue(resultSet.next());/*from  w  w  w.ja  v  a 2  s . com*/
    assertEquals(1, resultSet.getInt(1));
    assertFalse(resultSet.next());
    resultSet.close();

    connection.close();

    assert statement.isClosed();
}

From source file:com.micromux.cassandra.jdbc.PooledTest.java

@Test
public void preparedStatement() throws Exception {
    CassandraDataSource connectionPoolDataSource = new CassandraDataSource(HOST, PORT, KEYSPACE, USER, PASSWORD,
            VERSION, CONSISTENCY, TRUST_STORE, TRUST_PASS);

    DataSource pooledCassandraDataSource = new PooledCassandraDataSource(connectionPoolDataSource);

    Connection connection = pooledCassandraDataSource.getConnection();

    PreparedStatement statement = connection
            .prepareStatement("SELECT someint FROM pooled_test WHERE somekey = ?");
    statement.setString(1, "world");

    ResultSet resultSet = statement.executeQuery();
    assertTrue(resultSet.next());//w ww. j  ava  2s .c om
    assertEquals(1, resultSet.getInt(1));
    assertFalse(resultSet.next());
    resultSet.close();

    statement.close();
    connection.close();
}

From source file:com.alibaba.druid.benckmark.pool.Oracle_Case4.java

private void printAV_INFO(DataSource dataSource) throws SQLException {
    String sql = "SELECT DISTINCT ID FROM AV_INFO WHERE ROWNUM <= 10";
    Connection conn = dataSource.getConnection();
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(sql);

    JdbcUtils.printResultSet(rs);// w ww  .j  a  v a 2 s.  c o  m

    rs.close();
    stmt.close();
    conn.close();
}

From source file:com.micromux.cassandra.jdbc.PooledTest.java

@Test
public void preparedStatementClose() throws Exception {
    CassandraDataSource connectionPoolDataSource = new CassandraDataSource(HOST, PORT, KEYSPACE, USER, PASSWORD,
            VERSION, CONSISTENCY, TRUST_STORE, TRUST_PASS);

    DataSource pooledCassandraDataSource = new PooledCassandraDataSource(connectionPoolDataSource);

    Connection connection = pooledCassandraDataSource.getConnection();

    PreparedStatement statement = connection
            .prepareStatement("SELECT someInt FROM pooled_test WHERE somekey = ?");
    statement.setString(1, "world");

    ResultSet resultSet = statement.executeQuery();
    assertTrue(resultSet.next());//from w w w  .  j av  a  2s  .c o  m
    assertEquals(1, resultSet.getInt(1));
    assertFalse(resultSet.next());
    resultSet.close();

    connection.close();

    assert statement.isClosed();
}

From source file:fll.web.api.JudgesServlet.java

@Override
protected final void doGet(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException, ServletException {
    final ServletContext application = getServletContext();

    final DataSource datasource = ApplicationAttributes.getDataSource(application);
    Connection connection = null;
    try {/*w  w w  .  ja v a2s. c  om*/
        connection = datasource.getConnection();

        final ObjectMapper jsonMapper = new ObjectMapper();

        response.reset();
        response.setContentType("application/json");
        final PrintWriter writer = response.getWriter();

        final int currentTournament = Queries.getCurrentTournament(connection);
        final Collection<JudgeInformation> judges = JudgeInformation.getJudges(connection, currentTournament);

        jsonMapper.writeValue(writer, judges);
    } catch (final SQLException e) {
        throw new RuntimeException(e);
    } finally {
        SQLFunctions.close(connection);
    }

}

From source file:Controllers.AppointmentController.java

/**
 * return list on all appointments./*from w  w w .j ava  2 s . c  o  m*/
 *
 * @return
 */
public Appointment[] fetchAppointments() {

    Appointment[] appointments = null;

    try {
        Context ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("jdbc/medicalCareDataSource");
        connection = ds.getConnection();

        Statement stmt = connection.createStatement();

        String smtquery = "SELECT * FROM appointments";
        ResultSet resultSet;
        resultSet = stmt.executeQuery(smtquery);

        List<Appointment> appointmentsList = new ArrayList<Appointment>();
        while (resultSet.next()) {
            Appointment appointment = new Appointment();
            appointment.setAccountId(resultSet.getInt("accountId"));
            appointment.setAppointmentId(resultSet.getInt("appointmentId"));
            appointment.setDate(resultSet.getDate("date"));
            appointment.setDepartmentId(resultSet.getInt("departmentId"));
            appointment.setMessage(resultSet.getString("message"));
            appointmentsList.add(appointment);
        }

        appointments = new Appointment[appointmentsList.size()];
        appointments = appointmentsList.toArray(appointments);

        stmt.close();

    } catch (NamingException ex) {
        Logger.getLogger(AppointmentController.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(AppointmentController.class.getName()).log(Level.SEVERE, null, ex);
    }

    return appointments;
}

From source file:edu.cornell.mannlib.vitro.webapp.triplesource.impl.sdb.ContentTripleSourceSDB.java

private Store connectStore(DataSource bds, StoreDesc storeDesc) throws SQLException {
    SDBConnection conn = new SDBConnection(bds.getConnection());
    return SDBFactory.connectStore(conn, storeDesc);
}

From source file:org.biblionum.authentification.modele.UtilisateurModele.java

/**
 * Java method that updates a row in the generated sql table
 *
 * @param con (open java.sql.Connection)
 * @param nom/*from w  ww.  java 2  s .co  m*/
 * @param password
 * @param pseudo
 * @param prenom
 * @param utilisateur_type_id
 * @return boolean (true on success)
 * @throws SQLException
 */
public boolean updateUtilisateur(DataSource ds, int keyId, String nom, String password, String pseudo,
        String prenom) throws SQLException {
    con = ds.getConnection();
    String sql = "SELECT * FROM utilisateur WHERE id = ?";
    PreparedStatement statement = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_UPDATABLE);
    statement.setInt(1, keyId);
    ResultSet entry = statement.executeQuery();

    entry.last();
    int rows = entry.getRow();
    entry.beforeFirst();
    if (rows == 0) {
        entry.close();
        statement.close();
        con.close();
        return false;
    }
    entry.next();

    if (nom != null) {
        entry.updateString("nom", nom);
    }
    if (password != null) {
        entry.updateString("password", password);
    }
    if (pseudo != null) {
        entry.updateString("pseudo", pseudo);
    }
    if (prenom != null) {
        entry.updateString("prenom", prenom);
    }

    entry.updateRow();
    entry.close();
    statement.close();
    con.close();
    return true;
}

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

/**
 * get utilisatuer par type et categorie
 *//*from ww w  .j  a v a2s  . c o m*/

public ArrayList<Ouvrage> getOuvrage(DataSource ds, int type, int categorie) {

    ArrayList<Ouvrage> list = new ArrayList<Ouvrage>();

    try {
        con = ds.getConnection();
        stm = con.prepareStatement("SELECT * FROM ouvrage WHERE CategorieOuvrageid=? AND OuvrageTypeid=?");
        stm.setInt(1, categorie);//type user prof 1
        stm.setInt(2, type);
        rs = stm.executeQuery();

        BeanProcessor bp = new BeanProcessor();
        list = (ArrayList) bp.toBeanList(rs, Ouvrage.class);

        con.close();
        rs.close();

    } catch (SQLException ex) {
        Logger.getLogger(UtilisateurModele.class.getName()).log(Level.SEVERE, null, ex);
    }

    return list;
}