-
Notifications
You must be signed in to change notification settings - Fork 59
Assignment: Moon Mission CLI #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,61 +2,206 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.sql.Connection; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.sql.DriverManager; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.sql.PreparedStatement; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.sql.ResultSet; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.sql.SQLException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Arrays; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Scanner; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class Main { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static void main(String[] args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static void main(String[] args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Om vi kör i DevMode: Starta testdatabasen, via Docker/Testcontainers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isDevMode(args)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DevDatabaseInitializer.start(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Startar programlogiken. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new Main().run(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void run() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Resolve DB settings with precedence: System properties -> Environment variables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Hämtar databasinställningar. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String jdbcUrl = resolveConfig("APP_JDBC_URL", "APP_JDBC_URL"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String dbUser = resolveConfig("APP_DB_USER", "APP_DB_USER"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String dbPass = resolveConfig("APP_DB_PASS", "APP_DB_PASS"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Om någon databasinställning skulle saknas, så avbryts programmet. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (jdbcUrl == null || dbUser == null || dbPass == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new IllegalStateException( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Missing DB configuration. Provide APP_JDBC_URL, APP_DB_USER, APP_DB_PASS " + | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "as system properties (-Dkey=value) or environment variables."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Missing DB configuration. Provide APP_JDBC_URL, APP_DB_USER, APP_DB_PASS as system properties (-Dkey=value) or environment variables." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Skapar anslutning till databasen. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try (Connection connection = DriverManager.getConnection(jdbcUrl, dbUser, dbPass)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // För att läsa input av användaren i terminalen, så används Scanner. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Scanner scanner = new Scanner(System.in); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Loggar in med användarnamn och lösenord. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.print("Username: "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String username = scanner.nextLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.print("Password: "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String password = scanner.nextLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // En SQL-fråga för att kontrollera användarnamn och lösenord. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String loginSql = "SELECT * FROM account WHERE name = ? AND password = ?"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try (PreparedStatement stmt = connection.prepareStatement(loginSql)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stmt.setString(1, username); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stmt.setString(2, password); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try (ResultSet rs = stmt.executeQuery()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Om ingen rad hittas, så misslyckas inloggningen. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!rs.next()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Invalid username or password"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; // Avslutar run() om inloggningen misslyckas. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+47
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Check for password hashing implementation
echo "Checking if init.sql contains hashed passwords..."
if [ -f "src/main/resources/init.sql" ]; then
grep -i "password" src/main/resources/init.sql | head -20
fi
echo ""
echo "Searching for password hashing libraries or utilities in the codebase..."
rg -n "hash|bcrypt|pbkdf2|scrypt|argon" --type java -g '!**/test/**'Repository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom Length of output: 487 🏁 Script executed: #!/bin/bash
# Find the createAccount method
echo "Searching for createAccount method..."
rg -n "createAccount|CREATE.*ACCOUNT" --type java -A 15
echo ""
echo "Checking for any password manipulation in Java code..."
rg -n "password" --type java -B 2 -A 2 | head -60Repository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom Length of output: 9140 Passwords must be hashed using a secure algorithm. Passwords are currently stored and validated as plain text in the database. Lines 47–58 compare the input password directly against the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Fortsätter programmet efter lyckad inloggning. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Login successful!"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
36
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resource leak: Scanner is never closed. The Consider restructuring to ensure proper resource management: try (Connection connection = DriverManager.getConnection(jdbcUrl, dbUser, dbPass)) {
- // För att läsa input av användaren i terminalen, så används Scanner.
- Scanner scanner = new Scanner(System.in);
+ try (Scanner scanner = new Scanner(System.in)) {
+ // För att läsa input av användaren i terminalen, så används Scanner.
- } catch (SQLException e) {
+ }
+ } catch (SQLException e) {Note: Be aware that closing a Scanner that wraps
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // En meny-loop. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| boolean running = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (running) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("\nMenu:"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("1) List moon missions"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("2) Get moon mission by ID"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("3) Count missions for a year"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("4) Create account"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("5) Update account password"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("6) Delete account"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("0) Exit"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.print("Choose an option: "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Läser in användarens menyval. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| switch (scanner.nextLine()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case "1" -> listMissions(connection); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case "2" -> getMissionById(connection, scanner); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case "3" -> countMissionsByYear(connection, scanner); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case "4" -> createAccount(connection, scanner); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case "5" -> updatePassword(connection, scanner); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case "6" -> deleteAccount(connection, scanner); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case "0" -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| running = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Exiting program..."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default -> System.out.println("Invalid option."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (SQLException e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Om något skulle gå fel med databasen. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new RuntimeException(e); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //Todo: Starting point for your code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Determines if the application is running in development mode based on system properties, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * environment variables, or command-line arguments. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param args an array of command-line arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return {@code true} if the application is in development mode; {@code false} otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Listar alla rymduppdrag. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void listMissions(Connection conn) throws SQLException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try (PreparedStatement stmt = conn.prepareStatement("SELECT spacecraft FROM moon_mission"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ResultSet rs = stmt.executeQuery()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Moon missions:"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (rs.next()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println(rs.getString("spacecraft")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Hämtar ett specifikt uppdrag baserat på mission_id. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void getMissionById(Connection conn, Scanner scanner) throws SQLException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.print("Enter mission_id: "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String id = scanner.nextLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try (PreparedStatement stmt = conn.prepareStatement( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "SELECT spacecraft, launch_date FROM moon_mission WHERE mission_id = ?")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stmt.setString(1, id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try (ResultSet rs = stmt.executeQuery()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (rs.next()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Hämtar årtal från launch_date. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int year = rs.getDate("launch_date").toLocalDate().getYear(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Mission: " + rs.getString("spacecraft") + ", Year: " + year); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Mission not found."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Räknar hur många uppdrag som skedde ett specifikt år. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void countMissionsByYear(Connection conn, Scanner scanner) throws SQLException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.print("Enter year: "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String year = scanner.nextLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try (PreparedStatement stmt = conn.prepareStatement( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "SELECT COUNT(*) AS count FROM moon_mission WHERE YEAR(launch_date) = ?")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stmt.setString(1, year); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try (ResultSet rs = stmt.executeQuery()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (rs.next()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Year: " + year + ", Number of missions: " + rs.getInt("count")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Skapar ett nytt konto. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void createAccount(Connection conn, Scanner scanner) throws SQLException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.print("First name: "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String firstName = scanner.nextLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.print("Last name: "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String lastName = scanner.nextLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.print("SSN: "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String ssn = scanner.nextLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.print("Password: "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String password = scanner.nextLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try (PreparedStatement stmt = conn.prepareStatement( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "INSERT INTO account (first_name, last_name, ssn, password) VALUES (?, ?, ?, ?)")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stmt.setString(1, firstName); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stmt.setString(2, lastName); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stmt.setString(3, ssn); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stmt.setString(4, password); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stmt.executeUpdate(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Account created!"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+144
to
+163
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add input validation and improve error handling. The method has several concerns:
Consider adding validation: private void createAccount(Connection conn, Scanner scanner) throws SQLException {
System.out.print("First name: ");
String firstName = scanner.nextLine();
+ if (firstName == null || firstName.trim().isEmpty()) {
+ System.out.println("First name cannot be empty.");
+ return;
+ }
+
System.out.print("Last name: ");
String lastName = scanner.nextLine();
+ if (lastName == null || lastName.trim().isEmpty()) {
+ System.out.println("Last name cannot be empty.");
+ return;
+ }
+
System.out.print("SSN: ");
String ssn = scanner.nextLine();
+ if (ssn == null || ssn.trim().isEmpty()) {
+ System.out.println("SSN cannot be empty.");
+ return;
+ }
+
System.out.print("Password: ");
String password = scanner.nextLine();
+ if (password == null || password.trim().isEmpty()) {
+ System.out.println("Password cannot be empty.");
+ return;
+ }
+ // TODO: Hash password before storage
try (PreparedStatement stmt = conn.prepareStatement(
"INSERT INTO account (first_name, last_name, ssn, password) VALUES (?, ?, ?, ?)")) {
stmt.setString(1, firstName);
stmt.setString(2, lastName);
stmt.setString(3, ssn);
stmt.setString(4, password);
stmt.executeUpdate();
System.out.println("Account created!");
+ } catch (SQLException e) {
+ if (e.getSQLState() != null && e.getSQLState().startsWith("23")) {
+ System.out.println("Failed to create account: Duplicate SSN or constraint violation.");
+ } else {
+ throw e;
+ }
}
} |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Uppdaterar ett lösenord för ett konto. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void updatePassword(Connection conn, Scanner scanner) throws SQLException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.print("User ID: "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String userId = scanner.nextLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.print("New password: "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String newPassword = scanner.nextLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try (PreparedStatement stmt = conn.prepareStatement("UPDATE account SET password = ? WHERE user_id = ?")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stmt.setString(1, newPassword); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stmt.setString(2, userId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int rows = stmt.executeUpdate(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println(rows > 0 ? "Password updated!" : "User not found."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+166
to
+178
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add validation for empty password. The method doesn't validate that the new password is non-empty before updating the database. Apply this diff to add validation: private void updatePassword(Connection conn, Scanner scanner) throws SQLException {
System.out.print("User ID: ");
String userId = scanner.nextLine();
System.out.print("New password: ");
String newPassword = scanner.nextLine();
+
+ if (newPassword == null || newPassword.trim().isEmpty()) {
+ System.out.println("Password cannot be empty.");
+ return;
+ }
+ // TODO: Hash password before storage
try (PreparedStatement stmt = conn.prepareStatement("UPDATE account SET password = ? WHERE user_id = ?")) {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Tar bort ett konto. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void deleteAccount(Connection conn, Scanner scanner) throws SQLException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.print("User ID: "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String userId = scanner.nextLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try (PreparedStatement stmt = conn.prepareStatement("DELETE FROM account WHERE user_id = ?")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stmt.setString(1, userId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int rows = stmt.executeUpdate(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println(rows > 0 ? "Account deleted!" : "User not found."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Avgör om programmet körs i DevMode. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static boolean isDevMode(String[] args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Boolean.getBoolean("devMode")) //Add VM option -DdevMode=true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ("true".equalsIgnoreCase(System.getenv("DEV_MODE"))) //Environment variable DEV_MODE=true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Arrays.asList(args).contains("--dev"); //Argument --dev | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Boolean.getBoolean("devMode")) return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ("true".equalsIgnoreCase(System.getenv("DEV_MODE"))) return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Arrays.asList(args).contains("--dev"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Reads configuration with precedence: Java system property first, then environment variable. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Returns trimmed value or null if neither source provides a non-empty value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Läser värden från system properties eller environment variabler. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static String resolveConfig(String propertyKey, String envKey) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String v = System.getProperty(propertyKey); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (v == null || v.trim().isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| v = System.getenv(envKey); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (v == null || v.trim().isEmpty()) ? null : v.trim(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom
Length of output: 2220
🏁 Script executed:
Repository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom
Length of output: 154
🏁 Script executed:
Repository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom
Length of output: 371
🏁 Script executed:
# Examine DevDatabaseInitializer.java to understand the usage context cat ./src/main/java/com/example/DevDatabaseInitializer.javaRepository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom
Length of output: 870
🏁 Script executed:
Repository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom
Length of output: 270
🏁 Script executed:
Repository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom
Length of output: 178
🏁 Script executed:
Repository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom
Length of output: 136
🏁 Script executed:
# Examine Main.java to see the context of DevDatabaseInitializer.start() cat ./src/main/java/com/example/Main.javaRepository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom
Length of output: 8825
Move
DevDatabaseInitializerto test source or use a Maven profile for dev-mode dependencies.The
testcontainers-mysqldependency correctly remains incompilescope sinceDevDatabaseInitializeris insrc/main/javaand must referenceMySQLContainerat compile time. However, since dev mode initialization is optional (controlled viaisDevMode()check), the dependency is unnecessarily bundled in production artifacts.Current state: Dev mode is already conditional and requires explicit activation (system property
devMode, environment variableDEV_MODE, or--devargument), so the class is never instantiated in standard production runs.Recommended solution: Move
src/main/java/com/example/DevDatabaseInitializer.javatosrc/test/javaand add<scope>test</scope>to thetestcontainers-mysqldependency. This aligns the code organization with its actual purpose as a development/testing utility and prevents the dependency from being packaged in production artifacts.Alternatively, create a separate Maven profile for dev mode that includes the Testcontainers dependency only when explicitly activated.
🤖 Prompt for AI Agents