javax.sql.DataSource.getConnection()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(586)

本文整理了Java中javax.sql.DataSource.getConnection()方法的一些代码示例,展示了DataSource.getConnection()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。DataSource.getConnection()方法的具体详情如下:
包路径:javax.sql.DataSource
类名称:DataSource
方法名:getConnection

DataSource.getConnection介绍

[英]Creates a connection to the database represented by this DataSource.
[中]创建与此数据源表示的数据库的连接。

代码示例

代码示例来源:origin: spring-projects/spring-framework

  1. @Override
  2. public void shutdown(DataSource dataSource, String databaseName) {
  3. Connection con = null;
  4. try {
  5. con = dataSource.getConnection();
  6. if (con != null) {
  7. con.createStatement().execute("SHUTDOWN");
  8. }
  9. }
  10. catch (SQLException ex) {
  11. logger.info("Could not shut down embedded database", ex);
  12. }
  13. finally {
  14. if (con != null) {
  15. try {
  16. con.close();
  17. }
  18. catch (Throwable ex) {
  19. logger.debug("Could not close JDBC Connection on shutdown", ex);
  20. }
  21. }
  22. }
  23. }

代码示例来源:origin: elasticjob/elastic-job-lite

  1. private String getOriginalTaskId(final String taskId) {
  2. String sql = String.format("SELECT original_task_id FROM %s WHERE task_id = '%s' and state='%s' LIMIT 1", TABLE_JOB_STATUS_TRACE_LOG, taskId, State.TASK_STAGING);
  3. String result = "";
  4. try (
  5. Connection conn = dataSource.getConnection();
  6. PreparedStatement preparedStatement = conn.prepareStatement(sql);
  7. ResultSet resultSet = preparedStatement.executeQuery()
  8. ) {
  9. if (resultSet.next()) {
  10. return resultSet.getString("original_task_id");
  11. }
  12. } catch (final SQLException ex) {
  13. // TODO 记录失败直接输出日志,未来可考虑配置化
  14. log.error(ex.getMessage());
  15. }
  16. return result;
  17. }

代码示例来源:origin: apache/incubator-gobblin

  1. @Override
  2. public void delete(String storeName) throws IOException {
  3. try (Connection connection = dataSource.getConnection();
  4. PreparedStatement deleteStatement = connection.prepareStatement(DELETE_JOB_STORE_SQL)) {
  5. deleteStatement.setString(1, storeName);
  6. deleteStatement.executeUpdate();
  7. connection.commit();
  8. } catch (SQLException e) {
  9. throw new IOException("failure deleting storeName " + storeName, e);
  10. }
  11. }

代码示例来源:origin: apache/geode

  1. public static void listTableData(String tableName) throws NamingException, SQLException {
  2. Context ctx = cache.getJNDIContext();
  3. DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource");
  4. String sql = "select * from " + tableName;
  5. Connection conn = ds.getConnection();
  6. Statement sm = conn.createStatement();
  7. ResultSet rs = sm.executeQuery(sql);
  8. while (rs.next()) {
  9. System.out.println("id " + rs.getString(1) + " name " + rs.getString(2));
  10. }
  11. rs.close();
  12. conn.close();
  13. }

代码示例来源:origin: apache/geode

  1. /**
  2. * This method is used to return number rows from the timestamped table created by createTable()
  3. * in CacheUtils class.
  4. */
  5. public int getRows(String tableName) throws NamingException, SQLException {
  6. Context ctx = cache.getJNDIContext();
  7. DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource");
  8. String sql = "select * from " + tableName;
  9. Connection conn = ds.getConnection();
  10. Statement sm = conn.createStatement();
  11. ResultSet rs = sm.executeQuery(sql);
  12. int counter = 0;
  13. while (rs.next()) {
  14. counter++;
  15. // System.out.println("id "+rs.getString(1)+ " name "+rs.getString(2));
  16. }
  17. rs.close();
  18. conn.close();
  19. return counter;
  20. }

代码示例来源:origin: looly/hutool

  1. /**
  2. * 获得表的所有列名
  3. *
  4. * @param ds 数据源
  5. * @param tableName 表名
  6. * @return 列数组
  7. * @throws DbRuntimeException SQL执行异常
  8. */
  9. public static String[] getColumnNames(DataSource ds, String tableName) {
  10. List<String> columnNames = new ArrayList<String>();
  11. Connection conn = null;
  12. ResultSet rs = null;
  13. try {
  14. conn = ds.getConnection();
  15. final DatabaseMetaData metaData = conn.getMetaData();
  16. rs = metaData.getColumns(conn.getCatalog(), null, tableName, null);
  17. while (rs.next()) {
  18. columnNames.add(rs.getString("COLUMN_NAME"));
  19. }
  20. return columnNames.toArray(new String[columnNames.size()]);
  21. } catch (Exception e) {
  22. throw new DbRuntimeException("Get columns error!", e);
  23. } finally {
  24. DbUtil.close(rs, conn);
  25. }
  26. }

