Answer the question
In order to leave comments, you need to log in
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));
}
Answer the question
In order to leave comments, you need to log in
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);
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 questionAsk a Question
731 491 924 answers to any question