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.dangdang.ddframe.rdb.transaction.soft.integrate.storage.RdbTransactionLogStorageOperationsTest.java
private void createTable(final DataSource dataSource) throws SQLException { String dbSchema = "CREATE TABLE IF NOT EXISTS `transaction_log` (" + "`id` VARCHAR(40) NOT NULL, " + "`transaction_type` VARCHAR(30) NOT NULL, " + "`data_source` VARCHAR(255) NOT NULL, " + "`sql` TEXT NOT NULL, " + "`parameters` TEXT NOT NULL, " + "`creation_time` LONG NOT NULL, " + "`async_delivery_try_times` INT NOT NULL DEFAULT 0, " + "PRIMARY KEY (`id`));"; try (Connection conn = dataSource.getConnection(); PreparedStatement preparedStatement = conn.prepareStatement(dbSchema)) { preparedStatement.executeUpdate(); }/*from w w w . j av a 2 s .c om*/ }
From source file:org.apereo.services.persondir.support.jdbc.MultiRowJdbcPersonAttributeDaoTest.java
@Override protected void tearDownSchema(final DataSource dataSource) throws SQLException { final Connection con = dataSource.getConnection(); con.prepareStatement("DROP TABLE user_table").execute(); con.close();/*from w w w. ja v a2 s .com*/ }
From source file:com.uber.hoodie.hive.client.HoodieHiveClient.java
private Connection getConnection() throws SQLException { int count = 0; int maxTries = 3; if (connection == null) { Configuration conf = configuration.getConfiguration(); DataSource ds = getDatasource(); LOG.info("Getting Hive Connection from Datasource " + ds); while (true) { try { this.connection = ds.getConnection(); break; } catch (SQLException e) { if (++count == maxTries) throw e; }/* ww w .java 2s.co m*/ } } return connection; }
From source file:org.apache.cayenne.modeler.dialog.db.merge.MergerOptions.java
/** * check database and create the {@link List} of {@link MergerToken}s *///from www. j av a2 s.c om protected void prepareMigrator() { try { adapter = connectionInfo.makeAdapter(getApplication().getClassLoadingService()); MergerTokenFactory mergerTokenFactory = mergerTokenFactoryProvider.get(adapter); tokens.setMergerTokenFactory(mergerTokenFactory); FiltersConfig filters = FiltersConfig.create(defaultCatalog, defaultSchema, TableFilter.everything(), PatternFilter.INCLUDE_NOTHING); DataMapMerger merger = DataMapMerger.builder(mergerTokenFactory).filters(filters).build(); DbLoaderConfiguration config = new DbLoaderConfiguration(); config.setFiltersConfig(filters); DataSource dataSource = connectionInfo.makeDataSource(getApplication().getClassLoadingService()); DataMap dbImport; try (Connection conn = dataSource.getConnection();) { dbImport = new DbLoader(adapter, conn, config, new LoggingDbLoaderDelegate(LogFactory.getLog(DbLoader.class)), new DefaultObjectNameGenerator(NoStemStemmer.getInstance())).load(); } catch (SQLException e) { throw new CayenneRuntimeException("Can't doLoad dataMap from db.", e); } tokens.setTokens(merger.createMergeTokens(dataMap, dbImport)); } catch (Exception ex) { reportError("Error loading adapter", ex); } }
From source file:org.biblionum.ouvrage.modele.OuvrageModele.java
/** * Java method that updates a row in the generated sql table * * @param con (open java.sql.Connection) * @param auteur//from w w w. j av a2 s.c om * @param editeur * @param annee_edition * @param resume * @param nb_page * @param emplacement * @param couverture * @param ouvrageTipeid * @param categorieOuvrageid * @param niveauid_niveau * @param filiereid * @param titre * @return boolean (true on success) * @throws SQLException */ public boolean updateUtilisateur(DataSource ds, int keyId, String auteur, String editeur, int annee_edition, String resume, int nb_page, String emplacement, String couverture, int ouvrageTipeid, int categorieOuvrageid, int niveauid_niveau, int filiereid, String titre) throws SQLException { con = ds.getConnection(); String sql = "SELECT * FROM ouvrage 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 (auteur != null) { entry.updateString("auteur", auteur); } if (editeur != null) { entry.updateString("editeur", editeur); } entry.updateInt("annee_edition", annee_edition); if (resume != null) { entry.updateString("resume", resume); } entry.updateInt("nb_page", nb_page); if (emplacement != null) { entry.updateString("emplacement", emplacement); } if (couverture != null) { entry.updateString("couverture", couverture); } entry.updateInt("ouvrageTipeid", ouvrageTipeid); entry.updateInt("categorieOuvrageid", categorieOuvrageid); entry.updateInt("niveauid_niveau", niveauid_niveau); entry.updateInt("filiereid", filiereid); if (titre != null) { entry.updateString("titre", titre); } entry.updateRow(); entry.close(); statement.close(); con.close(); return true; }
From source file:com.searchbox.framework.config.DataConfiguration.java
@Bean @DependsOn("entityManagerFactory") public ResourceDatabasePopulator initDatabase(DataSource dataSource) throws Exception { ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); populator.setContinueOnError(true);//from w w w . j a v a 2 s. c om populator.addScript(new ClassPathResource("data/oppfinUsers.sql")); populator.addScript(new ClassPathResource("org/springframework/batch/core/schema-mysql.sql")); //populator.addScript(new ClassPathResource("data/spring-batch-h2-schema.sql")); populator.populate(dataSource.getConnection()); return populator; }
From source file:com.alfaariss.oa.engine.attribute.release.jdbc.JDBCPolicy.java
private Vector<String> readAttributes(DataSource dataSource, String attributeTable) throws AttributeException { Vector<String> vAttributeNames = new Vector<String>(); Connection oConnection = null; PreparedStatement oPreparedStatement = null; ResultSet oResultSet = null;//from w ww. ja va2s . com try { oConnection = dataSource.getConnection(); StringBuffer sbSelect = new StringBuffer("SELECT "); sbSelect.append(COLUMN_ATTRIBUTE_ATTRIBUTE); sbSelect.append(" FROM "); sbSelect.append(attributeTable); sbSelect.append(" WHERE "); sbSelect.append(COLUMN_ATTRIBUTE_POLICY_ID); sbSelect.append("=?"); oPreparedStatement = oConnection.prepareStatement(sbSelect.toString()); oPreparedStatement.setString(1, _sID); oResultSet = oPreparedStatement.executeQuery(); while (oResultSet.next()) { vAttributeNames.add(oResultSet.getString(COLUMN_ATTRIBUTE_ATTRIBUTE)); } } catch (Exception e) { _logger.fatal("Internal error during initialization", e); throw new AttributeException(SystemErrors.ERROR_INTERNAL); } finally { try { if (oResultSet != null) oResultSet.close(); } catch (Exception e) { _logger.error("Could not close resultset", e); } try { if (oPreparedStatement != null) oPreparedStatement.close(); } catch (Exception e) { _logger.error("Could not close statement", e); } try { if (oConnection != null) oConnection.close(); } catch (Exception e) { _logger.error("Could not close connection", e); } } return vAttributeNames; }
From source file:org.jasig.services.persondir.support.jdbc.MultiRowJdbcPersonAttributeDaoTest.java
@Override protected void setUpSchema(DataSource dataSource) throws SQLException { Connection con = dataSource.getConnection(); con.prepareStatement(//from w w w .j a va 2s .com "CREATE TABLE user_table " + "(netid VARCHAR, " + "attr_name VARCHAR, " + "attr_val VARCHAR)") .execute(); con.prepareStatement( "INSERT INTO user_table " + "(netid, attr_name, attr_val) " + "VALUES ('awp9', 'name', 'Andrew')") .execute(); con.prepareStatement("INSERT INTO user_table " + "(netid, attr_name, attr_val) " + "VALUES ('awp9', 'email', 'andrew.petro@yale.edu')").execute(); con.prepareStatement("INSERT INTO user_table " + "(netid, attr_name, attr_val) " + "VALUES ('awp9', 'shirt_color', 'blue')").execute(); con.prepareStatement("INSERT INTO user_table " + "(netid, attr_name, attr_val) " + "VALUES ('edalquist', 'name', 'Eric')").execute(); con.prepareStatement("INSERT INTO user_table " + "(netid, attr_name, attr_val) " + "VALUES ('edalquist', 'email', 'edalquist@unicon.net')").execute(); con.prepareStatement("INSERT INTO user_table " + "(netid, attr_name, attr_val) " + "VALUES ('edalquist', 'shirt_color', 'blue')").execute(); con.prepareStatement( "INSERT INTO user_table " + "(netid, attr_name, attr_val) " + "VALUES ('atest', 'name', 'Andrew')") .execute(); con.prepareStatement("INSERT INTO user_table " + "(netid, attr_name, attr_val) " + "VALUES ('atest', 'email', 'andrew.test@test.net')").execute(); con.prepareStatement("INSERT INTO user_table " + "(netid, attr_name, attr_val) " + "VALUES ('atest', 'shirt_color', 'red')").execute(); con.prepareStatement( "INSERT INTO user_table " + "(netid, attr_name, attr_val) " + "VALUES ('susan', 'name', 'Susan')") .execute(); con.prepareStatement("INSERT INTO user_table " + "(netid, attr_name, attr_val) " + "VALUES ('susan', 'email', 'susan.test@test.net')").execute(); con.prepareStatement("INSERT INTO user_table " + "(netid, attr_name, attr_val) " + "VALUES ('susan', 'shirt_color', null)").execute(); con.close(); }
From source file:nl.opengeogroep.dbk.DBKAPI.java
public Connection getConnection() { try {// w w w . j a va2 s. c o m InitialContext cxt = new InitialContext(); if (cxt == null) { throw new Exception("Uh oh -- no context!"); } DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/dbk-api"); if (ds == null) { throw new Exception("Data source not found!"); } Connection connection = ds.getConnection(); return connection; } catch (NamingException ex) { log.error("naming", ex); } catch (Exception ex) { log.error("exception", ex); } return null; }