In: Computer Science
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();
}
}
}
}
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