If you're building a web application with Struts and need a reliable relational database, MySQL is a solid and easy-to-manage choice. In this guide, we'll walk through integrating MySQL into your Struts project using Docker for local development and Kubernetes for production. You'll also find best practices for dependency management and clean configuration separation between environments.
1. Add Dependencies in pom.xml
First, make sure your project includes the necessary libraries:
<dependencies>
<!-- Struts core framework -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<!-- MySQL JDBC driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- Kubernetes client (optional for production config) -->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
</dependencies>
This setup gives you Struts for web handling, JDBC support for MySQL, and optional Kubernetes integration for production deployments.
2. Run MySQL with Docker for Local Development
Use Docker Compose to spin up a local MySQL instance quickly:
version: '3'
services:
mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: devdb
MYSQL_USER: devuser
MYSQL_PASSWORD: devpass
ports:
- "3306:3306"
Run it with:
docker-compose up -d
Your database will be available at
localhost:3306
, with database name
devdb
and credentials ready to go.
3. Create Dev Configuration: mysql-dev.properties
Include this in
src/main/resources
:
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/devdb?useSSL=false&serverTimezone=UTC
db.user=devuser
db.password=devpass
db.validationQuery=SELECT 1
Load these values in a Struts helper or DAO:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/mysql-dev.properties"));
Class.forName(props.getProperty("db.driver"));
Connection conn = DriverManager.getConnection(
props.getProperty("db.url"),
props.getProperty("db.user"),
props.getProperty("db.password")
);
// Optional: run validation
conn.createStatement().executeQuery(props.getProperty("db.validationQuery"));
Now you’re connected locally and can test your SQL logic.
4. Prepare Kubernetes Configuration for Production
In production, use Kubernetes ConfigMaps and Secrets to avoid hardcoding credentials:
kubectl create configmap mysql-config \
--from-literal=DB_URL=jdbc:mysql://mysql-service:3306/proddb?useSSL=false&serverTimezone=UTC \
--from-literal=DB_DRIVER=com.mysql.cj.jdbc.Driver \
--from-literal=DB_VALIDATION_QUERY=SELECT 1
kubectl create secret generic mysql-secret \
--from-literal=DB_USER=produser \
--from-literal=DB_PASSWORD=prodpass
5. Create Production Configuration: mysql-prod.properties
Add this file to your production artifacts:
db.driver=${DB_DRIVER}
db.url=${DB_URL}
db.user=${DB_USER}
db.password=${DB_PASSWORD}
db.validationQuery=${DB_VALIDATION_QUERY}
Your Struts code will pull these values from the runtime environment.
6. Kubernetes Deployment Snippet
In your deployment spec, include:
env:
- name: DB_DRIVER
valueFrom:
configMapKeyRef:
name: mysql-config
key: DB_DRIVER
- name: DB_URL
valueFrom:
configMapKeyRef:
name: mysql-config
key: DB_URL
- name: DB_VALIDATION_QUERY
valueFrom:
configMapKeyRef:
name: mysql-config
key: DB_VALIDATION_QUERY
- name: DB_USER
valueFrom:
secretKeyRef:
name: mysql-secret
key: DB_USER
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: DB_PASSWORD
This lets your application securely load production settings at launch.
7. Test Connection in Struts Action
Use this code to verify everything is wired correctly:
public class DbCheckAction 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 MySQL successfully!");
return SUCCESS;
} catch (Exception e) {
addActionError("Connection failed: " + e.getMessage());
return ERROR;
}
}
}
This works with both development and production setups, thanks to config files and environment variables.
Why This Pattern Works
- Fast development cycles: Docker + dev properties = instant database access
- Secure production: Credentials stored in Kubernetes, not in code
- Single codebase: Same logic works in local and cloud environments
- Clear separation: Using distinct
.properties
files avoids confusion
Using Docker for local MySQL, Kubernetes for production secrets/config, and environment-driven configuration in your Struts app gives you a clean, scalable setup. Now you have everything you need to build database-backed Struts applications—from dev environments to production—without fuss.
image quote pre code