代码示例来源:origin: voldemort/voldemort

  1. @Override
  2. public ClosableIterator<Pair<ByteArray, Versioned<byte[]>>> entries() {
  3. Connection conn = null;
  4. PreparedStatement stmt = null;
  5. ResultSet rs = null;
  6. String select = "select key_, version_, value_ from " + getName();
  7. try {
  8. conn = datasource.getConnection();
  9. stmt = conn.prepareStatement(select);
  10. rs = stmt.executeQuery();
  11. return new MysqlClosableIterator(conn, stmt, rs);
  12. } catch(SQLException e) {
  13. throw new PersistenceFailureException("Fix me!", e);
  14. }
  15. }

代码示例来源:origin: yu199195/hmily

  1. private int executeUpdate(final String sql, final Object... params) {
  2. Connection connection = null;
  3. PreparedStatement ps = null;
  4. try {
  5. connection = dataSource.getConnection();
  6. ps = connection.prepareStatement(sql);
  7. if (params != null) {
  8. for (int i = 0; i < params.length; i++) {
  9. ps.setObject(i + 1, convertDataTypeToDB(params[i]));
  10. }
  11. }
  12. return ps.executeUpdate();
  13. } catch (SQLException e) {
  14. LOGGER.error("executeUpdate-> " + e.getMessage());
  15. return FAIL_ROWS;
  16. } finally {
  17. close(connection, ps, null);
  18. }
  19. }

代码示例来源:origin: deeplearning4j/nd4j

  1. /**
  2. * Delete the given ndarray
  3. *
  4. * @param id the id of the ndarray to delete
  5. */
  6. @Override
  7. public void delete(String id) throws SQLException {
  8. Connection c = dataSource.getConnection();
  9. PreparedStatement p = c.prepareStatement(deleteStatement());
  10. p.setString(1, id);
  11. p.execute();
  12. }
  13. }

代码示例来源:origin: elasticjob/elastic-job-lite

  1. private int getEventCount(final String tableName, final Collection<String> tableFields, final Condition condition) {
  2. int result = 0;
  3. try (
  4. Connection conn = dataSource.getConnection();
  5. PreparedStatement preparedStatement = createCountPreparedStatement(conn, tableName, tableFields, condition);
  6. ResultSet resultSet = preparedStatement.executeQuery()
  7. ) {
  8. resultSet.next();
  9. result = resultSet.getInt(1);
  10. } catch (final SQLException ex) {
  11. // TODO 记录失败直接输出日志,未来可考虑配置化
  12. log.error("Fetch EventCount from DB error:", ex);
  13. }
  14. return result;
  15. }

代码示例来源:origin: stagemonitor/stagemonitor

  1. private void executePreparedStatement() throws SQLException {
  2. try (final Connection connection = dataSource.getConnection()) {
  3. final PreparedStatement preparedStatement = connection.prepareStatement("SELECT * from STAGEMONITOR");
  4. preparedStatement.execute();
  5. final ResultSet resultSet = preparedStatement.getResultSet();
  6. resultSet.next();
  7. }
  8. }

代码示例来源:origin: voldemort/voldemort

  1. public void executeQuery(DataSource datasource, String query) throws SQLException {
  2. Connection c = datasource.getConnection();
  3. PreparedStatement s = c.prepareStatement(query);
  4. s.execute();
  5. }

代码示例来源:origin: apache/incubator-gobblin

  1. @Override
  2. public boolean exists(String storeName, String tableName) throws IOException {
  3. try (Connection connection = dataSource.getConnection();
  4. PreparedStatement queryStatement = connection.prepareStatement(SELECT_JOB_STATE_EXISTS_SQL)) {
  5. int index = 0;
  6. queryStatement.setString(++index, storeName);
  7. queryStatement.setString(++index, tableName);
  8. try (ResultSet rs = queryStatement.executeQuery()) {
  9. if (rs.next()) {
  10. return true;
  11. } else {
  12. return false;
  13. }
  14. }
  15. } catch (SQLException e) {
  16. throw new IOException("Failure checking existence of storeName " + storeName + " tableName " + tableName, e);
  17. }
  18. }

