List of usage examples for javax.sql DataSource getConnection
Connection getConnection() throws SQLException;
Attempts to establish a connection with the data source that this DataSource object represents.
From source file:com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBeanTest.java
@Test public void shouldRespectTransactionAwareAttribute() throws Exception { DataSource dataSource = mock(DataSource.class); Connection connection = mock(Connection.class); given(dataSource.getConnection()).willReturn(connection); this.factoryBean.setDataSource(dataSource); this.factoryBean.setTransactionAware(false); DatabaseDataSourceConnection dataSourceConnection = this.factoryBean.getObject(); Connection actual = dataSourceConnection.getConnection(); assertSame(connection, actual);/*from ww w . j a v a2 s . co m*/ }
From source file:com.musala.configuration.RssAplicationConfiguration.java
@Bean(name = "dataSource") public DataSource dataSource() throws SQLException { DataSource dataSource = createDataSource(); DatabasePopulatorUtils.execute(createDatabasePopulator(), dataSource); System.out.println(dataSource.getConnection()); return dataSource; }
From source file:com.ribas.andrei.web.DBDataServlet.java
private Collection<DBSchema> getDataFromDataSource(DataSource dataSource) throws SQLException { Collection<DBSchema> dbSchemas = null; try (Connection connection = dataSource.getConnection()) { String sql = "select schema_name, is_default from information_schema.schemata order by schema_name"; try (PreparedStatement stmt = connection.prepareStatement(sql); ResultSet rs = stmt.executeQuery()) { while (rs.next()) { if (dbSchemas == null) { dbSchemas = new ArrayList<>(); }/*from ww w . j a v a 2 s.c om*/ dbSchemas.add(new DBSchema(rs.getString("SCHEMA_NAME"), rs.getBoolean("IS_DEFAULT"))); } } } return dbSchemas; }
From source file:org.biblionum.authentification.modele.UtilisateurModele.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//from w ww . j a v a 2 s .c o m */ public void deleteFromUtilisateur(DataSource ds, int keyId) throws SQLException { con = ds.getConnection(); String sql = "DELETE FROM utilisateur WHERE id = ?"; PreparedStatement statement = con.prepareStatement(sql); statement.setInt(1, keyId); statement.executeUpdate(); statement.close(); con.close(); }
From source file:br.com.manish.ahy.kernel.util.DAOUtil.java
public DAOUtil(DataSource ds) { try {/*from ww w . j av a2 s.co m*/ log.debug("Connecting to database."); con = ds.getConnection(); stmt = con.createStatement(); log.debug("Connection suceeded."); } catch (Exception e) { throw new OopsException(e, "Problem when acessing database."); } }
From source file:fll.web.api.TournamentTeamsServlet.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 {/*from w w w. j a v a 2 s .c om*/ connection = datasource.getConnection(); final Map<Integer, TournamentTeam> teamMap = Queries.getTournamentTeams(connection); final ObjectMapper jsonMapper = new ObjectMapper(); response.reset(); response.setContentType("application/json"); final PrintWriter writer = response.getWriter(); final String pathInfo = request.getPathInfo(); if (null != pathInfo && pathInfo.length() > 1) { final String teamNumberStr = pathInfo.substring(1); try { final int teamNumber = Integer.parseInt(teamNumberStr); LOGGER.info("Found team number: " + teamNumber); final TournamentTeam team = teamMap.get(teamNumber); if (null != team) { jsonMapper.writeValue(writer, team); return; } else { throw new RuntimeException("No team found with number " + teamNumber); } } catch (final NumberFormatException e) { throw new RuntimeException("Error parsing team number " + teamNumberStr, e); } } final Collection<TournamentTeam> teams = teamMap.values(); jsonMapper.writeValue(writer, teams); } catch (final SQLException e) { throw new RuntimeException(e); } finally { SQLFunctions.close(connection); } }
From source file:org.biblionum.ouvrage.modele.OuvrageModele.java
/** * Java method that creates the generated table * * @param con (open java.sql.Connection) * @throws SQLException/* ww w . j a v a 2s . c o m*/ */ public boolean createTableUtilisateur(DataSource ds) throws SQLException { con = ds.getConnection(); Statement statement = con.createStatement(); String sql = "CREATE TABLE utilisateur(id int AUTO_INCREMENT, " + "`auteur` VARCHAR(255), " + "`editeur` VARCHAR(255), " + "`annee_edition` INT, " + "`resume` VARCHAR(255), " + "`nb_page` INT, " + "`emplacement` VARCHAR(255), " + "`couverture` VARCHAR(255), " + "`ouvrageTipeid` INT, " + "`categorieOuvrageid` INT, " + "`niveauid_niveau` INT, " + "`filiereid` INT, " + "`titre` VARCHAR(255),)"; statement.execute(sql); statement.close(); con.close(); return true; }
From source file:com.teradata.benchto.driver.jdbc.ConnectionPoolTest.java
private Callable createQueryRunnable(DataSource dataSource, CountDownLatch countDownLatch) { return () -> { // open new connection try (Connection connection = dataSource.getConnection()) { // wait till all the other connections get opened countDownLatch.countDown();/* w w w.jav a2 s. co m*/ countDownLatch.await(1, MINUTES); //check that connection is alive checkThatConnectionAlive(connection); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } return null; }; }
From source file:com.jedi.oracle.OracleCall.java
@Override public void execute(DataSource dataSource) throws Exception { Connection connection = dataSource.getConnection(); try {//from www . ja va 2 s.c o m this.execute(connection); } finally { if (!connection.isClosed()) { connection.close(); } } }
From source file:com.csc.fsg.life.dao.bo.AbstractBusinessObjectHandler.java
/** Called to handle a command. /*from w w w.j a v a 2s. c o m*/ Wraps the {@link #handle(AbstractBusinessObject, BusinessObjectCommand, Connection)} method by creating a connection and ensuring the connection is closed. @param dataSource the dataSource. @throws BusinessObjectException If there is a business error with the command. @throws SQLException If there is an I/O error with the command. **/ public void handle(DataSource dataSource) throws BusinessObjectException, SQLException { Connection conn = null; try { conn = dataSource.getConnection(); // turn auto-commit off conn.setAutoCommit(false); // call the concrete classes handle method handle(bo, command, conn); // since we are successful, commit the transaction conn.commit(); } catch (SQLException e) { if (conn != null) conn.rollback(); throw new SQLException(exceptionHandler.getReadableMessage(e)); } catch (BusinessObjectException e) { if (conn != null) conn.rollback(); throw new BusinessObjectException(exceptionHandler.getReadableMessage(e)); } catch (Exception e) { if (conn != null) conn.rollback(); throw new BusinessObjectException(exceptionHandler.getReadableMessage(e)); } finally { try { if (conn != null) conn.close(); } catch (Exception cme) { logger.error("Connection close exception: " + cme.getMessage()); } } }