SQL的无限循环问题

时间:2018-12-09 21:21:48

标签: java sql loops while-loop

我的伴侣较早时没有运气,所以我想我会尝试的。我们这里有一个正在使用的程序正在导致一个无限循环的问题,我们无法弄清楚。基本上,该程序是针对电影租赁商店的雇员的,要求他提供8个不同的选项,他可以选择一个选项,作为回报,该选项将执行特定的代码路径。提示之一是“退出”选项,简而言之,我们希望员工继续提示他想要的选项,该选项将执行任何代码,直到该员工选择提示8退出并因此终止程序。相反,我们在员工选择的每个选择中都会遇到一个无限循环。例如,如果他选择提示1来显示所有电影,则它会不断地反复显示电影。任何有关如何解决此问题的建议将不胜感激。

import java.sql.*;
import java.util.Scanner;

public class MovieRentalDatabase {

   public static void main(String[]args){

      Scanner input = new Scanner(System.in);

      Connection conn = null;

      try 
      {
         conn =
            DriverManager.getConnection("jdbc:mysql://cs.neiu.edu:3306/18f315_Cody?serverTimezone=UTC&" +
                                   "user=18f315_Cody&password=");

         Statement stmt = conn.createStatement();



         System.out.println();
         System.out.println("Welcome To Windy City Video Employee Portal");
         System.out.println();



         System.out.println("Menu Choices: ");
         System.out.println("1. List All Movies Available");
         System.out.println("2. List All Customers From Rental Database");
         System.out.println("3. Add a New Movie To List");
         System.out.println("4. Enroll a New Member");
         System.out.println("5. Search Movies");
         System.out.println("6. Delete Movies From Store"); 
         System.out.println("7. Search For A Member ");
         System.out.println("8. Quit.");
         System.out.println();
         System.out.print("Enter your choice: ");
         int a = input.nextInt();
         System.out.println();
         int countmin =13;
         int countmax = 15;

         while(a != 8)
         {


         while(a < 1 || a > 8)
         {
            System.out.println("Choice Entered is Invalid!");
            System.out.println("Please Try Again");
            System.out.print("Enter your choice: ");
            a = input.nextInt();
            System.out.println();  
         }


         //If user input is 1
         if(a == 1)
         {
            String sql3 = "SELECT * FROM Movie";
            ResultSet rs = stmt.executeQuery(sql3);


            while(rs.next()){

               int movieID = rs.getInt("MovieID");
               String movieName = rs.getString("MovieName");
               Date release = rs.getDate("releaseDate");

            //display values from the sql statements from above
               System.out.println("MovieID: " + movieID);
               System.out.println("Movie Name: " + movieName);
               System.out.println("Release Date: " + release);
               System.out.println();
            } 
            rs.close();

         }


         //If user input is 2
         if(a == 2)
         {

            String sql4 = "SELECT * FROM MemberSignUp";
            ResultSet rs1 = stmt.executeQuery(sql4);

            while(rs1.next()){

               int memberID = rs1.getInt("MemberID");
               String firstName = rs1.getString("FirstName");
               String lastName = rs1.getString("LastName");

            //display values from the sql statements from above
               System.out.println("MemberID: " + memberID);
               System.out.println("First Name: " + firstName );
               System.out.println("Last Name: " + lastName);
               System.out.println();
            } 
            rs1.close();

         }

         //If user input is 3
         if(a == 3)
         {
            input.nextLine();
            System.out.print("Enter a New MovieID Number: ");
            String sql5 = input.nextLine();


            System.out.print("Enter The Movie Name: ");
            String sql6 = input.nextLine();

            System.out.print("Enter The Release Date: ");
            String sql7 = input.nextLine();

            String sql8 = "INSERT INTO Movie VALUES(" + sql5 + "," + "'" + sql6 + "'" + "," + "'" + sql7 + "');";


            stmt.executeUpdate(sql8);

         } 

         //If user input is 4
         if(a == 4)
         {
            input.nextLine();
            System.out.print("Enter a New MemberID Number: ");
            String sql9 = input.nextLine();
            int result = Integer.parseInt(sql9);


            while(result <= countmin || result >= countmax)
            {
               System.out.println();
               System.out.println("You Have Entered a MemberID That Already Exists!");
               System.out.println("Please use " + (countmin + 1) + " as the new MemberID");
               System.out.println();

               System.out.print("Enter a New MemberID Number: ");
               sql9 = input.nextLine();
               result = Integer.parseInt(sql9);
            }   


            System.out.print("Enter First Name: ");
            String sql10 = input.nextLine();

            System.out.print("Enter Last Name: ");
            String sql11 = input.nextLine();

            String sql12 = "INSERT INTO MemberSignUp VALUES(" + sql9 + "," + "'" + sql10 + "'" + "," + "'" + sql11 + "');";


            stmt.executeUpdate(sql12);
            countmin++;
            countmax++;

         } 

         //If user input is 5
         if(a == 5)
         {
            input.nextLine();

            System.out.print("Enter MovieID Number: ");
            String sql15 = input.nextLine();


            System.out.print("Enter Movie Name: ");
            String sql14 = input.nextLine();
            System.out.println();

            String sql16 = "Select MovieID, MovieName, releaseDate from Movie Where MovieName = " + "'"  + sql14 + "'" + "or MovieID = " + "'" + sql15 + "'" ;
            ResultSet rs2 = stmt.executeQuery(sql16);

            while(rs2.next()){

               int mID = rs2.getInt("MovieID");
               String mName = rs2.getString("MovieName");
               Date release1 = rs2.getDate("releaseDate");

               System.out.println("MovieID: " + mID);
               System.out.println("Movie Name: " + mName);
               System.out.println("Release Date: " + release1);
               System.out.println();

            } 
            rs2.close();
         }

         //If user input is 6
         if(a == 6)
         {
            input.nextLine();

            System.out.print("Enter MovieID Number: ");
            String sql17 = input.nextLine();


            System.out.print("Enter Movie Name: ");
            String sql18 = input.nextLine();

            System.out.println();
            System.out.println("Movie Sucessfully Deleted!");

            String sql19 = "DELETE FROM Movie Where MovieID = " + "'" + sql17 + "' AND MovieName = '" + sql18 + "'";
            stmt.executeUpdate(sql19);

         }

         //if user input is 7
         if(a == 7)
         {
            input.nextLine();

            System.out.print("Enter MemberID Number: ");
            String sql20 = input.nextLine();


            System.out.print("Enter First Name: ");
            String sql21 = input.nextLine();

            System.out.print("Enter Last Name: ");
            String sql22 = input.nextLine();

            String sql23 = "Select MemberID, FirstName, LastName from MemberSignUp Where MemberID = " + "'"  + sql20 + "'" + "or FirstName = " + "'" + sql21 + "'" + "or LastName = " + "'" + sql22 + "'" ;
            ResultSet rs3 = stmt.executeQuery(sql23);

            System.out.println();
            while(rs3.next()){

               int member = rs3.getInt("MemberID");
               String memberName = rs3.getString("FirstName");
               String memberLast = rs3.getString("LastName");

               System.out.println("MemberID: " + member);
               System.out.println("First Name: " + memberName);
               System.out.println("Last Name: " + memberLast);
               System.out.println();

            } 
            rs3.close();

         }


         //If user input is 8
         if(a == 8)
         {
            System.out.println();
            System.out.println("Enjoy The Rest Of Your Day!");
            System.exit(0);
         }

         }

      }catch (SQLException ex) 
      {
         // handle any errors
         System.out.println("SQLException: " + ex.getMessage());
         System.out.println("SQLState: " + ex.getSQLState());
         System.out.println("VendorError: " + ex.getErrorCode());
      }

   }
}

1 个答案:

答案 0 :(得分:0)

移动所有这些行:

    System.out.println("Menu Choices: ");
    System.out.println("1. List All Movies Available");
    System.out.println("2. List All Customers From Rental Database");
    System.out.println("3. Add a New Movie To List");
    System.out.println("4. Enroll a New Member");
    System.out.println("5. Search Movies");
    System.out.println("6. Delete Movies From Store");
    System.out.println("7. Search For A Member ");
    System.out.println("8. Quit.");
    System.out.println();
    System.out.print("Enter your choice: ");
    a = input.nextInt();  
    System.out.println();

紧接着:

while (a != 8) {

因此,在每次迭代的开始,都会向用户显示菜单。
在while循环之前,初始化变量a

int a = 0;

int中删除int a = input.nextInt();
您的代码太长,无法测试,但是我认为这可以解决无限循环问题。