Write a Java program that connects to a PostgreSQL database, retrieves data from a table named "employees", calculates the total salary paid per department, and generates a CSV report with columns for department name and total salary.

To create a Java program that connects to a PostgreSQL database, retrieves data from a table named "employees", calculates the total salary paid per department, and generates a CSV report with columns for department name and total salary, follow these steps:

1. First, make sure you have the PostgreSQL JDBC driver in your project's classpath. You can download it from [here](https://jdbc.postgresql.org/download.html) or add it as a dependency using Maven or Gradle.

2. Create a new Java file called `SalaryReportGenerator.java` and add the following code:

```java
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

public class SalaryReportGenerator {

    public static void main(String[] args) {
        String url = "jdbc:postgresql://localhost:5432/your_database_name";
        String user = "your_username";
        String password = "your_password";

        try (Connection connection = DriverManager.getConnection(url, user, password)) {
            Map<String, Double> departmentSalaries = getDepartmentSalaries(connection);
            generateCSVReport(departmentSalaries);
        } catch (SQLException e) {
            System.err.println("Error connecting to the database: " + e.getMessage());
        }
    }

    private static Map<String, Double> getDepartmentSalaries(Connection connection) throws SQLException {
        String query = "SELECT department, SUM(salary) FROM employees GROUP BY department";
        Map<String, Double> departmentSalaries = new HashMap<>();

        try (Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery(query)) {

            while (resultSet.next()) {
                String department = resultSet.getString(1);
                double totalSalary = resultSet.getDouble(2);
                departmentSalaries.put(department, totalSalary);
            }
        }

        return departmentSalaries;
    }

    private static void generateCSVReport(Map<String, Double> departmentSalaries) {
        String fileName = "department_salaries.csv";

        try (FileWriter fileWriter = new FileWriter(fileName)) {
            fileWriter.write("Department,Total Salary\n");

            for (Map.Entry<String, Double> entry : departmentSalaries.entrySet()) {
                fileWriter.write(entry.getKey() + "," + entry.getValue() + "\n");
            }

            System.out.println("CSV report generated successfully: " + fileName);
        } catch (IOException e) {
            System.err.println("Error generating CSV report: " + e.getMessage());
        }
    }
}
```

3. Replace `your_database_name`, `your_username`, and `your_password` with the appropriate values for your PostgreSQL database.

4. Run the program, and it will generate a CSV report called `department_salaries.csv` in the project's root directory containing the total salary paid per department.

Note that this code assumes you have an "employees" table with columns "department" and "salary". Make sure to adjust the query and column names if needed.