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 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 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();
}
}
}
}
We can add a simple while in the code where we are taing from the user and we should use and declare the function as int so that we can return a value that will indicate if the database exist or not , if data exist in database we will exit the loop and if data does not exist in database we will repeat the loop.
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);
int m=0;
while(m!=0){
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
m=printUserData(email, pin); // call printUserData method with email and pin
}
}
private static int 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();
}
}
if(result == null)
{
return 0;
}
else{
return 1;}
}
}
}