Struts remains a powerful Java web framework, and pairing it with Microsoft SQL Server (MsSQL) offers strong advantages for data-driven applications. In this guide, we’ll explore how to integrate MsSQL into a Struts project—covering Docker-based local development, Kubernetes-based production, and dependency setup—all while keeping your configuration clean and environment-specific.
1. Add Dependencies to pom.xml
Include these dependencies so your app can use Struts, MsSQL, and optionally Kubernetes in production:
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>${mssql.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
</dependencies>
This setup ensures you have web logic through Struts, database connectivity via JDBC, and optional Kubernetes support for production.
2. Run MsSQL with Docker for Local Development
Use Docker to set up the local SQL Server:
docker run -e "ACCEPT_EULA=Y" \
-e "SA_PASSWORD=YourStrong!Passw0rd" \
-p 1433:1433 \
--name mssql-dev \
-d mcr.microsoft.com/mssql/server:2022-latest
You’ll now have a SQL Server instance running on
localhost:1433
, with SA credentials set to
YourStrong!Passw0rd
.
3. Create Dev Config: mssql-dev.properties
Add this file to
src/main/resources
:
db.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
db.url=jdbc:sqlserver://localhost:1433;databaseName=master
db.user=sa
db.password=YourStrong!Passw0rd
db.validationQuery=SELECT 1
You can load this within a Struts helper or DAO class:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/mssql-dev.properties"));
Class.forName(props.getProperty("db.driver"));
Connection conn = DriverManager.getConnection(
props.getProperty("db.url"),
props.getProperty("db.user"),
props.getProperty("db.password")
);
// Optionally run props.getProperty("db.validationQuery") for testing
This gives you a working local environment to test your JDBC connections.
4. Set Up Kubernetes Config for Production
Use ConfigMap and Secret to manage prod configs securely:
kubectl create configmap mssql-config \
--from-literal=DB_URL=jdbc:sqlserver://mssql-svc:1433;databaseName=prodDb \
--from-literal=DB_DRIVER=com.microsoft.sqlserver.jdbc.SQLServerDriver \
--from-literal=DB_VALIDATION_QUERY=SELECT+1
kubectl create secret generic mssql-secret \
--from-literal=DB_USER=produser \
--from-literal=DB_PASSWORD=prodPass123
5. Add Production Config: mssql-prod.properties
Include this in your app’s resources:
db.driver=${DB_DRIVER}
db.url=${DB_URL}
db.user=${DB_USER}
db.password=${DB_PASSWORD}
db.validationQuery=${DB_VALIDATION_QUERY}
Struts loader or your utility class can pick up runtime environment variables using standard Java system properties or
System.getenv()
.
6. Kubernetes Deployment Snippet
Ensure your deployment references the config and secret:
env:
- name: DB_DRIVER
valueFrom:
configMapKeyRef:
name: mssql-config
key: DB_DRIVER
- name: DB_URL
valueFrom:
configMapKeyRef:
name: mssql-config
key: DB_URL
- name: DB_VALIDATION_QUERY
valueFrom:
configMapKeyRef:
name: mssql-config
key: DB_VALIDATION_QUERY
- name: DB_USER
valueFrom:
secretKeyRef:
name: mssql-secret
key: DB_USER
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: mssql-secret
key: DB_PASSWORD
Your Struts application will read these values at boot time, establishing a secure database connection in production.
7. Example Usage in a Struts Action
Here’s how you can verify connectivity in a Struts action:
public class DbAction extends ActionSupport {
public String execute() {
try (Connection conn = DriverManager.getConnection(
System.getenv("DB_URL"),
System.getenv("DB_USER"),
System.getenv("DB_PASSWORD"));
Statement stmt = conn.createStatement()) {
stmt.executeQuery(System.getenv("DB_VALIDATION_QUERY"));
addActionMessage("Connected to MsSQL successfully!");
return SUCCESS;
} catch (SQLException e) {
addActionError("Connection failed: " + e.getMessage());
return ERROR;
}
}
}
This approach keeps code consistent between development and production—only config files change.
Why This Setup Works
- Quick local setup: Docker and
.properties
let you develop fast.
- Secure production: Credentials never go into code—Kubernetes Secrets handle them.
- Reusable code: Same JDBC logic works across environments.
- Clear environment separation: Distinct
.properties
files prevent accidental misuse.
With Docker, Kubernetes, and smart configuration management, your Struts app can reliably work with Microsoft SQL Server in all environments. You get fast dev cycles, secure deployment, and maintainable code—all without juggling complex setups. Now you’re ready to build robust, data-driven Struts applications!
image quote pre code