代码示例来源:origin: apache/incubator-gobblin

  1. @Override
  2. public void delete(String storeName, String tableName) throws IOException {
  3. try (Connection connection = dataSource.getConnection();
  4. PreparedStatement deleteStatement = connection.prepareStatement(DELETE_JOB_STATE_SQL)) {
  5. int index = 0;
  6. deleteStatement.setString(++index, storeName);
  7. deleteStatement.setString(++index, tableName);
  8. deleteStatement.executeUpdate();
  9. connection.commit();
  10. } catch (SQLException e) {
  11. throw new IOException("failure deleting storeName " + storeName + " tableName " + tableName, e);
  12. }
  13. }

代码示例来源:origin: apache/rocketmq-externals

  1. private void initPositionFromBinlogTail() throws SQLException {
  2. String sql = "SHOW MASTER STATUS";
  3. Connection conn = null;
  4. ResultSet rs = null;
  5. try {
  6. Connection connection = dataSource.getConnection();
  7. rs = connection.createStatement().executeQuery(sql);
  8. while (rs.next()) {
  9. binlogFilename = rs.getString("File");
  10. nextPosition = rs.getLong("Position");
  11. }
  12. } finally {
  13. if (conn != null) {
  14. conn.close();
  15. }
  16. if (rs != null) {
  17. rs.close();
  18. }
  19. }
  20. }

代码示例来源:origin: apache/geode

  1. /**
  2. * This method is used to delete all rows from the timestamped table created by createTable() in
  3. * CacheUtils class.
  4. */
  5. public int deleteRows(String tableName) throws NamingException, SQLException {
  6. Context ctx = cache.getJNDIContext();
  7. DataSource da = (DataSource) ctx.lookup("java:/SimpleDataSource"); // doesn't req txn
  8. Connection conn = da.getConnection();
  9. Statement stmt = conn.createStatement();
  10. int rowsDeleted = 0; // assume that rows are always inserted in CacheUtils
  11. String sql = "";
  12. sql = "select * from " + tableName;
  13. ResultSet rs = stmt.executeQuery(sql);
  14. if (rs.next()) {
  15. sql = "delete from " + tableName;
  16. rowsDeleted = stmt.executeUpdate(sql);
  17. }
  18. rs.close();
  19. stmt.close();
  20. conn.close();
  21. return rowsDeleted;
  22. }

代码示例来源:origin: looly/hutool

  1. /**
  2. * 获得表的所有列名
  3. *
  4. * @param ds 数据源
  5. * @param tableName 表名
  6. * @return 列数组
  7. * @throws DbRuntimeException SQL执行异常
  8. */
  9. public static String[] getColumnNames(DataSource ds, String tableName) {
  10. List<String> columnNames = new ArrayList<String>();
  11. Connection conn = null;
  12. ResultSet rs = null;
  13. try {
  14. conn = ds.getConnection();
  15. final DatabaseMetaData metaData = conn.getMetaData();
  16. rs = metaData.getColumns(conn.getCatalog(), null, tableName, null);
  17. while (rs.next()) {
  18. columnNames.add(rs.getString("COLUMN_NAME"));
  19. }
  20. return columnNames.toArray(new String[columnNames.size()]);
  21. } catch (Exception e) {
  22. throw new DbRuntimeException("Get columns error!", e);
  23. } finally {
  24. DbUtil.close(rs, conn);
  25. }
  26. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Before
  2. public void setUp() throws Exception {
  3. this.connection = mock(Connection.class);
  4. this.dataSource = mock(DataSource.class);
  5. this.preparedStatement = mock(PreparedStatement.class);
  6. this.resultSet = mock(ResultSet.class);
  7. given(this.dataSource.getConnection()).willReturn(this.connection);
  8. given(this.connection.prepareStatement(anyString())).willReturn(this.preparedStatement);
  9. given(preparedStatement.executeQuery()).willReturn(resultSet);
  10. }

代码示例来源:origin: apache/geode

  1. public static void destroyTable(String tableName) throws NamingException, SQLException {
  2. Context ctx = cache.getJNDIContext();
  3. DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource");
  4. Connection conn = ds.getConnection();
  5. // System.out.println (" trying to drop table: " + tableName);
  6. String sql = "drop table " + tableName;
  7. Statement sm = conn.createStatement();
  8. sm.execute(sql);
  9. conn.close();
  10. }

代码示例来源:origin: apache/incubator-gobblin

  1. try (Connection connection = dataSource.getConnection();
  2. PreparedStatement createStatement = connection.prepareStatement(createJobTable)) {
  3. createStatement.executeUpdate();
  4. } catch (SQLException e) {
  5. throw new IOException("Failure creation table " + stateStoreTableName, e);

相关文章