/** Returns a data source for the PostgreSQL database. */ protected DataSource createDataSource() throws SQLException { DataSource candidate = super.createDataSource(); if (candidate instanceof BaseDataSource) { // Any kind of DataSource from the PostgreSQL driver. return candidate; } final PGSimpleDataSource source = new PGSimpleDataSource(); final Properties p = load(); int portNumber; try { portNumber = Integer.parseInt(p.getProperty("portNumber", "5432")); } catch (NumberFormatException exception) { portNumber = 5432; Logging.unexpectedException( "org.geotools.referencing.factory", DataSource.class, "<init>", exception); } source.setPortNumber(portNumber); source.setServerName(p.getProperty("serverName", "localhost")); source.setDatabaseName(p.getProperty("databaseName", "EPSG")); source.setUser(p.getProperty("user", "Geotools")); source.setPassword(p.getProperty("password", "Geotools")); source.setProperty("stringtype", "unspecified"); schema = p.getProperty("schema", null); return source; }
public DataSource getDatabase(String userName, String dbName, Map<String, String> properties) { final PGSimpleDataSource ds = new PGSimpleDataSource(); ds.setServerName("localhost"); ds.setPortNumber(port); ds.setDatabaseName(dbName); ds.setUser(userName); properties.forEach((propertyKey, propertyValue) -> { try { ds.setProperty(propertyKey, propertyValue); } catch (SQLException e) { throw new RuntimeException(e); } }); return ds; }
/** * Create a new Datasource given DBInfo. * More common usage is to call createDatasource(). */ public DataSource createDataSourceFromConnectionInfo(final ConnectionInfo connectionInfo) throws SQLException { final PGSimpleDataSource ds = new PGSimpleDataSource(); ds.setPortNumber(connectionInfo.getPort()); ds.setDatabaseName(connectionInfo.getDbName()); ds.setUser(connectionInfo.getUser()); Set<Entry<String, String>> properties = connectionInfo.getProperties().entrySet(); for (Entry<String, String> property : properties) { ds.setProperty(property.getKey(), property.getValue()); } return ds; }
ds.setApplicationName("Apache SIS test database"); ds.setCurrentSchema(schema); ds.setProperty(PGProperty.LOGGER_LEVEL, "OFF"); // For avoiding warning when no PostgreSQL server is running.