#1
Struts is a reliable Java web framework, and when you need a database backend, PostgreSQL is a top-tier choice. Whether you're running things locally or deploying to the cloud, this guide will walk you through setting up PostgreSQL in Struts—using Docker in development and Kubernetes in production—with clean configurations and clear environment separation.

1. Add Dependencies to pom.xml

In your pom.xml, include the following dependencies:
<dependencies>
  <!-- Struts core framework -->
  <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>${struts.version}</version>
  </dependency>

  <!-- PostgreSQL JDBC driver -->
  <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>${postgresql.version}</version>
  </dependency>

  <!-- Kubernetes client (optional for deployments) -->
  <dependency>
    <groupId>io.fabric8</groupId>
    <artifactId>kubernetes-client</artifactId>
    <version>${fabric8.version}</version>
  </dependency>
</dependencies>
This equips your project with Struts for web logic, PostgreSQL connectivity via JDBC, and optional Kubernetes support for production configuration management.

2. Run PostgreSQL with Docker for Local Development

To use PostgreSQL locally, create this docker-compose.yml:
version: '3'
services:
  postgres:
    image: postgres:14-alpine
    environment:
      POSTGRES_USER: devuser
      POSTGRES_PASSWORD: devpass
      POSTGRES_DB: devdb
    ports:
      - "5432:5432"
Run the service:
docker-compose up -d
Now you have a PostgreSQL instance at localhost:5432, ready for development.

3. Create Local Config: postgres-dev.properties

Add this file under src/main/resources:
db.driver=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5432/devdb
db.user=devuser
db.password=devpass
db.validationQuery=SELECT 1
Load these settings in a Struts utility or DAO helper:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/postgres-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 verify with validationQuery
This allows you to inject connections cleanly into your DAO or action classes.

4. Prepare Kubernetes Config for Production

In production, avoid hardcoding credentials. Use ConfigMaps and Secrets:
kubectl create configmap postgres-config \
  --from-literal=DB_URL=jdbc:postgresql://postgres-service:5432/proddb \
  --from-literal=DB_DRIVER=org.postgresql.Driver \
  --from-literal=DB_VALIDATION_QUERY=SELECT 1

kubectl create secret generic postgres-secret \
  --from-literal=DB_USER=produser \
  --from-literal=DB_PASSWORD=prodpass

5. Create Production Config: postgres-prod.properties

Place this alongside other resources:
db.driver=${DB_DRIVER}
db.url=${DB_URL}
db.user=${DB_USER}
db.password=${DB_PASSWORD}
db.validationQuery=${DB_VALIDATION_QUERY}
At runtime, environment variables supplied via Kubernetes are substituted in.

6. Helm Deployment Snippet

Make sure the Pod spec includes:
env:
  - name: DB_DRIVER
    valueFrom:
      configMapKeyRef:
        name: postgres-config
        key: DB_DRIVER
  - name: DB_URL
    valueFrom:
      configMapKeyRef:
        name: postgres-config
        key: DB_URL
  - name: DB_VALIDATION_QUERY
    valueFrom:
      configMapKeyRef:
        name: postgres-config
        key: DB_VALIDATION_QUERY
  - name: DB_USER
    valueFrom:
      secretKeyRef:
        name: postgres-secret
        key: DB_USER
  - name: DB_PASSWORD
    valueFrom:
      secretKeyRef:
        name: postgres-secret
        key: DB_PASSWORD
This ensures that your application picks up production settings automatically.

7. Example Code in Struts Action

Integrate with JDBC in your 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("DB connected successfully!");
      return SUCCESS;
    } catch (SQLException e) {
      addActionError("DB connection failed: " + e.getMessage());
      return ERROR;
    }
  }
}
This unified code works in both dev and production environments, with only the config properties file changing.

Why this Approach Makes Sense

  • Fast local development: Docker + dev properties let you iterate quickly.
  • Secure production: Kubernetes holds sensitive details in Secrets and ConfigMaps.
  • Consistent codebase: Same JDBC logic works in both environments.
  • Configuration separation: Distinct dev and prod config files minimize confusion.
By combining Docker, Kubernetes, and property-driven configuration in Struts, you build a solid foundations for working with PostgreSQL. You get speed and simplicity in development, and manageability and security in production. Your Struts web app is now ready for robust, real-world database interactions—no matter where it runs.

image quote pre code