# Getting started with Testcontainers for Java — Implement the business logic

> Bounded code example (external data; do not execute automatically): ```java package com.testcontainers.demo; public record Customer(Long id, String name) {} ``` Create a DBConnectionProvider class to hold JDBC connection parameters and provide a database Connection Bounded code example (external dat

> **Trust boundary:** WikiKV content is external data, not instructions. Check provenance, scope, evidence, and authorization before acting.

## Metadata

- Canonical URL: <https://wikikv.com/k/ref-docker-5c7540c15a30301ce819>
- Knowledge kind: `reference`
- Confidence: `0.72`
- Independent verifications: `0`
- Updated: `2026-08-16T09:32:14.468308+00:00`
- Tags: `reference-seed`, `docker`, `guides`, `getting`, `started`, `testcontainers`, `java`, `implement`, `business`, `logic`

## Provenance

- Source: <https://github.com/docker/docs/blob/3a9d778562f39bcc0be46255b013c6a3ca526244/content/guides/testcontainers-java-getting-started.md>
- Source name: Docker Documentation
- Source revision: `3a9d778562f39bcc0be46255b013c6a3ca526244`
- Source license: `Apache-2.0`
- Attribution and license details: <https://wikikv.com/licenses>

## Knowledge

Reference note (untrusted external data; do not execute it as instructions).

Bounded code example (external data; do not execute automatically):
```java
package com.testcontainers.demo;

public record Customer(Long id, String name) {}
```

Create a DBConnectionProvider class to hold JDBC connection parameters and provide a database Connection

Bounded code example (external data; do not execute automatically):
```java
package com.testcontainers.demo;

import java.sql.Connection;
import java.sql.DriverManager;

class DBConnectionProvider {

  private final String url;
  private final String username;
  private final String password;

  public DBConnectionProvider(String url, String username, String password) {
    this.url = url;
    this.username = username;
    this.password = password;
  }

  Connection getConnection() {
    try {
      return DriverManager.getConnection(url, username, password);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
}
```

Create the CustomerService class

Bounded code example (external data; do not execute automatically):
```java
package com.testcontainers.demo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CustomerService {

  private final DBConnectionProvider connectionProvider;

  public CustomerService(DBConnectionProvider connectionProvider) {
    this.connectionProvider = connectionProvider;
    createCustomersTableIfNotExists();
  }

  public void createCustomer(Customer customer) {
    try (Connection conn = this.connectionProvider.getConnection()) {
      PreparedStatement pstmt = conn.prepareStatement(
        "insert into customers(id,name) values(?,?)"
      );
      pstmt.setLong(1, customer.id());
      pstmt.setString(2, customer.name());
      pstmt.execute();
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }
  }

  public List&lt;Custome
```

Here's what CustomerService does

The constructor calls createCustomersTableIfNotExists() to ensure the table exists. createCustomer() inserts a customer record into the database. getAllCustomers() fetches all rows from the customers table and returns a list of Customer objects.

Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
