Skip to content
This repository was archived by the owner on Jun 18, 2026. It is now read-only.

Commit e271463

Browse files
refactor: extract shared getConnection() in Util.java to eliminate duplication
The getAppConnection() and getAzialaConnection() methods contained identical logic for reading env vars, validating the host, loading the driver, and opening a connection — differing only in the database name. Extracted a private getConnection(String database) method that: - Centralizes credential lookup and host validation - Validates the database name against injection (alphanumeric + underscore only) - Reduces duplication from ~20 lines to 2 one-liner delegations This makes adding new database connections a single-line call and eliminates the risk of the two methods drifting out of sync.
1 parent 1828e5a commit e271463

1 file changed

Lines changed: 28 additions & 12 deletions

File tree

Gvisual/src/app/Util.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,43 @@ private static String validateHost(String host) {
5959
return host;
6060
}
6161

62-
public static Connection getAppConnection() throws Exception {
62+
/**
63+
* Opens a JDBC connection to the specified PostgreSQL database.
64+
*
65+
* <p>Credentials and host are read from environment variables
66+
* ({@code DB_HOST}, {@code DB_USER}, {@code DB_PASS}).
67+
*
68+
* @param database the database name to connect to
69+
* @return an open {@link Connection}
70+
* @throws Exception if the driver cannot be loaded or the connection fails
71+
*/
72+
private static Connection getConnection(String database) throws Exception {
73+
if (database == null || database.isEmpty()) {
74+
throw new IllegalArgumentException("database name must not be null or empty");
75+
}
76+
// Reject database names that could inject JDBC parameters
77+
if (!database.matches("[a-zA-Z0-9_]+")) {
78+
throw new IllegalArgumentException(
79+
"database name contains invalid characters: " + database
80+
+ ". Only alphanumerics and underscores are allowed.");
81+
}
82+
6383
String host = validateHost(envOrDefault("DB_HOST", DEFAULT_HOST));
6484
String user = requireEnv("DB_USER");
6585
String pass = requireEnv("DB_PASS");
6686

6787
Class.forName("org.postgresql.Driver");
6888
Connection conn = DriverManager.getConnection(
69-
"jdbc:postgresql://" + host + "/nic_apps", user, pass);
70-
System.out.println("Successfully connected to database \"nic_apps\"");
89+
"jdbc:postgresql://" + host + "/" + database, user, pass);
90+
System.out.println("Successfully connected to database \"" + database + "\"");
7191
return conn;
7292
}
7393

74-
public static Connection getAzialaConnection() throws Exception {
75-
String host = validateHost(envOrDefault("DB_HOST", DEFAULT_HOST));
76-
String user = requireEnv("DB_USER");
77-
String pass = requireEnv("DB_PASS");
94+
public static Connection getAppConnection() throws Exception {
95+
return getConnection("nic_apps");
96+
}
7897

79-
Class.forName("org.postgresql.Driver");
80-
Connection conn = DriverManager.getConnection(
81-
"jdbc:postgresql://" + host + "/nic_aziala", user, pass);
82-
System.out.println("Successfully connected to database \"nic_aziala\"");
83-
return conn;
98+
public static Connection getAzialaConnection() throws Exception {
99+
return getConnection("nic_aziala");
84100
}
85101
}

0 commit comments

Comments
 (0)