What's new

Baka may makahelp sa logic neto

aenaen

Forum Veteran
Elite
Joined
Jan 3, 2021
Posts
1,381
Solutions
5
Reaction
1,201
Points
775
1672812801443.png

Java po pala language. any idea po! Thanks
 

Attachments

import java.sql.*; import java.util.Scanner; public class AppointmentScheduler { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/db_kainpepe"; // Database credentials static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { // Register JDBC driver Class.forName(JDBC_DRIVER); // Open a connection conn = DriverManager.getConnection(DB_URL, USER, PASS); // Create a statement stmt = conn.createStatement(); Scanner input = new Scanner(System.in); int choice = 0; do { System.out.println("What do you want to do?"); System.out.println("1. Show list of appointments"); System.out.println("2. Add a new appointment"); System.out.println("3. Update an appointment"); System.out.println("4. Exit"); choice = input.nextInt(); switch (choice) { case 1: // Show list of appointments showAppointments(stmt); break; case 2: // Add a new appointment addAppointment(stmt, input); break; case 3: // Update an appointment updateAppointment(stmt, input); break; case 4: // Exit break; default: System.out.println("Invalid choice."); break; } } while (choice != 4); // Clean-up environment stmt.close(); conn.close(); } catch (SQLException se) { // Handle errors for JDBC se.printStackTrace(); } catch (Exception e) { // Handle errors for Class.forName e.printStackTrace(); } finally { // Finally block used to close resources try { if (stmt != null) { stmt.close(); } } catch (SQLException se2) { } // Nothing we can do try { if (conn != null) { conn.close(); } } catch (SQLException se) { se.printStackTrace(); } } } // Method to show a list of appointments public static void showAppointments(Statement stmt) throws SQLException { // Execute a query String sql = "SELECT * FROM appointments"; ResultSet rs = stmt.executeQuery(sql); // Extract data from result set while (rs.next()) { // Retrieve by column name

how to update the status if approve change pending to approve if rejected add to table reject and remove from the list of pending

// Method to update the status of an appointment public static void updateAppointment( Statement stmt, Scanner input) throws SQLException { // Show the list of appointments showAppointments(stmt); System.out.print("Enter the ID of the appointment you want to update: "); int id = input.nextInt(); // Retrieve the current status of the appointment String sql = "SELECT status FROM appointments WHERE id = ?"; PreparedStatement preparedStmt = conn.prepareStatement(sql); preparedStmt.setInt(1, id); ResultSet rs = preparedStmt.executeQuery(); rs.next(); String currentStatus = rs.getString("status"); if (currentStatus.equals("pending")) { // Update the status of the appointment System.out.println("1. Approve"); System.out.println("2. Reject"); int choice = input.nextInt(); if (choice == 1) { // Approve the appointment sql = "UPDATE appointments SET status = 'approved' WHERE id = ?"; preparedStmt = conn.prepareStatement(sql); preparedStmt.setInt(1, id); preparedStmt.executeUpdate(); } else if (choice == 2) { // Reject the appointment and move it to the trash sql = "DELETE FROM appointments WHERE id = ?"; preparedStmt = conn.prepareStatement(sql); preparedStmt.setInt(1, id); preparedStmt.execute(); sql = "INSERT INTO trash (name, address, date, status) SELECT name, address, date, 'rejected' FROM appointments WHERE id = ?"; preparedStmt = conn.prepareStatement(sql); preparedStmt.setInt(1, id); preparedStmt.execute(); } else { System.out.println("Invalid choice."); } } else { System.out.println("This appointment cannot be updated because its status is not pending."); } }

how to add appoitment case 2

// Method to add a new appointment to the list public static void addAppointment(Statement stmt, Scanner input) throws SQLException { System.out.print("Enter name: "); String name = input.nextLine(); System.out.print("Enter address: "); String address = input.nextLine(); System.out.print("Enter date (YYYY-MM-DD): "); String date = input.nextLine(); String status = "pending"; // Insert the new appointment into the database String sql = "INSERT INTO appointments (name, address, date, status) VALUES (?, ?, ?, ?)"; PreparedStatement preparedStmt = conn.prepareStatement(sql); preparedStmt.setString(1, name); preparedStmt.setString(2, address); preparedStmt.setString(3, date); preparedStmt.setString(4, status); preparedStmt.execute(); }
 
import java.sql.*; import java.util.Scanner; public class AppointmentScheduler { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/db_kainpepe"; // Database credentials static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { // Register JDBC driver Class.forName(JDBC_DRIVER); // Open a connection conn = DriverManager.getConnection(DB_URL, USER, PASS); // Create a statement stmt = conn.createStatement(); Scanner input = new Scanner(System.in); int choice = 0; do { System.out.println("What do you want to do?"); System.out.println("1. Show list of appointments"); System.out.println("2. Add a new appointment"); System.out.println("3. Update an appointment"); System.out.println("4. Exit"); choice = input.nextInt(); switch (choice) { case 1: // Show list of appointments showAppointments(stmt); break; case 2: // Add a new appointment addAppointment(stmt, input); break; case 3: // Update an appointment updateAppointment(stmt, input); break; case 4: // Exit break; default: System.out.println("Invalid choice."); break; } } while (choice != 4); // Clean-up environment stmt.close(); conn.close(); } catch (SQLException se) { // Handle errors for JDBC se.printStackTrace(); } catch (Exception e) { // Handle errors for Class.forName e.printStackTrace(); } finally { // Finally block used to close resources try { if (stmt != null) { stmt.close(); } } catch (SQLException se2) { } // Nothing we can do try { if (conn != null) { conn.close(); } } catch (SQLException se) { se.printStackTrace(); } } } // Method to show a list of appointments public static void showAppointments(Statement stmt) throws SQLException { // Execute a query String sql = "SELECT * FROM appointments"; ResultSet rs = stmt.executeQuery(sql); // Extract data from result set while (rs.next()) { // Retrieve by column name

how to update the status if approve change pending to approve if rejected add to table reject and remove from the list of pending

// Method to update the status of an appointment public static void updateAppointment( Statement stmt, Scanner input) throws SQLException { // Show the list of appointments showAppointments(stmt); System.out.print("Enter the ID of the appointment you want to update: "); int id = input.nextInt(); // Retrieve the current status of the appointment String sql = "SELECT status FROM appointments WHERE id = ?"; PreparedStatement preparedStmt = conn.prepareStatement(sql); preparedStmt.setInt(1, id); ResultSet rs = preparedStmt.executeQuery(); rs.next(); String currentStatus = rs.getString("status"); if (currentStatus.equals("pending")) { // Update the status of the appointment System.out.println("1. Approve"); System.out.println("2. Reject"); int choice = input.nextInt(); if (choice == 1) { // Approve the appointment sql = "UPDATE appointments SET status = 'approved' WHERE id = ?"; preparedStmt = conn.prepareStatement(sql); preparedStmt.setInt(1, id); preparedStmt.executeUpdate(); } else if (choice == 2) { // Reject the appointment and move it to the trash sql = "DELETE FROM appointments WHERE id = ?"; preparedStmt = conn.prepareStatement(sql); preparedStmt.setInt(1, id); preparedStmt.execute(); sql = "INSERT INTO trash (name, address, date, status) SELECT name, address, date, 'rejected' FROM appointments WHERE id = ?"; preparedStmt = conn.prepareStatement(sql); preparedStmt.setInt(1, id); preparedStmt.execute(); } else { System.out.println("Invalid choice."); } } else { System.out.println("This appointment cannot be updated because its status is not pending."); } }

how to add appoitment case 2

// Method to add a new appointment to the list public static void addAppointment(Statement stmt, Scanner input) throws SQLException { System.out.print("Enter name: "); String name = input.nextLine(); System.out.print("Enter address: "); String address = input.nextLine(); System.out.print("Enter date (YYYY-MM-DD): "); String date = input.nextLine(); String status = "pending"; // Insert the new appointment into the database String sql = "INSERT INTO appointments (name, address, date, status) VALUES (?, ?, ?, ?)"; PreparedStatement preparedStmt = conn.prepareStatement(sql); preparedStmt.setString(1, name); preparedStmt.setString(2, address); preparedStmt.setString(3, date); preparedStmt.setString(4, status); preparedStmt.execute(); }
salamat ng marami master!
 

Similar threads

Back
Top