AbstractDriverBasedDataSource.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package org.es.jdbc.api;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import java.util.Properties;
  5. public abstract class AbstractDriverBasedDataSource extends AbstractDataSource {
  6. private String url;
  7. private Properties connectionProperties;
  8. public String getUrl() {
  9. return this.url;
  10. }
  11. public void setUrl(String url) {
  12. this.url = url.trim();
  13. }
  14. public Properties getConnectionProperties() {
  15. return this.connectionProperties;
  16. }
  17. public void setConnectionProperties(Properties connectionProperties) {
  18. this.connectionProperties = connectionProperties;
  19. }
  20. public Connection getConnection() throws SQLException {
  21. return getConnectionFromDriver();
  22. }
  23. @Override
  24. public Connection getConnection(String username, String password) throws SQLException {
  25. return getConnection();
  26. }
  27. protected Connection getConnectionFromDriver() throws SQLException {
  28. Properties mergedProps = new Properties();
  29. Properties connProps = getConnectionProperties();
  30. if (connProps != null) {
  31. mergedProps.putAll(connProps);
  32. }
  33. return getConnectionFromDriver(mergedProps);
  34. }
  35. protected abstract Connection getConnectionFromDriver(Properties props) throws SQLException;
  36. }