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
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<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.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Docker Documentation — content/guides/testcontainers-java-getting-started.md :: Implement the business logic ↗Revision 3a9d778562f3 · Apache-2.0 and attribution