-
Notifications
You must be signed in to change notification settings - Fork 59
Database lab 3 #8
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
24da335
35c0aa1
6282ae2
9e8cb48
d041fad
19c5e23
5d38357
114c9cf
5631195
3f7f144
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 |
|---|---|---|
| @@ -1,20 +1,24 @@ | ||
| package com.example; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.SQLException; | ||
| import java.sql.*; | ||
| import java.util.Arrays; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public class | ||
| Main { | ||
|
|
||
| static void main(String[] args) { | ||
| private Connection connection; // Koppla upp mot databas | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| if (isDevMode(args)) { | ||
| DevDatabaseInitializer.start(); | ||
| } | ||
| new Main().run(); | ||
| } | ||
|
|
||
| public void run() { | ||
|
|
||
| // Resolve DB settings with precedence: System properties -> Environment variables | ||
| String jdbcUrl = resolveConfig("APP_JDBC_URL", "APP_JDBC_URL"); | ||
| String dbUser = resolveConfig("APP_DB_USER", "APP_DB_USER"); | ||
|
|
@@ -26,13 +30,226 @@ public void run() { | |
| "as system properties (-Dkey=value) or environment variables."); | ||
| } | ||
|
|
||
| try (Connection connection = DriverManager.getConnection(jdbcUrl, dbUser, dbPass)) { | ||
| // Connection, sparas i instansvariabel | ||
| try { | ||
| this.connection = DriverManager.getConnection(jdbcUrl, dbUser, dbPass); | ||
| System.out.println("Connected to database successfully."); | ||
| } catch (SQLException e) { | ||
| throw new RuntimeException(e); | ||
| throw new RuntimeException("Could not connect to database", e); | ||
| } | ||
|
|
||
| //Todo: Starting point for your code | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| // todo: Log in loop | ||
| while (true) { | ||
| System.out.print("Username (or 0 to exit): "); | ||
| String username = scanner.nextLine(); | ||
| if (username.equals("0")) return; | ||
|
|
||
| System.out.print("Password (or 0 to exit): "); | ||
| String password = scanner.nextLine(); | ||
| if (password.equals("0")) return; | ||
|
|
||
| if (login(username, password)) { | ||
| break; | ||
| } else { | ||
| System.out.println("Invalid username or password"); | ||
| } | ||
| } | ||
|
|
||
| // todo: menu after successfully logged in | ||
| boolean running = true; | ||
|
|
||
| while (running) { | ||
| printMenu(); | ||
| String choice = scanner.nextLine(); | ||
|
|
||
| switch (choice) { | ||
| case "1" -> listMoonMissions(); | ||
| case "2" -> getMoonMissionById(scanner); | ||
| case "3" -> countMissionsByYear(scanner); | ||
| case "4" -> createAccount(scanner); | ||
| case "5" -> updatePassword(scanner); | ||
| case "6" -> deleteAccount(scanner); | ||
| case "0" -> running = false; | ||
| default -> System.out.println("invalid"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // todo: LOGIN uses column `name` created from CONCAT in init.sql | ||
| private boolean login(String username, String password) { | ||
| String sql = "SELECT * FROM account WHERE name = ? AND password = ?"; | ||
|
|
||
| try (PreparedStatement stmt = connection.prepareStatement(sql)) { | ||
| stmt.setString(1, username); | ||
| stmt.setString(2, password); | ||
|
|
||
| ResultSet rs = stmt.executeQuery(); | ||
| return rs.next(); | ||
|
|
||
| } catch (SQLException e) { | ||
| throw new RuntimeException("Login failed", e); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // todo: print CLI menu | ||
| private void printMenu() { | ||
| System.out.println(" ---> MENU <--- "); | ||
| System.out.println("1) List moon missions"); | ||
| System.out.println("2) Get moon mission"); | ||
| System.out.println("3) Count missions by 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"); | ||
| } | ||
|
|
||
| // todo: choice 1 - List moon missions | ||
| private void listMoonMissions() { | ||
| String sql = "SELECT spacecraft FROM moon_mission"; | ||
|
|
||
| try (Statement stmt = connection.createStatement(); | ||
| ResultSet rs = stmt.executeQuery(sql)) { | ||
|
|
||
| while (rs.next()) { | ||
| System.out.println(rs.getString("spacecraft")); | ||
| } | ||
|
|
||
| } catch (SQLException e) { | ||
| throw new RuntimeException("Could not list missions", e); | ||
| } | ||
| } | ||
|
|
||
| // todo: choice 2 - Get mission by ID | ||
| private void getMoonMissionById(Scanner scanner) { | ||
| System.out.print("mission_id: "); | ||
| int id = Integer.parseInt(scanner.nextLine()); | ||
|
|
||
| String sql = "SELECT * FROM moon_mission WHERE mission_id = ?"; | ||
|
|
||
| try (PreparedStatement stmt = connection.prepareStatement(sql)) { | ||
| stmt.setInt(1, id); | ||
| ResultSet rs = stmt.executeQuery(); | ||
|
|
||
| if (rs.next()) { | ||
| System.out.println( | ||
| rs.getInt("mission_id") + " " + | ||
| rs.getString("spacecraft") + " " + | ||
| rs.getString("launch_date") | ||
| ); | ||
| System.out.println("------------"); | ||
| } else { | ||
| System.out.println("Invalid username or password"); | ||
| } | ||
|
|
||
| } catch (SQLException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // todo: choice 3 - Count missions by year | ||
| private void countMissionsByYear(Scanner scanner) { | ||
| System.out.print("Total missions for 2019: "); | ||
| int year = Integer.parseInt(scanner.nextLine()); | ||
|
|
||
| String sql = "SELECT COUNT(*) FROM moon_mission WHERE YEAR(launch_date) = ?"; | ||
|
|
||
| try (PreparedStatement stmt = connection.prepareStatement(sql)) { | ||
| stmt.setInt(1, year); | ||
| ResultSet rs = stmt.executeQuery(); | ||
| rs.next(); | ||
| System.out.println(rs.getInt(1)); | ||
|
|
||
| } catch (SQLException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
Comment on lines
+156
to
+172
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. Prompt for year should not hardcode 2019
System.out.print("Total missions for 2019: ");but then reads an arbitrary A clearer and spec‑aligned version would be: - System.out.print("Total missions for 2019: ");
+ System.out.print("year: ");
int year = Integer.parseInt(scanner.nextLine());The rest of the query logic (using 🤖 Prompt for AI Agents |
||
|
|
||
| //todo: Choice 4 - Add costumer method. | ||
| private void createAccount(Scanner scanner) { | ||
| System.out.print("first name: "); | ||
| String first = scanner.nextLine(); | ||
|
|
||
| System.out.print("last name: "); | ||
| String last = scanner.nextLine(); | ||
|
|
||
| System.out.print("ssn: "); | ||
| String ssn = scanner.nextLine(); | ||
|
|
||
| System.out.print("password: "); | ||
| String pw = scanner.nextLine(); | ||
|
|
||
| String sql = """ | ||
| INSERT INTO account (first_name, last_name, ssn, password) | ||
| VALUES (?, ?, ?, ?) | ||
| """; | ||
|
|
||
| try (PreparedStatement stmt = connection.prepareStatement(sql)) { | ||
| stmt.setString(1, first); | ||
| stmt.setString(2, last); | ||
| stmt.setString(3, ssn); | ||
| stmt.setString(4, pw); | ||
| stmt.executeUpdate(); | ||
|
|
||
| System.out.println("created"); | ||
|
|
||
| } catch (SQLException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // todo: Choice - 5 Update password method | ||
| private void updatePassword(Scanner scanner) { | ||
| System.out.print("user_id: "); | ||
| int id = Integer.parseInt(scanner.nextLine()); | ||
|
|
||
| System.out.print("new password: "); | ||
| String newPw = scanner.nextLine(); | ||
|
|
||
| String sql = "UPDATE account SET password = ? WHERE user_id = ?"; | ||
|
|
||
| try (PreparedStatement stmt = connection.prepareStatement(sql)) { | ||
| stmt.setString(1, newPw); | ||
| stmt.setInt(2, id); | ||
| stmt.executeUpdate(); | ||
|
|
||
| System.out.println("updated"); | ||
|
|
||
| } catch (SQLException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| // todo: Choice 6 - Delete account method | ||
| private void deleteAccount(Scanner scanner) { | ||
| System.out.print("user_id: "); | ||
| int id = Integer.parseInt(scanner.nextLine()); | ||
|
|
||
| String sql = "DELETE FROM account WHERE user_id = ?"; | ||
|
|
||
| try (PreparedStatement stmt = connection.prepareStatement(sql)) { | ||
| stmt.setInt(1, id); | ||
| stmt.executeUpdate(); | ||
|
|
||
| System.out.println("deleted"); | ||
|
|
||
| } catch (SQLException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Determines if the application is running in development mode based on system properties, | ||
| * environment variables, or command-line arguments. | ||
|
|
||
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.
Wrong error message when mission ID is not found
In
getMoonMissionById, theelsebranch currently prints:This is misleading in the context of looking up a mission and will confuse users (and likely tests that expect a mission‑specific message).
Apply a small fix like:
Optionally, you may also want to validate that the entered ID is numeric and handle
NumberFormatExceptionwith a simpleinvalidmessage, but that’s not strictly required by the assignment.🤖 Prompt for AI Agents