K
K
Konstantin Malyarov2015-12-25 18:02:32
Java
Konstantin Malyarov, 2015-12-25 18:02:32

Recall of a line, duplication of request, how to solve?

There is a loop that iterates through the rows in the database and compares them with the entered row.
If the line repeats, then writes "Email is busy". Otherwise, the line is entered into the database.
There is a problem, if you first enter the mail that is already in the database, and then a new one, then the request of the first will be duplicated, after the second is completed.

private static String checkemail(Connection emailconnect)  {
        String query = "SELECT COUNT(*) FROM testdatabase.signup;";// Задаем параметр запроса - количество строк
        String email = null;// Задаем переменную email, которая будет возвращена в случаи, если он уникален
        try {
            email = Signup.reademail(email);// считываем с клавиатуры email
            Statement statement = emailconnect.createStatement();//создал статемент
            ResultSet resultSet = statement.executeQuery(query);//сделал запрос, ответ записал в переменную
            int id = 0;
            while (resultSet.next()){
                id = resultSet.getInt(1); // считываю по стокам и перевожу количество строк в ID
            }
            int x = 0;// нумерация запроса
            int y = 1;// нумерация строки
            while (x < id){
                String emailid = "SELECT * FROM testdatabase.signup where id = " + y + ";";// строка запроса
                Statement statementemail = emailconnect.createStatement();//создание статемента
                ResultSet resultSetemail = statementemail.executeQuery(emailid);//получение результатов
                resultSetemail.next();// хз зачем эта строка, но без нее не работает (я знаю, что это для того чтоб читать построчно)
                String emailres = resultSetemail.getString(2);//записываем результат в строку
                System.out.println("Это строка " + email);
                System.out.println("Это результат с БД " + y + " " + emailres);
                if(email.equals(emailres)){ //сравниваем результат из БД и результат введенный с клавиатуры
                    System.out.println("E-mail занят");
                    Signup.checkemail(emailconnect);// если повторяется вызываем метод снова
                }
                x = x + 1;
                y = y + 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return email;// иначе возвращаем значение email
    }

    private static String reademail(String email) {
        System.out.println("Введите E-mail:");//Объявил
        Scanner scanneremail = new Scanner(System.in);//метод
        email = scanneremail.nextLine();//scanner() для считывания строки
        return email;//вернул его значение

Here is the result when you first enter an existing email, then a new email.
Database connection established!
Enter E-mail:
[email protected]
This is the string [email protected]
This is the result from DB 1 [email protected]
E-mail is busy
Enter E-mail:
[email protected]
This is the string [email protected]
This is the result with DB 1 [email protected]
This is the string [email protected]
This is the result with DB 2 [email protected]
This is the string [email protected]
This is the result with DB 3 [email protected]
This is the string [email protected]
This result from database 4 [email protected]
This is the string [email protected]
This is the result from DB 2 [email protected]
This is the string [email protected]
This is the result from DB 3 [email protected]
This is the string [email protected]
This is the result from DB 4 [email protected]
E-mail is busy
Enter E -mail:
That is, the query to the database goes like this:
Reads the first line 1, found a match. Writes that the email is busy and asks for a new one. Enter a new e-mail.
Queries:
1
2
3
4
Returns to the old query.
2
3
4
How to treat?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2015-12-25
@Konstantin18ko

Move the email search functionality to the database
If the id is returned, then the email is there.
This will greatly simplify your code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question