A
A
askalidov2022-04-03 19:43:37
PostgreSQL
askalidov, 2022-04-03 19:43:37

How to rewrite a method (SQL query)?

I have a method:

public static int check(String name, String email, String password) throws SQLException {
        return statement.executeUpdate(String.format("""
                        SELECT name, email, password FROM userlogins
                        WHERE name = '%s' AND email = '%s' AND password = '%s'
                """, name, email, password));
    }

With this request (for certain name, email, password) a column is returned to pgAdmin.

I need to return true if there is such a column in the database - otherwise false, is it possible to implement this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
askalidov, 2022-04-04
@askalidov

This method returns true if there is a record in the database and false if it is not:

public static boolean check(String name, String email, String password) throws SQLException {

        ResultSet resultSet = statement.executeQuery(String.format("""
                SELECT count(*) as cnt FROM userlogins
                WHERE name = '%s' AND email = '%s' AND password = '%s'
                """, name, email, password));

        resultSet.next();

        return resultSet.getBoolean(1);

D
Dmitry Roo, 2022-04-03
@xez

postgres will return you not a "string", but three strings: name, email, password.
About your gibberish:
I take it statement is a PreparedStatement, so the executeUpdate method is :
"Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing , such as a DDL statement."
"returns nothing" translates to "returns nothing".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question