Struts is a classic Java web framework, and pairing it with IBM Db2 gives you enterprise-grade relational database capabilities. Whether you're testing features locally or deploying at scale, this guide walks you through connecting a Struts application to Db2—using Docker in development and Kubernetes for production—while keeping your configurations clean and environment-specific.
1. Add Dependencies in pom.xml
Start by adding these essential dependencies:
<dependencies>
<!-- Struts framework -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<!-- IBM Db2 JDBC driver -->
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>jcc</artifactId>
<version>${db2.jdbc.version}</version>
</dependency>
<!-- Kubernetes client (optional, for production) -->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
</dependencies>
This gives your app access to Struts, Db2 connectivity, and Kubernetes client support for production deployments.
2. Run Db2 Locally with Docker
Use Docker to quickly spin up a Db2 instance on your machine:
docker run -d --name db2-dev \
-e LICENSE=accept \
-e DB2INST1_PASSWORD=devpass \
-p 50000:50000 \
ibmcom/db2
After it’s running, Db2 listens on
localhost:50000
, with default database
BLUDB
and user
db2inst1
using the password
devpass
.
3. Create Development Configuration: db2-dev.properties
Add this file under
src/main/resources
:
db.driver=com.ibm.db2.jcc.DB2Driver
db.url=jdbc:db2://localhost:50000/BLUDB
db.user=db2inst1
db.password=devpass
db.validationQuery=SELECT 1 FROM SYSIBM.SYSDUMMY1
Load these settings in your Struts helper or DAO:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/db2-dev.properties"));
Class.forName(props.getProperty("db.driver"));
try (Connection conn = DriverManager.getConnection(
props.getProperty("db.url"),
props.getProperty("db.user"),
props.getProperty("db.password"))) {
conn.createStatement().executeQuery(props.getProperty("db.validationQuery"));
}
You're now connected to Db2 for local testing.
4. Prepare Kubernetes Config for Production
To manage production credentials securely:
kubectl create configmap db2-config \
--from-literal=DB_DRIVER=com.ibm.db2.jcc.DB2Driver \
--from-literal=DB_URL=jdbc:db2://db2-service:50000/PRODDB \
--from-literal=DB_VALIDATION_QUERY=SELECT 1 FROM SYSIBM.SYSDUMMY1
kubectl create secret generic db2-secret \
--from-literal=DB_USER=produser \
--from-literal=DB_PASSWORD=prodPass123
5. Add Production Configuration: db2-prod.properties
Include this file in your application resources:
db.driver=${DB_DRIVER}
db.url=${DB_URL}
db.user=${DB_USER}
db.password=${DB_PASSWORD}
db.validationQuery=${DB_VALIDATION_QUERY}
At runtime, your Struts-based application loads these from environment variables.
6. Kubernetes Deployment Example
Include these environment variables in your Pod spec:
env:
- name: DB_DRIVER
valueFrom:
configMapKeyRef:
name: db2-config
key: DB_DRIVER
- name: DB_URL
valueFrom:
configMapKeyRef:
name: db2-config
key: DB_URL
- name: DB_VALIDATION_QUERY
valueFrom:
configMapKeyRef:
name: db2-config
key: DB_VALIDATION_QUERY
- name: DB_USER
valueFrom:
secretKeyRef:
name: db2-secret
key: DB_USER
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db2-secret
key: DB_PASSWORD
Once deployed, your app automatically picks up all production credentials and settings.
7. Health-Check Action in Struts
Here's a simple Struts action that verifies connectivity:
public class DbHealthAction extends ActionSupport {
public String execute() {
String url = System.getenv("DB_URL");
String user = System.getenv("DB_USER");
String pass = System.getenv("DB_PASSWORD");
String validation = System.getenv("DB_VALIDATION_QUERY");
try (Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement()) {
stmt.executeQuery(validation);
addActionMessage("Connected to Db2 successfully!");
return SUCCESS;
} catch (SQLException e) {
addActionError("Connection failed: " + e.getMessage());
return ERROR;
}
}
}
This works unaltered in both development and production environments—only the properties file changes.
Why This Setup Works
- Rapid local dev with Docker and simple properties file
- Secure production via Kubernetes-managed ConfigMaps and Secrets
- Single codebase with consistent connection logic across environments
- Clear separation of environments through dedicated
.properties
files
With Docker for local testing and Kubernetes for production, your Struts application can confidently connect to IBM Db2. This setup keeps your configuration clean, credentials safe, and code reusable across environments. Happy coding!
image quote pre code