Question

In: Computer Science

How do I get my program to ask the user to re enter the correct information...

How do I get my program to ask the user to re enter the correct information if the information entered does not match the database records? When I run my program it does let me know that the information entered does not match the database records but it does not ask me to enter the information again.

Please add code (in bold) in the proper area with short explanation

package MailMeSQL;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Scanner;

import com.mysql.cj.jdbc.JdbcConnection;

public class mailmeMain {

   public static void main(String[] args) {

       Scanner scanner = new Scanner(System.in);

       System.out.println("Please Enter your Email:");

       String email = scanner.nextLine(); //get email from user

       System.out.println("Please Enter your pin:");

       int pin = scanner.nextInt(); //get pin from user

       printUserData(email, pin); // call printUserData method with email and pin

   }

   private static void printUserData(String email, int pin) {

       String url = "jdbc:mysql://localhost:3306/mailme";

       String username = "root";

       String password = "Chicago#71519";

       Connection connection = null;

       PreparedStatement preparedStatement = null;

       ResultSet result = null;

       try {

           connection = DriverManager.getConnection(url, username, password);

           String query = "select * from tenant_profile where email=? and pin=?";

           preparedStatement = connection.prepareStatement(query); // prepare the sql query

           preparedStatement.setString(1, email); // bind email value

           preparedStatement.setInt(2, pin); //bind pin value

           result = preparedStatement.executeQuery(); //execute query

                           while (result.next()) { //fetch result

               System.out.println("Email=" + result.getString("email") + ",pin=" + result.getString("pin")

                       + ",Package=" + result.getString("packages"));

           }

       } catch (SQLException e) {

           e.printStackTrace();

       } finally {

           if (result != null)

               try {

                   connection.close();

                   result.close();

               } catch (SQLException e) {

                   e.printStackTrace();

               }

       }

   }

}

Solutions

Expert Solution

package MailMeSQL;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Scanner;

import com.mysql.cj.jdbc.JdbcConnection;

public class mailmeMain {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Please Enter your Email:");

String email = scanner.nextLine(); //get email from user

System.out.println("Please Enter your pin:");

int pin = scanner.nextInt(); //get pin from user

printUserData(email, pin); // call printUserData method with email and pin

}

private static void printUserData(String email, int pin) {

String url = "jdbc:mysql://localhost:3306/mailme";

String username = "root";

String password = "Chicago#71519";

Connection connection = null;

PreparedStatement preparedStatement = null;

ResultSet result = null;

try {

connection = DriverManager.getConnection(url, username, password);

String query = "select * from tenant_profile where email=? and pin=?";

preparedStatement = connection.prepareStatement(query); // prepare the sql query

preparedStatement.setString(1, email); // bind email value

preparedStatement.setInt(2, pin); //bind pin value

result = preparedStatement.executeQuery(); //execute query
if (result.next()) {
while (result.next()) { //fetch result

System.out.println("Email=" + result.getString("email") + ",pin=" + result.getString("pin")
+ ",Package=" + result.getString("packages"));

}
} else {
System.out.println("Invalid Credentials. Try again.");
Scanner scanner = new Scanner(System.in);

System.out.println("Please Enter your Email:");

email = scanner.nextLine(); //get email from user

System.out.println("Please Enter your pin:");

pin = scanner.nextInt(); //get pin from user
printUserData(email, pin);

}
} catch (SQLException e) {

e.printStackTrace();

} finally {

if (result != null) {
try {

connection.close();

result.close();

} catch (SQLException e) {

e.printStackTrace();

}
}

}

}

}

//bold code will ask you to re-enter data till you enter correct information


Related Solutions

How do I get my program to ask the user to re enter the correct information...
How do I get my program to ask the user to re enter the correct information if the information entered does not match the database records? When I run my program it does let me know that the information entered does not match but it does not ask me to enter the information again. package MailMeSQL; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; import com.mysql.cj.jdbc.JdbcConnection; public class mailmeMain {    public static void...
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
// This program ask the user to enter a character. It then performs a // linear...
// This program ask the user to enter a character. It then performs a // linear search on a character array and display the number of times // that the character appears on the array. If the character is not in the // array, then it will display a message saying that is was not found. // Add the necessary code for the program to work. // NOTE: // You don't have to edit anything in the main(), just in...
Write a program in Java to: Ask the user how many scores they want to enter...
Write a program in Java to: Ask the user how many scores they want to enter (=> Create an array based the entered size) Ask the user to enter their scores in the quarter (Fill the array with the entered scores(assigned to elements)) ==>> Use a "for" loop Find the greatest score Calculate and show the average Show the final grade based on the average (For example A,B,C ... ) Print the scores (the elements of the array) => Use...
Write a Java program to do the following: Ask the user to enter 10 first names...
Write a Java program to do the following: Ask the user to enter 10 first names (one word - no hyphen or apostrophe) using the keyboard. 1) Display the list of names one per line on the Console. 2) Display the names again, after eliminating duplicates using a HashSet (Your code MUST use HashSet).
1. Write a program that will ask the user to enter a character and then classify...
1. Write a program that will ask the user to enter a character and then classify the character as one of the following using only IF-ELSE and logical operators. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • Integer • Lower Case Vowel • Upper Case Vowel • Lower Case Consonant • Upper Case Consonant • Special Character
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
I need to ask a user what numbers they want to enter. They can enter as...
I need to ask a user what numbers they want to enter. They can enter as many as they like. Then inside I need to use conditionals to determine if the numbers are <=20, <=323 && > 30, >200. I can't figure out how to let the user enter as many inputs as they want. I know I need to use a loop to check each number entered and determine if it is small middle or greater but I can't...
Write a program in MIPS Assembly. This program will ask the user to enter 20 numbers....
Write a program in MIPS Assembly. This program will ask the user to enter 20 numbers. The program will store these numbers in an array in memory (sequential memory locations). It will then print out the list of numbers in three different formats: The numbers will be printed each on a separate line. The numbers will be printed on a single line, with spaces between the numbers. The program will ask the user to enter a number n. The program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT