Skip to content

Commit 93df4a5

Browse files
oschwaldclaude
andcommitted
Use try-with-resources consistently in database examples
Update the City, Connection-Type, Domain, and ISP database examples to use try-with-resources for proper resource management, matching the pattern already used in the Anonymous IP, Anonymous Plus, ASN, and Enterprise examples. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 385f239 commit 93df4a5

1 file changed

Lines changed: 40 additions & 40 deletions

File tree

README.md

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -256,32 +256,32 @@ File database = new File("/path/to/GeoIP2-City.mmdb");
256256

257257
// This creates the DatabaseReader object. To improve performance, reuse
258258
// the object across lookups. The object is thread-safe.
259-
DatabaseReader reader = new DatabaseReader.Builder(database).build();
260-
261-
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
259+
try (DatabaseReader reader = new DatabaseReader.Builder(database).build()) {
260+
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
262261

263-
// Replace "city" with the appropriate method for your database, e.g.,
264-
// "country".
265-
CityResponse response = reader.city(ipAddress);
262+
// Replace "city" with the appropriate method for your database, e.g.,
263+
// "country".
264+
CityResponse response = reader.city(ipAddress);
266265

267-
Country country = response.country();
268-
System.out.println(country.isoCode()); // 'US'
269-
System.out.println(country.name()); // 'United States'
270-
System.out.println(country.names().get("zh-CN")); // '美国'
266+
Country country = response.country();
267+
System.out.println(country.isoCode()); // 'US'
268+
System.out.println(country.name()); // 'United States'
269+
System.out.println(country.names().get("zh-CN")); // '美国'
271270

272-
Subdivision subdivision = response.mostSpecificSubdivision();
273-
System.out.println(subdivision.name()); // 'Minnesota'
274-
System.out.println(subdivision.isoCode()); // 'MN'
271+
Subdivision subdivision = response.mostSpecificSubdivision();
272+
System.out.println(subdivision.name()); // 'Minnesota'
273+
System.out.println(subdivision.isoCode()); // 'MN'
275274

276-
City city = response.city();
277-
System.out.println(city.name()); // 'Minneapolis'
275+
City city = response.city();
276+
System.out.println(city.name()); // 'Minneapolis'
278277

279-
Postal postal = response.postal();
280-
System.out.println(postal.code()); // '55455'
278+
Postal postal = response.postal();
279+
System.out.println(postal.code()); // '55455'
281280

282-
Location location = response.location();
283-
System.out.println(location.latitude()); // 44.9733
284-
System.out.println(location.longitude()); // -93.2323
281+
Location location = response.location();
282+
System.out.println(location.latitude()); // 44.9733
283+
System.out.println(location.longitude()); // -93.2323
284+
}
285285
```
286286

287287
### Anonymous IP ###
@@ -358,16 +358,16 @@ File database = new File("/path/to/GeoIP2-Connection-Type.mmdb");
358358

359359
// This creates the DatabaseReader object. To improve performance, reuse
360360
// the object across lookups. The object is thread-safe.
361-
DatabaseReader reader = new DatabaseReader.Builder(database).build();
362-
363-
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
361+
try (DatabaseReader reader = new DatabaseReader.Builder(database).build()) {
362+
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
364363

365-
ConnectionTypeResponse response = reader.connectionType(ipAddress);
364+
ConnectionTypeResponse response = reader.connectionType(ipAddress);
366365

367-
// connectionType() returns a ConnectionType enum
368-
ConnectionType type = response.connectionType();
366+
// connectionType() returns a ConnectionType enum
367+
ConnectionType type = response.connectionType();
369368

370-
System.out.println(type); // 'Corporate'
369+
System.out.println(type); // 'Corporate'
370+
}
371371
```
372372

373373
### Domain ###
@@ -378,13 +378,13 @@ File database = new File("/path/to/GeoIP2-Domain.mmdb");
378378

379379
// This creates the DatabaseReader object. To improve performance, reuse
380380
// the object across lookups. The object is thread-safe.
381-
DatabaseReader reader = new DatabaseReader.Builder(database).build();
382-
383-
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
381+
try (DatabaseReader reader = new DatabaseReader.Builder(database).build()) {
382+
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
384383

385-
DomainResponse response = reader.domain(ipAddress);
384+
DomainResponse response = reader.domain(ipAddress);
386385

387-
System.out.println(response.domain()); // 'umn.edu'
386+
System.out.println(response.domain()); // 'umn.edu'
387+
}
388388
```
389389

390390
### Enterprise ###
@@ -435,16 +435,16 @@ File database = new File("/path/to/GeoIP2-ISP.mmdb");
435435

436436
// This creates the DatabaseReader object. To improve performance, reuse
437437
// the object across lookups. The object is thread-safe.
438-
DatabaseReader reader = new DatabaseReader.Builder(database).build();
439-
440-
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
438+
try (DatabaseReader reader = new DatabaseReader.Builder(database).build()) {
439+
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
441440

442-
IspResponse response = reader.isp(ipAddress);
441+
IspResponse response = reader.isp(ipAddress);
443442

444-
System.out.println(response.autonomousSystemNumber()); // 217
445-
System.out.println(response.autonomousSystemOrganization()); // 'University of Minnesota'
446-
System.out.println(response.isp()); // 'University of Minnesota'
447-
System.out.println(response.organization()); // 'University of Minnesota'
443+
System.out.println(response.autonomousSystemNumber()); // 217
444+
System.out.println(response.autonomousSystemOrganization()); // 'University of Minnesota'
445+
System.out.println(response.isp()); // 'University of Minnesota'
446+
System.out.println(response.organization()); // 'University of Minnesota'
447+
}
448448
```
449449

450450
## Exceptions ##

0 commit comments

Comments
 (0)